Skip to content

Commit

Permalink
fix: use execWf when setting PodMetadata (#6512)
Browse files Browse the repository at this point in the history
Signed-off-by: Ed Marks <edwardmarks@bulb.co.uk>
  • Loading branch information
ed-marks authored Aug 11, 2021
1 parent 333df2b commit dc4f0a1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
6 changes: 3 additions & 3 deletions workflow/controller/workflowpod.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,12 +696,12 @@ func isResourcesSpecified(ctr *apiv1.Container) bool {

// addMetadata applies metadata specified in the template
func (woc *wfOperationCtx) addMetadata(pod *apiv1.Pod, tmpl *wfv1.Template) {
if woc.wf.Spec.PodMetadata != nil {
if woc.execWf.Spec.PodMetadata != nil {
// add workflow-level pod annotations and labels
for k, v := range woc.wf.Spec.PodMetadata.Annotations {
for k, v := range woc.execWf.Spec.PodMetadata.Annotations {
pod.ObjectMeta.Annotations[k] = v
}
for k, v := range woc.wf.Spec.PodMetadata.Labels {
for k, v := range woc.execWf.Spec.PodMetadata.Labels {
pod.ObjectMeta.Labels[k] = v
}
}
Expand Down
56 changes: 56 additions & 0 deletions workflow/controller/workflowpod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1513,3 +1513,59 @@ func TestGetDeadline(t *testing.T) {
deadline, _ = getPodDeadline(pod)
assert.Equal(t, executionDeadline.Format(time.RFC3339), deadline.Format(time.RFC3339))
}

func TestPodMetadataWithWorkflowDefaults(t *testing.T) {
cancel, controller := newController()
defer cancel()

wfDefaultAnnotations := make(map[string]string)
wfDefaultAnnotations["controller-level-pod-annotation"] = "annotation-value"
wfDefaultAnnotations["workflow-level-pod-annotation"] = "set-by-controller"
wfDefaultLabels := make(map[string]string)
wfDefaultLabels["controller-level-pod-label"] = "label-value"
wfDefaultLabels["workflow-level-pod-label"] = "set-by-controller"
controller.Config.WorkflowDefaults = &wfv1.Workflow{
Spec: wfv1.WorkflowSpec{
PodMetadata: &wfv1.Metadata{
Annotations: wfDefaultAnnotations,
Labels: wfDefaultLabels,
},
},
}

wf := wfv1.MustUnmarshalWorkflow(helloWorldWf)
ctx := context.Background()
woc := newWorkflowOperationCtx(wf, controller)
err := woc.setExecWorkflow(ctx)
assert.NoError(t, err)
mainCtr := woc.execWf.Spec.Templates[0].Container
pod, _ := woc.createWorkflowPod(ctx, wf.Name, []apiv1.Container{*mainCtr}, &wf.Spec.Templates[0], &createWorkflowPodOpts{})
assert.Equal(t, "annotation-value", pod.ObjectMeta.Annotations["controller-level-pod-annotation"])
assert.Equal(t, "set-by-controller", pod.ObjectMeta.Annotations["workflow-level-pod-annotation"])
assert.Equal(t, "label-value", pod.ObjectMeta.Labels["controller-level-pod-label"])
assert.Equal(t, "set-by-controller", pod.ObjectMeta.Labels["workflow-level-pod-label"])
cancel() // need to cancel to spin up pods with the same name

cancel, controller = newController()
defer cancel()
controller.Config.WorkflowDefaults = &wfv1.Workflow{
Spec: wfv1.WorkflowSpec{
PodMetadata: &wfv1.Metadata{
Annotations: wfDefaultAnnotations,
Labels: wfDefaultLabels,
},
},
}
wf = wfv1.MustUnmarshalWorkflow(wfWithPodMetadata)
ctx = context.Background()
woc = newWorkflowOperationCtx(wf, controller)
err = woc.setExecWorkflow(ctx)
assert.NoError(t, err)
mainCtr = woc.execWf.Spec.Templates[0].Container
pod, _ = woc.createWorkflowPod(ctx, wf.Name, []apiv1.Container{*mainCtr}, &wf.Spec.Templates[0], &createWorkflowPodOpts{})
assert.Equal(t, "foo", pod.ObjectMeta.Annotations["workflow-level-pod-annotation"])
assert.Equal(t, "bar", pod.ObjectMeta.Labels["workflow-level-pod-label"])
assert.Equal(t, "annotation-value", pod.ObjectMeta.Annotations["controller-level-pod-annotation"])
assert.Equal(t, "label-value", pod.ObjectMeta.Labels["controller-level-pod-label"])
cancel()
}

0 comments on commit dc4f0a1

Please sign in to comment.