Skip to content

Commit d5d7e8f

Browse files
authored
fix: allow flags to be set when auto-sync is disabled (#24328) (#24380)
Signed-off-by: downfa11 <[email protected]>
1 parent 7357465 commit d5d7e8f

File tree

6 files changed

+72
-30
lines changed

6 files changed

+72
-30
lines changed

cmd/util/app.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ func AddAppFlags(command *cobra.Command, opts *AppOptions) {
136136
command.Flags().StringVar(&opts.project, "project", "", "Application project name")
137137
command.Flags().StringVar(&opts.syncPolicy, "sync-policy", "", "Set the sync policy (one of: manual (aliases of manual: none), automated (aliases of automated: auto, automatic))")
138138
command.Flags().StringArrayVar(&opts.syncOptions, "sync-option", []string{}, "Add or remove a sync option, e.g add `Prune=false`. Remove using `!` prefix, e.g. `!Prune=false`")
139-
command.Flags().BoolVar(&opts.autoPrune, "auto-prune", false, "Set automatic pruning when sync is automated")
140-
command.Flags().BoolVar(&opts.selfHeal, "self-heal", false, "Set self healing when sync is automated")
141-
command.Flags().BoolVar(&opts.allowEmpty, "allow-empty", false, "Set allow zero live resources when sync is automated")
139+
command.Flags().BoolVar(&opts.autoPrune, "auto-prune", false, "Set automatic pruning for automated sync policy")
140+
command.Flags().BoolVar(&opts.selfHeal, "self-heal", false, "Set self healing for automated sync policy")
141+
command.Flags().BoolVar(&opts.allowEmpty, "allow-empty", false, "Set allow zero live resources for automated sync policy")
142142
command.Flags().StringVar(&opts.namePrefix, "nameprefix", "", "Kustomize nameprefix")
143143
command.Flags().StringVar(&opts.nameSuffix, "namesuffix", "", "Kustomize namesuffix")
144144
command.Flags().StringVar(&opts.kustomizeVersion, "kustomize-version", "", "Kustomize version")
@@ -284,25 +284,26 @@ func SetAppSpecOptions(flags *pflag.FlagSet, spec *argoappv1.ApplicationSpec, ap
284284
spec.SyncPolicy.Retry.Refresh = appOpts.retryRefresh
285285
}
286286
})
287-
if flags.Changed("auto-prune") {
288-
if spec.SyncPolicy == nil || !spec.SyncPolicy.IsAutomatedSyncEnabled() {
289-
log.Fatal("Cannot set --auto-prune: application not configured with automatic sync")
287+
288+
if flags.Changed("auto-prune") || flags.Changed("self-heal") || flags.Changed("allow-empty") {
289+
if spec.SyncPolicy == nil {
290+
spec.SyncPolicy = &argoappv1.SyncPolicy{}
290291
}
291-
spec.SyncPolicy.Automated.Prune = appOpts.autoPrune
292-
}
293-
if flags.Changed("self-heal") {
294-
if spec.SyncPolicy == nil || !spec.SyncPolicy.IsAutomatedSyncEnabled() {
295-
log.Fatal("Cannot set --self-heal: application not configured with automatic sync")
292+
if spec.SyncPolicy.Automated == nil {
293+
disabled := false
294+
spec.SyncPolicy.Automated = &argoappv1.SyncPolicyAutomated{Enabled: &disabled}
296295
}
297-
spec.SyncPolicy.Automated.SelfHeal = appOpts.selfHeal
298-
}
299-
if flags.Changed("allow-empty") {
300-
if spec.SyncPolicy == nil || !spec.SyncPolicy.IsAutomatedSyncEnabled() {
301-
log.Fatal("Cannot set --allow-empty: application not configured with automatic sync")
296+
297+
if flags.Changed("auto-prune") {
298+
spec.SyncPolicy.Automated.Prune = appOpts.autoPrune
299+
}
300+
if flags.Changed("self-heal") {
301+
spec.SyncPolicy.Automated.SelfHeal = appOpts.selfHeal
302+
}
303+
if flags.Changed("allow-empty") {
304+
spec.SyncPolicy.Automated.AllowEmpty = appOpts.allowEmpty
302305
}
303-
spec.SyncPolicy.Automated.AllowEmpty = appOpts.allowEmpty
304306
}
305-
306307
return visited
307308
}
308309

cmd/util/app_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,47 @@ func Test_setAppSpecOptions(t *testing.T) {
267267
require.NoError(t, f.SetFlag("sync-option", "!a=1"))
268268
assert.Nil(t, f.spec.SyncPolicy)
269269
})
270+
t.Run("AutoPruneFlag", func(t *testing.T) {
271+
f := newAppOptionsFixture()
272+
273+
// syncPolicy is nil (automated.enabled = false)
274+
require.NoError(t, f.SetFlag("auto-prune", "true"))
275+
require.NotNil(t, f.spec.SyncPolicy.Automated.Enabled)
276+
assert.False(t, *f.spec.SyncPolicy.Automated.Enabled)
277+
assert.True(t, f.spec.SyncPolicy.Automated.Prune)
278+
279+
// automated.enabled = true
280+
*f.spec.SyncPolicy.Automated.Enabled = true
281+
require.NoError(t, f.SetFlag("auto-prune", "false"))
282+
assert.True(t, *f.spec.SyncPolicy.Automated.Enabled)
283+
assert.False(t, f.spec.SyncPolicy.Automated.Prune)
284+
})
285+
t.Run("SelfHealFlag", func(t *testing.T) {
286+
f := newAppOptionsFixture()
287+
288+
require.NoError(t, f.SetFlag("self-heal", "true"))
289+
require.NotNil(t, f.spec.SyncPolicy.Automated.Enabled)
290+
assert.False(t, *f.spec.SyncPolicy.Automated.Enabled)
291+
assert.True(t, f.spec.SyncPolicy.Automated.SelfHeal)
292+
293+
*f.spec.SyncPolicy.Automated.Enabled = true
294+
require.NoError(t, f.SetFlag("self-heal", "false"))
295+
assert.True(t, *f.spec.SyncPolicy.Automated.Enabled)
296+
assert.False(t, f.spec.SyncPolicy.Automated.SelfHeal)
297+
})
298+
t.Run("AllowEmptyFlag", func(t *testing.T) {
299+
f := newAppOptionsFixture()
300+
301+
require.NoError(t, f.SetFlag("allow-empty", "true"))
302+
require.NotNil(t, f.spec.SyncPolicy.Automated.Enabled)
303+
assert.False(t, *f.spec.SyncPolicy.Automated.Enabled)
304+
assert.True(t, f.spec.SyncPolicy.Automated.AllowEmpty)
305+
306+
*f.spec.SyncPolicy.Automated.Enabled = true
307+
require.NoError(t, f.SetFlag("allow-empty", "false"))
308+
assert.True(t, *f.spec.SyncPolicy.Automated.Enabled)
309+
assert.False(t, f.spec.SyncPolicy.Automated.AllowEmpty)
310+
})
270311
t.Run("RetryLimit", func(t *testing.T) {
271312
require.NoError(t, f.SetFlag("sync-retry-limit", "5"))
272313
assert.Equal(t, int64(5), f.spec.SyncPolicy.Retry.Limit)

docs/user-guide/commands/argocd_admin_app_generate-spec.md

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/user-guide/commands/argocd_app_add-source.md

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/user-guide/commands/argocd_app_create.md

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/user-guide/commands/argocd_app_set.md

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)