Skip to content

Commit e25fdd8

Browse files
anandrkskdPaulSonOfLars
authored andcommitted
feat: add enable field for automatedSync (argoproj#21999)
Signed-off-by: Anand Kumar Singh <[email protected]>
1 parent 8682e25 commit e25fdd8

22 files changed

+1222
-755
lines changed

applicationset/controllers/applicationset_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,7 @@ func (r *ApplicationSetReconciler) syncValidApplications(logCtx *log.Entry, appl
13981398
pruneEnabled := false
13991399

14001400
// ensure that Applications generated with RollingSync do not have an automated sync policy, since the AppSet controller will handle triggering the sync operation instead
1401-
if validApps[i].Spec.SyncPolicy != nil && validApps[i].Spec.SyncPolicy.Automated != nil {
1401+
if validApps[i].Spec.SyncPolicy != nil && validApps[i].Spec.SyncPolicy.IsAutomatedSyncEnabled() {
14021402
pruneEnabled = validApps[i].Spec.SyncPolicy.Automated.Prune
14031403
validApps[i].Spec.SyncPolicy.Automated = nil
14041404
}

assets/swagger.json

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/argocd/commands/app.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ func printAppSummaryTable(app *argoappv1.Application, appURL string, windows *ar
664664
}
665665

666666
var syncPolicy string
667-
if app.Spec.SyncPolicy != nil && app.Spec.SyncPolicy.Automated != nil {
667+
if app.Spec.SyncPolicy != nil && app.Spec.SyncPolicy.IsAutomatedSyncEnabled() {
668668
syncPolicy = "Automated"
669669
if app.Spec.SyncPolicy.Automated.Prune {
670670
syncPolicy += " (Prune)"
@@ -1726,7 +1726,7 @@ func NewApplicationListCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
17261726
}
17271727

17281728
func formatSyncPolicy(app argoappv1.Application) string {
1729-
if app.Spec.SyncPolicy == nil || app.Spec.SyncPolicy.Automated == nil {
1729+
if app.Spec.SyncPolicy == nil || !app.Spec.SyncPolicy.IsAutomatedSyncEnabled() {
17301730
return "Manual"
17311731
}
17321732
policy := "Auto"
@@ -2158,7 +2158,7 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
21582158
}
21592159

21602160
if local != "" {
2161-
if app.Spec.SyncPolicy != nil && app.Spec.SyncPolicy.Automated != nil && !dryRun {
2161+
if app.Spec.SyncPolicy != nil && app.Spec.SyncPolicy.IsAutomatedSyncEnabled() && !dryRun {
21622162
log.Fatal("Cannot use local sync when Automatic Sync Policy is enabled except with --dry-run")
21632163
}
21642164

cmd/argocd/commands/applicationset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ func printAppSetSummaryTable(appSet *arogappsetv1.ApplicationSet) {
460460
syncPolicyStr string
461461
syncPolicy = appSet.Spec.Template.Spec.SyncPolicy
462462
)
463-
if syncPolicy != nil && syncPolicy.Automated != nil {
463+
if syncPolicy != nil && syncPolicy.IsAutomatedSyncEnabled() {
464464
syncPolicyStr = "Automated"
465465
if syncPolicy.Automated.Prune {
466466
syncPolicyStr += " (Prune)"

cmd/util/app.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -289,19 +289,19 @@ func SetAppSpecOptions(flags *pflag.FlagSet, spec *argoappv1.ApplicationSpec, ap
289289
}
290290
})
291291
if flags.Changed("auto-prune") {
292-
if spec.SyncPolicy == nil || spec.SyncPolicy.Automated == nil {
292+
if spec.SyncPolicy == nil || !spec.SyncPolicy.IsAutomatedSyncEnabled() {
293293
log.Fatal("Cannot set --auto-prune: application not configured with automatic sync")
294294
}
295295
spec.SyncPolicy.Automated.Prune = appOpts.autoPrune
296296
}
297297
if flags.Changed("self-heal") {
298-
if spec.SyncPolicy == nil || spec.SyncPolicy.Automated == nil {
298+
if spec.SyncPolicy == nil || !spec.SyncPolicy.IsAutomatedSyncEnabled() {
299299
log.Fatal("Cannot set --self-heal: application not configured with automatic sync")
300300
}
301301
spec.SyncPolicy.Automated.SelfHeal = appOpts.selfHeal
302302
}
303303
if flags.Changed("allow-empty") {
304-
if spec.SyncPolicy == nil || spec.SyncPolicy.Automated == nil {
304+
if spec.SyncPolicy == nil || !spec.SyncPolicy.IsAutomatedSyncEnabled() {
305305
log.Fatal("Cannot set --allow-empty: application not configured with automatic sync")
306306
}
307307
spec.SyncPolicy.Automated.AllowEmpty = appOpts.allowEmpty

controller/appcontroller.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2060,7 +2060,7 @@ func (ctrl *ApplicationController) autoSync(app *appv1.Application, syncStatus *
20602060
logCtx = logCtx.WithField("time_ms", time.Since(ts.StartTime).Milliseconds())
20612061
logCtx.Debug("Finished auto sync")
20622062
}()
2063-
if app.Spec.SyncPolicy == nil || app.Spec.SyncPolicy.Automated == nil {
2063+
if app.Spec.SyncPolicy == nil || !app.Spec.SyncPolicy.IsAutomatedSyncEnabled() {
20642064
return nil, 0
20652065
}
20662066

@@ -2486,14 +2486,14 @@ func isOperationInProgress(app *appv1.Application) bool {
24862486
func automatedSyncEnabled(oldApp *appv1.Application, newApp *appv1.Application) bool {
24872487
oldEnabled := false
24882488
oldSelfHealEnabled := false
2489-
if oldApp.Spec.SyncPolicy != nil && oldApp.Spec.SyncPolicy.Automated != nil {
2489+
if oldApp.Spec.SyncPolicy != nil && oldApp.Spec.SyncPolicy.IsAutomatedSyncEnabled() {
24902490
oldEnabled = true
24912491
oldSelfHealEnabled = oldApp.Spec.SyncPolicy.Automated.SelfHeal
24922492
}
24932493

24942494
newEnabled := false
24952495
newSelfHealEnabled := false
2496-
if newApp.Spec.SyncPolicy != nil && newApp.Spec.SyncPolicy.Automated != nil {
2496+
if newApp.Spec.SyncPolicy != nil && newApp.Spec.SyncPolicy.IsAutomatedSyncEnabled() {
24972497
newEnabled = true
24982498
newSelfHealEnabled = newApp.Spec.SyncPolicy.Automated.SelfHeal
24992499
}

controller/appcontroller_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,24 @@ func TestAutoSync(t *testing.T) {
602602
assert.False(t, app.Operation.Sync.Prune)
603603
}
604604

605+
func TestAutoSyncEnabledSetToTrue(t *testing.T) {
606+
app := newFakeApp()
607+
enable := true
608+
app.Spec.SyncPolicy.Automated = &v1alpha1.SyncPolicyAutomated{Enable: &enable}
609+
ctrl := newFakeController(&fakeData{apps: []runtime.Object{app}}, nil)
610+
syncStatus := v1alpha1.SyncStatus{
611+
Status: v1alpha1.SyncStatusCodeOutOfSync,
612+
Revision: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
613+
}
614+
cond, _ := ctrl.autoSync(app, &syncStatus, []v1alpha1.ResourceStatus{{Name: "guestbook", Kind: kube.DeploymentKind, Status: v1alpha1.SyncStatusCodeOutOfSync}}, true)
615+
assert.Nil(t, cond)
616+
app, err := ctrl.applicationClientset.ArgoprojV1alpha1().Applications(test.FakeArgoCDNamespace).Get(t.Context(), "my-app", metav1.GetOptions{})
617+
require.NoError(t, err)
618+
assert.NotNil(t, app.Operation)
619+
assert.NotNil(t, app.Operation.Sync)
620+
assert.False(t, app.Operation.Sync.Prune)
621+
}
622+
605623
func TestMultiSourceSelfHeal(t *testing.T) {
606624
// Simulate OutOfSync caused by object change in cluster
607625
// So our Sync Revisions and SyncStatus Revisions should deep equal
@@ -711,6 +729,23 @@ func TestSkipAutoSync(t *testing.T) {
711729
assert.Nil(t, app.Operation)
712730
})
713731

732+
// Verify we skip when auto-sync is disabled
733+
t.Run("AutoSyncEnableFieldIsSetFalse", func(t *testing.T) {
734+
app := newFakeApp()
735+
enable := false
736+
app.Spec.SyncPolicy.Automated = &v1alpha1.SyncPolicyAutomated{Enable: &enable}
737+
ctrl := newFakeController(&fakeData{apps: []runtime.Object{app}}, nil)
738+
syncStatus := v1alpha1.SyncStatus{
739+
Status: v1alpha1.SyncStatusCodeOutOfSync,
740+
Revision: "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
741+
}
742+
cond, _ := ctrl.autoSync(app, &syncStatus, []v1alpha1.ResourceStatus{}, true)
743+
assert.Nil(t, cond)
744+
app, err := ctrl.applicationClientset.ArgoprojV1alpha1().Applications(test.FakeArgoCDNamespace).Get(t.Context(), "my-app", metav1.GetOptions{})
745+
require.NoError(t, err)
746+
assert.Nil(t, app.Operation)
747+
})
748+
714749
// Verify we skip when application is marked for deletion
715750
t.Run("ApplicationIsMarkedForDeletion", func(t *testing.T) {
716751
app := newFakeApp()

controller/metrics/metrics.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ func (c *appCollector) collectApps(ch chan<- prometheus.Metric, app *argoappv1.A
412412
healthStatus = health.HealthStatusUnknown
413413
}
414414

415-
autoSyncEnabled := app.Spec.SyncPolicy != nil && app.Spec.SyncPolicy.Automated != nil
415+
autoSyncEnabled := app.Spec.SyncPolicy != nil && app.Spec.SyncPolicy.IsAutomatedSyncEnabled()
416416

417417
addGauge(descAppInfo, 1, strconv.FormatBool(autoSyncEnabled), git.NormalizeGitURL(app.Spec.GetSource().RepoURL), app.Spec.Destination.Server, app.Spec.Destination.Namespace, string(syncStatus), string(healthStatus), operation)
418418

docs/user-guide/auto_sync.md

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ spec:
1818
syncPolicy:
1919
automated: {}
2020
```
21+
Application CRD now also support explicitly setting automated sync to be turn on or off by using `spec.syncPolicy.automated.enable` flag to true or false. When `enable` field is set to true, Automated Sync is active and when set to false controller will skip automated sync even if `prune`, `self-heal` and `allowEmpty` are set.
22+
```yaml
23+
spec:
24+
syncPolicy:
25+
automated:
26+
enable: true
27+
```
28+
29+
!!!note
30+
Setting `spec.syncPolicy.automated.enable` flag to null will be treated as automated sync as enabled. When using `enable` field set to false, fields like `prune`, `self-heal` and `allowEmpty` can be set without enabling them.
2131

2232
## Temporarily toggling auto-sync for applications managed by ApplicationSets
2333

0 commit comments

Comments
 (0)