Skip to content

Commit

Permalink
fix: add it to gettemplatename and add test.
Browse files Browse the repository at this point in the history
Signed-off-by: shuangkun <[email protected]>
  • Loading branch information
shuangkun committed Jan 5, 2025
1 parent 20cab88 commit 83aa9d7
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 15 deletions.
68 changes: 68 additions & 0 deletions workflow/controller/operator_workflow_template_ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 697 in workflow/controller/operator_workflow_template_ref_test.go

View workflow job for this annotation

GitHub Actions / Lint

require-error: for error assertions use require (testifylint)
assert.NoError(t, err)

Check failure on line 698 in workflow/controller/operator_workflow_template_ref_test.go

View workflow job for this annotation

GitHub Actions / Lint

negative-positive: use assert.Positive (testifylint)
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")
}
38 changes: 23 additions & 15 deletions workflow/templateresolution/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
}

0 comments on commit 83aa9d7

Please sign in to comment.