Skip to content

Commit

Permalink
feat: app wait with --degraded (#10139)
Browse files Browse the repository at this point in the history
* feat: app wait with --degraded

Signed-off-by: May Zhang <[email protected]>

* feat: adding doc change

Signed-off-by: May Zhang <[email protected]>
  • Loading branch information
mayzhang2000 authored Aug 5, 2022
1 parent e93397e commit 5da0c2e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
18 changes: 17 additions & 1 deletion cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type watchOpts struct {
health bool
operation bool
suspended bool
degraded bool
}

// NewApplicationCreateCommand returns a new instance of an `argocd app create` command
Expand Down Expand Up @@ -1361,6 +1362,7 @@ func NewApplicationWaitCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
command.Flags().BoolVar(&watch.sync, "sync", false, "Wait for sync")
command.Flags().BoolVar(&watch.health, "health", false, "Wait for health")
command.Flags().BoolVar(&watch.suspended, "suspended", false, "Wait for suspended")
command.Flags().BoolVar(&watch.degraded, "degraded", false, "Wait for degraded")
command.Flags().StringVarP(&selector, "selector", "l", "", "Wait for apps by label")
command.Flags().StringArrayVar(&resources, "resource", []string{}, fmt.Sprintf("Sync only specific resources as GROUP%sKIND%sNAME. Fields may be blank. This option may be specified repeatedly", resourceFieldDelimiter, resourceFieldDelimiter))
command.Flags().BoolVar(&watch.operation, "operation", false, "Wait for pending operations")
Expand Down Expand Up @@ -1738,13 +1740,27 @@ func groupResourceStates(app *argoappv1.Application, selectedResources []*argoap
// check if resource health, sync and operation statuses matches watch options
func checkResourceStatus(watch watchOpts, healthStatus string, syncStatus string, operationStatus *argoappv1.Operation) bool {
healthCheckPassed := true
if watch.suspended && watch.health {

if watch.suspended && watch.health && watch.degraded {
healthCheckPassed = healthStatus == string(health.HealthStatusHealthy) ||
healthStatus == string(health.HealthStatusSuspended) ||
healthStatus == string(health.HealthStatusDegraded)
} else if watch.suspended && watch.degraded {
healthCheckPassed = healthStatus == string(health.HealthStatusDegraded) ||
healthStatus == string(health.HealthStatusSuspended)
} else if watch.degraded && watch.health {
healthCheckPassed = healthStatus == string(health.HealthStatusHealthy) ||
healthStatus == string(health.HealthStatusDegraded)
//below are good
} else if watch.suspended && watch.health {
healthCheckPassed = healthStatus == string(health.HealthStatusHealthy) ||
healthStatus == string(health.HealthStatusSuspended)
} else if watch.suspended {
healthCheckPassed = healthStatus == string(health.HealthStatusSuspended)
} else if watch.health {
healthCheckPassed = healthStatus == string(health.HealthStatusHealthy)
} else if watch.degraded {
healthCheckPassed = healthStatus == string(health.HealthStatusDegraded)
}

synced := !watch.sync || syncStatus == string(argoappv1.SyncStatusCodeSynced)
Expand Down
32 changes: 32 additions & 0 deletions cmd/argocd/commands/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,22 @@ func TestMergeWitoutUpdate(t *testing.T) {
}

func TestCheckResourceStatus(t *testing.T) {
t.Run("Degraded, Suspended and health status passed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: true,
health: true,
degraded: true,
}, string(health.HealthStatusHealthy), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.True(t, res)
})
t.Run("Degraded, Suspended and health status failed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: true,
health: true,
degraded: true,
}, string(health.HealthStatusProgressing), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.False(t, res)
})
t.Run("Suspended and health status passed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: true,
Expand Down Expand Up @@ -1153,6 +1169,22 @@ func TestCheckResourceStatus(t *testing.T) {
res := checkResourceStatus(watchOpts{}, string(health.HealthStatusProgressing), string(v1alpha1.SyncStatusCodeOutOfSync), &v1alpha1.Operation{})
assert.True(t, res)
})
t.Run("Degraded passed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: false,
health: false,
degraded: true,
}, string(health.HealthStatusDegraded), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.True(t, res)
})
t.Run("Degraded failed", func(t *testing.T) {
res := checkResourceStatus(watchOpts{
suspended: false,
health: false,
degraded: true,
}, string(health.HealthStatusProgressing), string(v1alpha1.SyncStatusCodeSynced), &v1alpha1.Operation{})
assert.False(t, res)
})
}

func Test_hasAppChanged(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions docs/user-guide/commands/argocd_app_wait.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ argocd app wait [APPNAME.. | -l selector] [flags]
### Options

```
--degraded Wait for degraded
--health Wait for health
-h, --help help for wait
--operation Wait for pending operations
Expand Down

0 comments on commit 5da0c2e

Please sign in to comment.