Skip to content

Commit

Permalink
fix(controller): Only set global parameters after workflow validation…
Browse files Browse the repository at this point in the history
… succeeded to avoid panics (#5477)

Signed-off-by: terrytangyuan <terrytangyuan@gmail.com>
  • Loading branch information
terrytangyuan authored Mar 22, 2021
1 parent 5bd7ce8 commit 513756e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
4 changes: 2 additions & 2 deletions workflow/controller/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,6 @@ func (woc *wfOperationCtx) operate(ctx context.Context) {
woc.preExecutionNodePhases[node.ID] = node.Phase
}

woc.setGlobalParameters(woc.execWf.Spec.Arguments)

// Perform one-time workflow validation
if woc.wf.Status.Phase == wfv1.WorkflowUnknown {
woc.markWorkflowRunning(ctx)
Expand All @@ -263,6 +261,7 @@ func (woc *wfOperationCtx) operate(ctx context.Context) {
woc.markWorkflowFailed(ctx, msg)
return
}
woc.setGlobalParameters(woc.execWf.Spec.Arguments)
// If we received conditions during validation (such as SpecWarnings), add them to the Workflow object
if len(*wfConditions) > 0 {
woc.wf.Status.Conditions.JoinConditions(wfConditions)
Expand All @@ -285,6 +284,7 @@ func (woc *wfOperationCtx) operate(ctx context.Context) {
}
woc.wf.Status.EstimatedDuration = woc.estimateWorkflowDuration()
} else {
woc.setGlobalParameters(woc.execWf.Spec.Arguments)
woc.workflowDeadline = woc.getWorkflowDeadline()
err := woc.podReconciliation(ctx)
if err == nil {
Expand Down
38 changes: 38 additions & 0 deletions workflow/controller/operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3825,6 +3825,44 @@ func TestResolvePlaceholdersInGlobalVariables(t *testing.T) {
assert.Equal(t, "testServiceAccountName", serviceAccountNameValue.String())
}

var unsuppliedArgValue = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
name: wf-with-unsupplied-param-
spec:
arguments:
parameters:
- name: missing
entrypoint: whalesay
templates:
- arguments: {}
container:
args:
- hello world
command:
- cowsay
image: docker/whalesay:latest
name: ""
resources: {}
inputs: {}
metadata: {}
name: whalesay
outputs: {}
`

func TestUnsuppliedArgValue(t *testing.T) {
wf := unmarshalWF(unsuppliedArgValue)
cancel, controller := newController(wf)
defer cancel()

ctx := context.Background()
woc := newWorkflowOperationCtx(wf, controller)
woc.operate(ctx)
assert.Equal(t, woc.wf.Status.Conditions[0].Status, metav1.ConditionStatus("True"))
assert.Equal(t, woc.wf.Status.Message, "invalid spec: spec.arguments.missing.value is required")
}

var maxDurationOnErroredFirstNode = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
Expand Down
29 changes: 29 additions & 0 deletions workflow/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,38 @@ spec:
args: ["Global 1: {{workflow.parameters.message1}} Input 1: {{inputs.parameters.message1}} Input 2/Steps Input 1/Global 1: {{inputs.parameters.message2}} Input 3/Global 2: {{inputs.parameters.message3}} Input4/Steps Input 2 internal/Global 1: {{inputs.parameters.message4}}"]
`

var unsuppliedArgValue = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: step-with-unsupplied-param-
spec:
arguments:
parameters:
- name: missing
entrypoint: whalesay
templates:
- arguments: {}
container:
args:
- hello world
command:
- cowsay
image: docker/whalesay:latest
name: ""
resources: {}
inputs: {}
metadata: {}
name: whalesay
outputs: {}
`

func TestGlobalParam(t *testing.T) {
_, err := validate(globalParam)
assert.NoError(t, err)

_, err = validate(unsuppliedArgValue)
assert.EqualError(t, err, "spec.arguments.missing.value is required")
}

var invalidTemplateNames = `
Expand Down

0 comments on commit 513756e

Please sign in to comment.