Skip to content

Commit

Permalink
Fail app sync if prune flag is required (#276)
Browse files Browse the repository at this point in the history
* Add status field to resource details

* Update generated code

* Set up const message responses

* Check number of resources requiring pruning

* Fix imports

* Use string, thanks @alexmt

* Update generated code
  • Loading branch information
merenbach authored Jun 12, 2018
1 parent 9fa622d commit df0e2e4
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 140 deletions.
22 changes: 18 additions & 4 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,11 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
errors.CheckError(err)
status, err := waitUntilOperationCompleted(appIf, appName)
errors.CheckError(err)
printOperationResult(appName, status)
if !status.Phase.Successful() {
err = printOperationResult(appName, status)
if err != nil {
log.Fatal(err)
}
if !status.Phase.Successful() && !dryRun {
os.Exit(1)
}
},
Expand Down Expand Up @@ -793,7 +796,10 @@ func NewApplicationRollbackCommand(clientOpts *argocdclient.ClientOptions) *cobr

status, err := waitUntilOperationCompleted(appIf, appName)
errors.CheckError(err)
printOperationResult(appName, status)
err = printOperationResult(appName, status)
if err != nil {
log.Fatal(err)
}
if !status.Phase.Successful() {
os.Exit(1)
}
Expand All @@ -805,7 +811,7 @@ func NewApplicationRollbackCommand(clientOpts *argocdclient.ClientOptions) *cobr

const printOpFmtStr = "%-20s%s\n"

func printOperationResult(appName string, opState *argoappv1.OperationState) {
func printOperationResult(appName string, opState *argoappv1.OperationState) error {
fmt.Printf(printOpFmtStr, "Application:", appName)
var syncRes *argoappv1.SyncOperationResult
if opState.SyncResult != nil {
Expand All @@ -823,11 +829,19 @@ func printOperationResult(appName string, opState *argoappv1.OperationState) {
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Printf("\n")
fmt.Fprintf(w, "KIND\tNAME\tMESSAGE\n")
pruningRequired := 0
for _, resDetails := range syncRes.Resources {
fmt.Fprintf(w, "%s\t%s\t%s\n", resDetails.Kind, resDetails.Name, resDetails.Message)
if resDetails.Status == argoappv1.ResourceDetailsPruningRequired {
pruningRequired++
}
}
_ = w.Flush()
if pruningRequired > 0 {
return fmt.Errorf("Some resources (%d) require pruning", pruningRequired)
}
}
return nil
}

// NewApplicationManifestsCommand returns a new instance of an `argocd app manifests` command
Expand Down
6 changes: 6 additions & 0 deletions controller/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,25 +454,31 @@ func syncObject(config *rest.Config, namespace string, targetObj, liveObj *unstr
if prune {
if dryRun {
resDetails.Message = "pruned (dry run)"
resDetails.Status = v1alpha1.ResourceDetailsSyncedAndPruned
} else {
err := kubeutil.DeleteResource(config, liveObj, namespace)
if err != nil {
resDetails.Message = err.Error()
resDetails.Status = v1alpha1.ResourceDetailsSyncFailed
successful = false
} else {
resDetails.Message = "pruned"
resDetails.Status = v1alpha1.ResourceDetailsSyncedAndPruned
}
}
} else {
resDetails.Message = "ignored (requires pruning)"
resDetails.Status = v1alpha1.ResourceDetailsPruningRequired
}
} else {
message, err := kube.ApplyResource(config, targetObj, namespace, dryRun)
if err != nil {
resDetails.Message = err.Error()
resDetails.Status = v1alpha1.ResourceDetailsSyncFailed
successful = false
} else {
resDetails.Message = message
resDetails.Status = v1alpha1.ResourceDetailsSynced
}
}
return resDetails, successful
Expand Down
Loading

0 comments on commit df0e2e4

Please sign in to comment.