Skip to content

Commit

Permalink
feat: exposed sync retry options via cli for app create (argoproj#5638)
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Agarwal <[email protected]>
  • Loading branch information
Shubhama19 authored Mar 1, 2021
1 parent cbc04c7 commit c6d3728
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 1 deletion.
32 changes: 32 additions & 0 deletions cmd/util/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"net/url"
"os"
"strings"
"time"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"

"github.com/argoproj/argo-cd/common"
"github.com/argoproj/argo-cd/pkg/apis/application"
Expand Down Expand Up @@ -62,6 +64,10 @@ type AppOptions struct {
Validate bool
directoryExclude string
directoryInclude string
retryLimit int64
retryBackoffDuration time.Duration
retryBackoffMaxDuration time.Duration
retryBackoffFactor int64
}

func AddAppFlags(command *cobra.Command, opts *AppOptions) {
Expand Down Expand Up @@ -105,6 +111,10 @@ func AddAppFlags(command *cobra.Command, opts *AppOptions) {
command.Flags().StringArrayVar(&opts.kustomizeCommonAnnotations, "kustomize-common-annotation", []string{}, "Set common labels in Kustomize")
command.Flags().StringVar(&opts.directoryExclude, "directory-exclude", "", "Set glob expression used to exclude files from application source path")
command.Flags().StringVar(&opts.directoryInclude, "directory-include", "", "Set glob expression used to include files from application source path")
command.Flags().Int64Var(&opts.retryLimit, "retry-limit", 0, "Max number of allowed sync retries")
command.Flags().DurationVar(&opts.retryBackoffDuration, "retry-backoff-duration", common.DefaultSyncRetryDuration, "Retry backoff base duration. Input needs to be a duration (e.g. 2m, 1h)")
command.Flags().DurationVar(&opts.retryBackoffMaxDuration, "retry-backoff-max-duration", common.DefaultSyncRetryMaxDuration, "Max retry backoff duration. Input needs to be a duration (e.g. 2m, 1h)")
command.Flags().Int64Var(&opts.retryBackoffFactor, "retry-backoff-factor", common.DefaultSyncRetryFactor, "Factor multiplies the base duration after each failed retry")
}

func SetAppSpecOptions(flags *pflag.FlagSet, spec *argoappv1.ApplicationSpec, appOpts *AppOptions) int {
Expand Down Expand Up @@ -238,6 +248,28 @@ func SetAppSpecOptions(flags *pflag.FlagSet, spec *argoappv1.ApplicationSpec, ap
if spec.SyncPolicy.IsZero() {
spec.SyncPolicy = nil
}
case "retry-limit":
if appOpts.retryLimit > 0 {
if spec.SyncPolicy == nil {
spec.SyncPolicy = &argoappv1.SyncPolicy{}
}
spec.SyncPolicy.Retry = &argoappv1.RetryStrategy{
Limit: appOpts.retryLimit,
Backoff: &argoappv1.Backoff{
Duration: appOpts.retryBackoffDuration.String(),
MaxDuration: appOpts.retryBackoffMaxDuration.String(),
Factor: pointer.Int64Ptr(appOpts.retryBackoffFactor),
},
}
} else if appOpts.retryLimit == 0 {
if spec.SyncPolicy.IsZero() {
spec.SyncPolicy = nil
} else {
spec.SyncPolicy.Retry = nil
}
} else {
log.Fatalf("Invalid retry-limit [%d]", appOpts.retryLimit)
}
}
})
if flags.Changed("auto-prune") {
Expand Down
7 changes: 7 additions & 0 deletions cmd/util/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,11 @@ func Test_setAppSpecOptions(t *testing.T) {
assert.NoError(t, f.SetFlag("sync-option", "!a=1"))
assert.Nil(t, f.spec.SyncPolicy)
})
t.Run("RetryLimit", func(t *testing.T) {
assert.NoError(t, f.SetFlag("retry-limit", "5"))
assert.True(t, f.spec.SyncPolicy.Retry.Limit == 5)

assert.NoError(t, f.SetFlag("retry-limit", "0"))
assert.Nil(t, f.spec.SyncPolicy)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ argocd-util config app APPNAME [flags]
--project string Application project name
--release-name string Helm release-name
--repo string Repository URL, ignored if a file is set
--retry-backoff-duration duration Retry backoff base duration. Input needs to be a duration (e.g. 2m, 1h) (default 5s)
--retry-backoff-factor int Factor multiplies the base duration after each failed retry (default 2)
--retry-backoff-max-duration duration Max retry backoff duration. Input needs to be a duration (e.g. 2m, 1h) (default 3m0s)
--retry-limit int Max number of allowed sync retries
--revision string The tracking source branch, tag, commit or Helm chart version the application will sync to
--revision-history-limit int How many items to keep in revision history (default 10)
--self-heal Set self healing when sync is automated
Expand Down
4 changes: 4 additions & 0 deletions docs/user-guide/commands/argocd_app_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ argocd app create APPNAME [flags]
--project string Application project name
--release-name string Helm release-name
--repo string Repository URL, ignored if a file is set
--retry-backoff-duration duration Retry backoff base duration. Input needs to be a duration (e.g. 2m, 1h) (default 5s)
--retry-backoff-factor int Factor multiplies the base duration after each failed retry (default 2)
--retry-backoff-max-duration duration Max retry backoff duration. Input needs to be a duration (e.g. 2m, 1h) (default 3m0s)
--retry-limit int Max number of allowed sync retries
--revision string The tracking source branch, tag, commit or Helm chart version the application will sync to
--revision-history-limit int How many items to keep in revision history (default 10)
--self-heal Set self healing when sync is automated
Expand Down
4 changes: 4 additions & 0 deletions docs/user-guide/commands/argocd_app_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ argocd app set APPNAME [flags]
--project string Application project name
--release-name string Helm release-name
--repo string Repository URL, ignored if a file is set
--retry-backoff-duration duration Retry backoff base duration. Input needs to be a duration (e.g. 2m, 1h) (default 5s)
--retry-backoff-factor int Factor multiplies the base duration after each failed retry (default 2)
--retry-backoff-max-duration duration Max retry backoff duration. Input needs to be a duration (e.g. 2m, 1h) (default 3m0s)
--retry-limit int Max number of allowed sync retries
--revision string The tracking source branch, tag, commit or Helm chart version the application will sync to
--revision-history-limit int How many items to keep in revision history (default 10)
--self-heal Set self healing when sync is automated
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/application/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ type SyncPolicy struct {
}

func (p *SyncPolicy) IsZero() bool {
return p == nil || (p.Automated == nil && len(p.SyncOptions) == 0)
return p == nil || (p.Automated == nil && len(p.SyncOptions) == 0 && p.Retry == nil)
}

type RetryStrategy struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/application/v1alpha1/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,7 @@ func TestSyncPolicy_IsZero(t *testing.T) {
assert.True(t, (&SyncPolicy{}).IsZero())
assert.False(t, (&SyncPolicy{Automated: &SyncPolicyAutomated{}}).IsZero())
assert.False(t, (&SyncPolicy{SyncOptions: SyncOptions{""}}).IsZero())
assert.False(t, (&SyncPolicy{Retry: &RetryStrategy{}}).IsZero())
}

func TestSyncOptions_HasOption(t *testing.T) {
Expand Down

0 comments on commit c6d3728

Please sign in to comment.