Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions cmd/util/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ func AddAppFlags(command *cobra.Command, opts *AppOptions) {
command.Flags().StringVar(&opts.project, "project", "", "Application project name")
command.Flags().StringVar(&opts.syncPolicy, "sync-policy", "", "Set the sync policy (one of: manual (aliases of manual: none), automated (aliases of automated: auto, automatic))")
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`")
command.Flags().BoolVar(&opts.autoPrune, "auto-prune", false, "Set automatic pruning when sync is automated")
command.Flags().BoolVar(&opts.selfHeal, "self-heal", false, "Set self healing when sync is automated")
command.Flags().BoolVar(&opts.allowEmpty, "allow-empty", false, "Set allow zero live resources when sync is automated")
command.Flags().BoolVar(&opts.autoPrune, "auto-prune", false, "Set automatic pruning for automated sync policy")
command.Flags().BoolVar(&opts.selfHeal, "self-heal", false, "Set self healing for automated sync policy")
command.Flags().BoolVar(&opts.allowEmpty, "allow-empty", false, "Set allow zero live resources for automated sync policy")
command.Flags().StringVar(&opts.namePrefix, "nameprefix", "", "Kustomize nameprefix")
command.Flags().StringVar(&opts.nameSuffix, "namesuffix", "", "Kustomize namesuffix")
command.Flags().StringVar(&opts.kustomizeVersion, "kustomize-version", "", "Kustomize version")
Expand Down Expand Up @@ -273,25 +273,26 @@ func SetAppSpecOptions(flags *pflag.FlagSet, spec *argoappv1.ApplicationSpec, ap
}
}
})
if flags.Changed("auto-prune") {
if spec.SyncPolicy == nil || !spec.SyncPolicy.IsAutomatedSyncEnabled() {
log.Fatal("Cannot set --auto-prune: application not configured with automatic sync")

if flags.Changed("auto-prune") || flags.Changed("self-heal") || flags.Changed("allow-empty") {
if spec.SyncPolicy == nil {
spec.SyncPolicy = &argoappv1.SyncPolicy{}
}
spec.SyncPolicy.Automated.Prune = appOpts.autoPrune
}
if flags.Changed("self-heal") {
if spec.SyncPolicy == nil || !spec.SyncPolicy.IsAutomatedSyncEnabled() {
log.Fatal("Cannot set --self-heal: application not configured with automatic sync")
if spec.SyncPolicy.Automated == nil {
disabled := false
spec.SyncPolicy.Automated = &argoappv1.SyncPolicyAutomated{Enabled: &disabled}
}
spec.SyncPolicy.Automated.SelfHeal = appOpts.selfHeal
}
if flags.Changed("allow-empty") {
if spec.SyncPolicy == nil || !spec.SyncPolicy.IsAutomatedSyncEnabled() {
log.Fatal("Cannot set --allow-empty: application not configured with automatic sync")

if flags.Changed("auto-prune") {
spec.SyncPolicy.Automated.Prune = appOpts.autoPrune
}
if flags.Changed("self-heal") {
spec.SyncPolicy.Automated.SelfHeal = appOpts.selfHeal
}
if flags.Changed("allow-empty") {
spec.SyncPolicy.Automated.AllowEmpty = appOpts.allowEmpty
}
spec.SyncPolicy.Automated.AllowEmpty = appOpts.allowEmpty
}

return visited
}

Expand Down
30 changes: 30 additions & 0 deletions cmd/util/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,36 @@ func Test_setAppSpecOptions(t *testing.T) {
require.NoError(t, f.SetFlag("sync-option", "!a=1"))
assert.Nil(t, f.spec.SyncPolicy)
})
t.Run("AutoPruneFlag", func(t *testing.T) {
f.spec.SyncPolicy = nil
require.NoError(t, f.SetFlag("auto-prune", "true"))
require.NotNil(t, f.spec.SyncPolicy)
require.NotNil(t, f.spec.SyncPolicy.Automated)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing an assert to validate that spec.SyncPolicy.Automated.Enabled is false`.

  • It should be validated that if enabled: true (or not nil), then setting the flag will keep enabled to true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the current app_test.go, the logic causes one test to affect another.

I added a Reset() method to prevent them from interfering with one another.

assert.True(t, f.spec.SyncPolicy.Automated.Prune)

require.NoError(t, f.SetFlag("auto-prune", "false"))
assert.False(t, f.spec.SyncPolicy.Automated.Prune)
})
t.Run("SelfHealFlag", func(t *testing.T) {
f.spec.SyncPolicy = nil
require.NoError(t, f.SetFlag("self-heal", "true"))
require.NotNil(t, f.spec.SyncPolicy)
require.NotNil(t, f.spec.SyncPolicy.Automated)
assert.True(t, f.spec.SyncPolicy.Automated.SelfHeal)

require.NoError(t, f.SetFlag("self-heal", "false"))
assert.False(t, f.spec.SyncPolicy.Automated.SelfHeal)
})
t.Run("AllowEmptyFlag", func(t *testing.T) {
f.spec.SyncPolicy = nil
require.NoError(t, f.SetFlag("allow-empty", "true"))
require.NotNil(t, f.spec.SyncPolicy)
require.NotNil(t, f.spec.SyncPolicy.Automated)
assert.True(t, f.spec.SyncPolicy.Automated.AllowEmpty)

require.NoError(t, f.SetFlag("allow-empty", "false"))
assert.False(t, f.spec.SyncPolicy.Automated.AllowEmpty)
})
t.Run("RetryLimit", func(t *testing.T) {
require.NoError(t, f.SetFlag("sync-retry-limit", "5"))
assert.Equal(t, int64(5), f.spec.SyncPolicy.Retry.Limit)
Expand Down
6 changes: 3 additions & 3 deletions docs/user-guide/commands/argocd_admin_app_generate-spec.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/user-guide/commands/argocd_app_add-source.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/user-guide/commands/argocd_app_create.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/user-guide/commands/argocd_app_set.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading