Skip to content

Commit

Permalink
fix: Allow hooks to be specified in workflowDefaults (#11214)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joibel authored and terrytangyuan committed Jul 19, 2023
1 parent 1109ab4 commit 1a3f17f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions workflow/util/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ func MergeTo(patch, target *wfv1.Workflow) error {
return nil
}

patchHooks := patch.Spec.Hooks
patch.Spec.Hooks = nil
patchWfBytes, err := json.Marshal(patch)
if err != nil {
return err
Expand All @@ -36,6 +38,13 @@ func MergeTo(patch, target *wfv1.Workflow) error {
if err != nil {
return err
}

for name, hook := range patchHooks {
// If the patch hook doesn't exist in target
if _, ok := target.Spec.Hooks[name]; !ok {
target.Spec.Hooks[name] = hook
}
}
return nil
}

Expand Down
37 changes: 37 additions & 0 deletions workflow/util/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,40 @@ func TestJoinWorkflowMetaData(t *testing.T) {
assert.Equal("wf", wf2.Annotations["testAnnotation"])
})
}

var baseHookWF = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
generateName: workflow-template-hello-world-
spec:
hooks:
foo:
template: a
expression: workflow.status == "Pending"
`

var patchHookWF = `
apiVersion: argoproj.io/v1alpha1
kind: Workflow
spec:
hooks:
foo:
template: c
expression: workflow.status == "Pending"
bar:
template: b
expression: workflow.status == "Pending"
`

// Ensure hook bar ends up in result, but foo is unchanged
func TestMergeHooks(t *testing.T) {
patchHookWf := wfv1.MustUnmarshalWorkflow(patchHookWF)
targetHookWf := wfv1.MustUnmarshalWorkflow(baseHookWF)

err := MergeTo(patchHookWf, targetHookWf)
assert.NoError(t, err)
assert.Equal(t, 2, len(targetHookWf.Spec.Hooks))
assert.Equal(t, "a", targetHookWf.Spec.Hooks[`foo`].Template)
assert.Equal(t, "b", targetHookWf.Spec.Hooks[`bar`].Template)
}

0 comments on commit 1a3f17f

Please sign in to comment.