mirror of
https://github.com/argoproj/argo-cd
synced 2026-04-21 17:07:16 +00:00
Merge branch 'master' into addRefreshAnnotation
This commit is contained in:
commit
ed50b93b9d
4 changed files with 172 additions and 21 deletions
|
|
@ -1295,6 +1295,41 @@ Are you sure you want to disable auto-sync and rollback application '${props.mat
|
|||
action: () => AppUtils.showDeploy('all', null, appContext),
|
||||
disabled: !app.spec.source && (!app.spec.sources || app.spec.sources.length === 0) && !app.spec.sourceHydrator
|
||||
},
|
||||
{
|
||||
iconClassName: 'fa fa-toggle-on',
|
||||
title: <ActionMenuItem actionLabel='Toggle Auto-Sync' />,
|
||||
action: async () => {
|
||||
const isEnabled = app.spec.syncPolicy?.automated && app.spec.syncPolicy.automated.enabled !== false;
|
||||
const confirmationTitle = isEnabled ? 'Disable Auto-Sync?' : 'Enable Auto-Sync?';
|
||||
const confirmed = await appContext.popup.confirm(
|
||||
confirmationTitle,
|
||||
isEnabled
|
||||
? 'Are you sure you want to disable automated application synchronization'
|
||||
: 'If enabled, application will automatically sync when changes are detected'
|
||||
);
|
||||
if (!confirmed) return;
|
||||
try {
|
||||
await services.applications.runResourceAction(
|
||||
app.metadata.name,
|
||||
app.metadata.namespace,
|
||||
{
|
||||
name: app.metadata.name,
|
||||
namespace: app.metadata.namespace,
|
||||
group: 'argoproj.io',
|
||||
kind: 'Application',
|
||||
version: 'v1alpha1'
|
||||
} as appModels.ResourceNode,
|
||||
'toggle-auto-sync',
|
||||
[]
|
||||
);
|
||||
} catch (e) {
|
||||
appContext.notifications.show({
|
||||
content: <ErrorNotification title={`Unable to "${confirmationTitle.replace(/\?/g, '')}"`} e={e} />,
|
||||
type: NotificationType.Error
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
...(app.status?.operationState?.phase === 'Running' && app.status.resources.find(r => r.requiresDeletionConfirmation)
|
||||
? [
|
||||
{
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ import {services} from '../../../shared/services';
|
|||
|
||||
import {ApplicationSyncOptionsField} from '../application-sync-options/application-sync-options';
|
||||
import {RevisionFormField} from '../revision-form-field/revision-form-field';
|
||||
import {ComparisonStatusIcon, HealthStatusIcon, syncStatusMessage, urlPattern, formatCreationTimestamp, getAppDefaultSource, getAppSpecDefaultSource} from '../utils';
|
||||
import {ComparisonStatusIcon, HealthStatusIcon, syncStatusMessage, urlPattern, formatCreationTimestamp, getAppDefaultSource, getAppSpecDefaultSource, appRBACName} from '../utils';
|
||||
import {ApplicationRetryOptions} from '../application-retry-options/application-retry-options';
|
||||
import {ApplicationRetryView} from '../application-retry-view/application-retry-view';
|
||||
import {Link} from 'react-router-dom';
|
||||
|
|
@ -528,6 +528,56 @@ export const ApplicationSummary = (props: ApplicationSummaryProps) => {
|
|||
]
|
||||
: [...standardSourceItems, ...sourceItems];
|
||||
|
||||
async function toggleAutoSync(ctx: ContextApis) {
|
||||
const isEnabled = app.spec.syncPolicy?.automated && app.spec.syncPolicy.automated.enabled !== false;
|
||||
const confirmationTitle = isEnabled ? 'Disable Auto-Sync?' : 'Enable Auto-Sync?';
|
||||
const confirmationText = isEnabled
|
||||
? 'Are you sure you want to disable automated application synchronization'
|
||||
: 'If checked, application will automatically sync when changes are detected';
|
||||
const confirmed = await ctx.popup.confirm(confirmationTitle, confirmationText);
|
||||
if (confirmed) {
|
||||
try {
|
||||
setChangeSync(true);
|
||||
const canUpdate = await services.accounts.canI('applications', 'update', appRBACName(app)).catch(() => false);
|
||||
if (canUpdate) {
|
||||
const updatedApp = JSON.parse(JSON.stringify(props.app)) as models.Application;
|
||||
if (!updatedApp.spec.syncPolicy) {
|
||||
updatedApp.spec.syncPolicy = {};
|
||||
}
|
||||
const existingAutomated = updatedApp.spec.syncPolicy.automated;
|
||||
updatedApp.spec.syncPolicy.automated = {
|
||||
prune: existingAutomated?.prune ?? false,
|
||||
selfHeal: existingAutomated?.selfHeal ?? false,
|
||||
enabled: !isEnabled
|
||||
};
|
||||
await updateApp(updatedApp, {validate: false});
|
||||
} else {
|
||||
await services.applications.runResourceAction(
|
||||
app.metadata.name,
|
||||
app.metadata.namespace,
|
||||
{
|
||||
name: app.metadata.name,
|
||||
namespace: app.metadata.namespace,
|
||||
group: 'argoproj.io',
|
||||
kind: 'Application',
|
||||
version: 'v1alpha1'
|
||||
} as models.ResourceNode,
|
||||
'toggle-auto-sync',
|
||||
[]
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
ctx.notifications.show({
|
||||
content: <ErrorNotification title={`Unable to "${confirmationTitle.replace(/\?/g, '')}"`} e={e} />,
|
||||
type: NotificationType.Error
|
||||
});
|
||||
} finally {
|
||||
setChangeSync(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handler for the PRUNE RESOURCES and SELF HEAL checkboxes only; auto-sync toggling lives in `toggleAutoSync` above.
|
||||
async function setAutoSync(ctx: ContextApis, confirmationTitle: string, confirmationText: string, prune: boolean, selfHeal: boolean, enable: boolean) {
|
||||
const confirmed = await ctx.popup.confirm(confirmationTitle, confirmationText);
|
||||
if (confirmed) {
|
||||
|
|
@ -542,7 +592,7 @@ export const ApplicationSummary = (props: ApplicationSummaryProps) => {
|
|||
await updateApp(updatedApp, {validate: false});
|
||||
} catch (e) {
|
||||
ctx.notifications.show({
|
||||
content: <ErrorNotification title={`Unable to "${confirmationTitle.replace(/\?/g, '')}:`} e={e} />,
|
||||
content: <ErrorNotification title={`Unable to "${confirmationTitle.replace(/\?/g, '')}"`} e={e} />,
|
||||
type: NotificationType.Error
|
||||
});
|
||||
} finally {
|
||||
|
|
@ -663,18 +713,8 @@ export const ApplicationSummary = (props: ApplicationSummaryProps) => {
|
|||
<div className='columns small-12'>
|
||||
<div className='checkbox-container'>
|
||||
<Checkbox
|
||||
onChange={async (val: boolean) => {
|
||||
const automated = app.spec.syncPolicy?.automated || {prune: false, selfHeal: false};
|
||||
setAutoSync(
|
||||
ctx,
|
||||
val ? 'Enable Auto-Sync?' : 'Disable Auto-Sync?',
|
||||
val
|
||||
? 'If checked, application will automatically sync when changes are detected'
|
||||
: 'Are you sure you want to disable automated application synchronization',
|
||||
automated.prune,
|
||||
automated.selfHeal,
|
||||
val
|
||||
);
|
||||
onChange={async () => {
|
||||
await toggleAutoSync(ctx);
|
||||
}}
|
||||
checked={app.spec.syncPolicy?.automated && app.spec.syncPolicy.automated.enabled !== false}
|
||||
id='enable-auto-sync'
|
||||
|
|
|
|||
|
|
@ -397,15 +397,16 @@ func (c *diffConfig) DiffFromCache(appName string) (bool, []*v1alpha1.ResourceDi
|
|||
return false, nil
|
||||
}
|
||||
cachedDiff := make([]*v1alpha1.ResourceDiff, 0)
|
||||
if c.stateCache != nil {
|
||||
err := c.stateCache.GetAppManagedResources(appName, &cachedDiff)
|
||||
if err != nil {
|
||||
log.Errorf("DiffFromCache error: error getting managed resources for app %s: %s", appName, err)
|
||||
return false, nil
|
||||
err := c.stateCache.GetAppManagedResources(appName, &cachedDiff)
|
||||
if err != nil {
|
||||
if errors.Is(err, appstatecache.ErrCacheMiss) {
|
||||
log.Warnf("DiffFromCache: cannot get managed resources for app %s: %s", appName, err)
|
||||
} else {
|
||||
log.Errorf("DiffFromCache: cannot get managed resources for app %s: %s", appName, err)
|
||||
}
|
||||
return true, cachedDiff
|
||||
return false, nil
|
||||
}
|
||||
return false, nil
|
||||
return true, cachedDiff
|
||||
}
|
||||
|
||||
// preDiffNormalize applies the normalization of live and target resources before invoking
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
package diff_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/sirupsen/logrus/hooks/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
|
|
@ -12,6 +15,7 @@ import (
|
|||
argo "github.com/argoproj/argo-cd/v3/util/argo/diff"
|
||||
"github.com/argoproj/argo-cd/v3/util/argo/normalizers"
|
||||
"github.com/argoproj/argo-cd/v3/util/argo/testdata"
|
||||
cacheutil "github.com/argoproj/argo-cd/v3/util/cache"
|
||||
appstatecache "github.com/argoproj/argo-cd/v3/util/cache/appstate"
|
||||
)
|
||||
|
||||
|
|
@ -248,3 +252,74 @@ func TestDiffConfigBuilder(t *testing.T) {
|
|||
require.Nil(t, diffConfig)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDiffFromCache(t *testing.T) {
|
||||
t.Run("returns false and logs warning on cache miss", func(t *testing.T) {
|
||||
// given
|
||||
hook := test.NewLocal(logrus.StandardLogger())
|
||||
defer hook.Reset()
|
||||
|
||||
// Real in-memory cache with no data stored → triggers ErrCacheMiss
|
||||
cache := appstatecache.NewCache(cacheutil.NewCache(cacheutil.NewInMemoryCache(0)), 0)
|
||||
|
||||
diffConfig, err := argo.NewDiffConfigBuilder().
|
||||
WithDiffSettings([]v1alpha1.ResourceIgnoreDifferences{}, map[string]v1alpha1.ResourceOverride{}, false, normalizers.IgnoreNormalizerOpts{}).
|
||||
WithTracking("", "").
|
||||
WithCache(cache, "application-name").
|
||||
Build()
|
||||
require.NoError(t, err)
|
||||
|
||||
// when
|
||||
found, cachedDiff := diffConfig.DiffFromCache("application-name")
|
||||
|
||||
// then
|
||||
assert.False(t, found)
|
||||
assert.Nil(t, cachedDiff)
|
||||
require.Len(t, hook.Entries, 1)
|
||||
assert.Equal(t, logrus.WarnLevel, hook.LastEntry().Level)
|
||||
assert.Contains(t, hook.LastEntry().Message, "cannot get managed resources for app application-name")
|
||||
assert.Contains(t, hook.LastEntry().Message, appstatecache.ErrCacheMiss.Error())
|
||||
})
|
||||
|
||||
t.Run("returns false and logs error on cache failure", func(t *testing.T) {
|
||||
// given
|
||||
hook := test.NewLocal(logrus.StandardLogger())
|
||||
defer hook.Reset()
|
||||
|
||||
errCache := errors.New("cache unavailable")
|
||||
// Custom cache client that always returns the given error on Get
|
||||
failClient := &failingCacheClient{
|
||||
InMemoryCache: cacheutil.NewInMemoryCache(0),
|
||||
err: errCache,
|
||||
}
|
||||
cache := appstatecache.NewCache(cacheutil.NewCache(failClient), 0)
|
||||
|
||||
diffConfig, err := argo.NewDiffConfigBuilder().
|
||||
WithDiffSettings([]v1alpha1.ResourceIgnoreDifferences{}, map[string]v1alpha1.ResourceOverride{}, false, normalizers.IgnoreNormalizerOpts{}).
|
||||
WithTracking("", "").
|
||||
WithCache(cache, "application-name").
|
||||
Build()
|
||||
require.NoError(t, err)
|
||||
|
||||
// when
|
||||
found, cachedDiff := diffConfig.DiffFromCache("application-name")
|
||||
|
||||
// then
|
||||
assert.False(t, found)
|
||||
assert.Nil(t, cachedDiff)
|
||||
require.Len(t, hook.Entries, 1)
|
||||
assert.Equal(t, logrus.ErrorLevel, hook.LastEntry().Level)
|
||||
assert.Contains(t, hook.LastEntry().Message, "cannot get managed resources for app application-name")
|
||||
assert.Contains(t, hook.LastEntry().Message, errCache.Error())
|
||||
})
|
||||
}
|
||||
|
||||
// failingCacheClient embeds InMemoryCache and overrides Get to always return a custom error.
|
||||
type failingCacheClient struct {
|
||||
*cacheutil.InMemoryCache
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *failingCacheClient) Get(_ string, _ any) error {
|
||||
return f.err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue