Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Expose validation webhook warnings in Argo CD UI #545

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions pkg/sync/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ const (
OperationFailed OperationPhase = "Failed"
OperationError OperationPhase = "Error"
OperationSucceeded OperationPhase = "Succeeded"
OperationWarning OperationPhase = "Warning"
)

func (os OperationPhase) Completed() bool {
switch os {
case OperationFailed, OperationError, OperationSucceeded:
case OperationFailed, OperationError, OperationSucceeded, OperationWarning:
return true
}
return false
Expand All @@ -80,14 +81,18 @@ func (os OperationPhase) Successful() bool {
func (os OperationPhase) Failed() bool {
return os == OperationFailed
}
func (os OperationPhase) Warning() bool {
return os == OperationWarning
}

type ResultCode string

const (
ResultCodeSynced ResultCode = "Synced"
ResultCodeSyncFailed ResultCode = "SyncFailed"
ResultCodePruned ResultCode = "Pruned"
ResultCodePruneSkipped ResultCode = "PruneSkipped"
ResultCodeSynced ResultCode = "Synced"
ResultCodeSyncFailed ResultCode = "SyncFailed"
ResultCodePruned ResultCode = "Pruned"
ResultCodePruneSkipped ResultCode = "PruneSkipped"
ResultCodeSyncedWithWarning ResultCode = "SyncedWithWarning"
)

type HookType string
Expand Down
37 changes: 29 additions & 8 deletions pkg/sync/sync_context.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sync

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -383,7 +384,7 @@ func (sc *syncContext) setRunningPhase(tasks []*syncTask, isPendingDeletion bool
}
}

// sync has performs the actual apply or hook based sync
// sync performs the actual apply or hook based sync
func (sc *syncContext) Sync() {
sc.log.WithValues("skipHooks", sc.skipHooks, "started", sc.started()).Info("Syncing")
tasks, ok := sc.getSyncTasks()
Expand All @@ -397,7 +398,7 @@ func (sc *syncContext) Sync() {
} else {
// Perform a `kubectl apply --dry-run` against all the manifests. This will detect most (but
// not all) validation issues with the user's manifests (e.g. will detect syntax issues, but
// will not not detect if they are mutating immutable fields). If anything fails, we will refuse
// will not detect if they are mutating immutable fields). If anything fails, we will refuse
// to perform the sync. we only wish to do this once per operation, performing additional dry-runs
// is harmless, but redundant. The indicator we use to detect if we have already performed
// the dry-run for this operation, is if the resource or hook list is empty.
Expand Down Expand Up @@ -536,6 +537,14 @@ func (sc *syncContext) Sync() {
} else {
sc.setRunningPhase(remainingTasks, false)
}
case warning:
if remainingTasks.Len() == 0 {
// delete all completed hooks which have appropriate delete policy
sc.deleteHooks(hooksPendingDeletionSuccessful)
sc.setOperationPhase(common.OperationWarning, "synced with warning")
} else {
sc.setRunningPhase(remainingTasks, false)
}
default:
sc.setRunningPhase(tasks.Filter(func(task *syncTask) bool {
return task.deleteOnPhaseCompletion()
Expand Down Expand Up @@ -908,7 +917,9 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, force, validate bool) (c
if dryRun {
dryRunStrategy = cmdutil.DryRunClient
}

// Warnings handler
warn := bytes.NewBuffer(nil)
rest.SetDefaultWarningHandler(rest.NewWarningWriter(warn, rest.WarningWriterOptions{}))
var err error
var message string
shouldReplace := sc.replace || resourceutil.HasAnnotationOption(t.targetObj, common.AnnotationSyncOptions, common.SyncOptionReplace)
Expand Down Expand Up @@ -945,6 +956,9 @@ func (sc *syncContext) applyObject(t *syncTask, dryRun, force, validate bool) (c
sc.log.Error(err, fmt.Sprintf("failed to ensure that CRD %s is ready", crdName))
}
}
if warn.Len() != 0 {
return common.ResultCodeSyncedWithWarning, warn.String()
}
return common.ResultCodeSynced, message
}

Expand Down Expand Up @@ -1069,17 +1083,19 @@ func (sc *syncContext) getResourceIf(task *syncTask, verb string) (dynamic.Resou
}

var operationPhases = map[common.ResultCode]common.OperationPhase{
common.ResultCodeSynced: common.OperationRunning,
common.ResultCodeSyncFailed: common.OperationFailed,
common.ResultCodePruned: common.OperationSucceeded,
common.ResultCodePruneSkipped: common.OperationSucceeded,
common.ResultCodeSynced: common.OperationRunning,
common.ResultCodeSyncFailed: common.OperationFailed,
common.ResultCodePruned: common.OperationSucceeded,
common.ResultCodePruneSkipped: common.OperationSucceeded,
common.ResultCodeSyncedWithWarning: common.OperationWarning,
}

// tri-state
type runState int

const (
successful runState = iota
warning
pending
failed
)
Expand Down Expand Up @@ -1192,6 +1208,11 @@ func (sc *syncContext) processCreateTasks(state runState, tasks syncTasks, dryRu
logCtx.WithValues("message", message).Info("Apply failed")
state = failed
}
if result == common.ResultCodeSyncedWithWarning {
state = warning
phase := operationPhases[result]
sc.setResourceResult(t, result, phase, message)
}
if !dryRun || sc.dryRun || result == common.ResultCodeSyncFailed {
phase := operationPhases[result]
// no resources are created in dry-run, so running phase means validation was
Expand Down Expand Up @@ -1293,7 +1314,7 @@ func (s *stateSync) Wait() runState {
}
case successful:
switch result {
case pending, failed:
case warning, pending, failed:
res = result
}
}
Expand Down