diff --git a/workflow/controller/operator_workflow_template_ref_test.go b/workflow/controller/operator_workflow_template_ref_test.go index 805905712fcb7..50b3875196cea 100644 --- a/workflow/controller/operator_workflow_template_ref_test.go +++ b/workflow/controller/operator_workflow_template_ref_test.go @@ -634,3 +634,71 @@ func TestWorkflowTemplateWithDynamicRef(t *testing.T) { woc.operate(ctx) assert.Equal(t, wfv1.WorkflowSucceeded, woc.wf.Status.Phase) } + +const wfTemplateWithPodMetadata = ` +apiVersion: argoproj.io/v1alpha1 +kind: ClusterWorkflowTemplate +metadata: + name: workflow-template +spec: + entrypoint: whalesay-template + podMetadata: + labels: + workflow-template-label: hello + annotations: + all-pods-should-have-this: value + arguments: + parameters: + - name: message + value: hello world + + templates: + - name: whalesay-template + inputs: + parameters: + - name: message + container: + image: docker/whalesay + command: [cowsay] + args: ["{{inputs.parameters.message}}"]` + +const wfWithTemplateRef = ` +apiVersion: argoproj.io/v1alpha1 +kind: Workflow +metadata: + name: test-workflow + namespace: argo-workflows-system +spec: + podMetadata: + labels: + caller-label: hello + entrypoint: start + templates: + - name: start + steps: + - - name: hello + templateRef: + name: workflow-template + template: whalesay-template + clusterScope: true + arguments: + parameters: + - name: message + value: Hello Bug` + +func TestWorkflowTemplateWithPodMetedata(t *testing.T) { + cancel, controller := newController(wfv1.MustUnmarshalWorkflow(wfWithTemplateRef), wfv1.MustUnmarshalWorkflowTemplate(wfTemplateWithPodMetadata)) + defer cancel() + + ctx := context.Background() + woc := newWorkflowOperationCtx(wfv1.MustUnmarshalWorkflow(wfWithTemplateRef), controller) + woc.operate(ctx) + assert.Equal(t, wfv1.WorkflowRunning, woc.wf.Status.Phase) + pods, err := listPods(woc) + assert.NoError(t, err) + assert.True(t, len(pods.Items) > 0, "pod was not created successfully") + pod := pods.Items[0] + assert.Contains(t, pod.Labels, "caller-label") + assert.Contains(t, pod.Labels, "workflow-template-label") + assert.Contains(t, pod.Annotations, "all-pods-should-have-this") +} diff --git a/workflow/templateresolution/context.go b/workflow/templateresolution/context.go index 3c71689c00029..8921da7258168 100644 --- a/workflow/templateresolution/context.go +++ b/workflow/templateresolution/context.go @@ -104,6 +104,10 @@ func (ctx *Context) GetTemplateByName(name string) (*wfv1.Template, error) { ctx.log.Debugf("Getting the template by name: %s", name) tmpl := ctx.tmplBase.GetTemplateByName(name) + + podMetadata := ctx.tmplBase.GetPodMetadata() + ctx.addPodMetadata(podMetadata, tmpl) + if tmpl == nil { return nil, errors.Errorf(errors.CodeNotFound, "template %s not found", name) } @@ -138,22 +142,8 @@ func (ctx *Context) GetTemplateFromRef(tmplRef *wfv1.TemplateRef) (*wfv1.Templat template = wftmpl.GetTemplateByName(tmplRef.Template) - // add workflow template level pod annotations and labels to template podMetadata := wftmpl.GetPodMetadata() - if podMetadata != nil { - if template.Metadata.Annotations == nil { - template.Metadata.Annotations = make(map[string]string) - } - for k, v := range podMetadata.Annotations { - template.Metadata.Annotations[k] = v - } - if template.Metadata.Labels == nil { - template.Metadata.Labels = make(map[string]string) - } - for k, v := range podMetadata.Labels { - template.Metadata.Labels[k] = v - } - } + ctx.addPodMetadata(podMetadata, template) if template == nil { return nil, errors.Errorf(errors.CodeNotFound, "template %s not found in workflow template %s", tmplRef.Template, tmplRef.Name) @@ -285,3 +275,21 @@ func (ctx *Context) WithClusterWorkflowTemplate(name string) (*Context, error) { } return ctx.WithTemplateBase(cwftmpl), nil } + +// addPodMetadata add podMetadata in workflow template level to template +func (ctx *Context) addPodMetadata(podMetadata *wfv1.Metadata, tmpl *wfv1.Template) { + if podMetadata != nil { + if tmpl.Metadata.Annotations == nil { + tmpl.Metadata.Annotations = make(map[string]string) + } + for k, v := range podMetadata.Annotations { + tmpl.Metadata.Annotations[k] = v + } + if tmpl.Metadata.Labels == nil { + tmpl.Metadata.Labels = make(map[string]string) + } + for k, v := range podMetadata.Labels { + tmpl.Metadata.Labels[k] = v + } + } +}