From e64918adb65030b35509f77f73faf75324d4c527 Mon Sep 17 00:00:00 2001 From: Chitrang Patel Date: Mon, 3 Oct 2022 14:57:06 -0400 Subject: [PATCH] Propagated Parameters for Finally Tasks Propagated parameters was only called for regular pipeline tasks, not finally tasks. This was due to an oversight, and not a design choice. This PR extends the propagated paramsters usage to finally tasks now. It addresses issue https://github.com/tektoncd/pipeline/issues/5588 . --- ...ropagating_params_implicit_parameters.yaml | 9 + ...pagating_params_with_scope_precedence.yaml | 12 + ...ms_with_scope_precedence_default_only.yaml | 12 + pkg/reconciler/pipelinerun/resources/apply.go | 1 + .../pipelinerun/resources/apply_test.go | 373 ++++++++++++++++++ 5 files changed, 407 insertions(+) diff --git a/examples/v1beta1/pipelineruns/alpha/propagating_params_implicit_parameters.yaml b/examples/v1beta1/pipelineruns/alpha/propagating_params_implicit_parameters.yaml index 9cf03eea189..60560a82e44 100644 --- a/examples/v1beta1/pipelineruns/alpha/propagating_params_implicit_parameters.yaml +++ b/examples/v1beta1/pipelineruns/alpha/propagating_params_implicit_parameters.yaml @@ -16,3 +16,12 @@ spec: script: | #!/usr/bin/env bash echo "$(params.HELLO)" + finally: + - name: echo-hello-finally + taskSpec: + steps: + - name: echo + image: ubuntu + script: | + #!/usr/bin/env bash + echo "And Finally ... $(params.HELLO)" diff --git a/examples/v1beta1/pipelineruns/alpha/propagating_params_with_scope_precedence.yaml b/examples/v1beta1/pipelineruns/alpha/propagating_params_with_scope_precedence.yaml index 0791e27ad41..bcbe548bf4d 100644 --- a/examples/v1beta1/pipelineruns/alpha/propagating_params_with_scope_precedence.yaml +++ b/examples/v1beta1/pipelineruns/alpha/propagating_params_with_scope_precedence.yaml @@ -19,3 +19,15 @@ spec: script: | #!/usr/bin/env bash echo "$(params.HELLO)" + finally: + - name: echo-hello-finally + params: + - name: HELLO + value: "Finally Hello World!" + taskSpec: + steps: + - name: echo + image: ubuntu + script: | + #!/usr/bin/env bash + echo "And finally.. $(params.HELLO)" diff --git a/examples/v1beta1/pipelineruns/alpha/propagating_params_with_scope_precedence_default_only.yaml b/examples/v1beta1/pipelineruns/alpha/propagating_params_with_scope_precedence_default_only.yaml index 3d5af4a40c0..70e7c856e1b 100644 --- a/examples/v1beta1/pipelineruns/alpha/propagating_params_with_scope_precedence_default_only.yaml +++ b/examples/v1beta1/pipelineruns/alpha/propagating_params_with_scope_precedence_default_only.yaml @@ -19,3 +19,15 @@ spec: script: | #!/usr/bin/env bash echo "$(params.HELLO)" + finally: + - name: echo-hello-finally + taskSpec: + params: + - name: HELLO + default: "Default Hello World!" + steps: + - name: echo + image: ubuntu + script: | + #!/usr/bin/env bash + echo "And finally... $(params.HELLO)" diff --git a/pkg/reconciler/pipelinerun/resources/apply.go b/pkg/reconciler/pipelinerun/resources/apply.go index abdc00e3bb7..d1cd06d8e80 100644 --- a/pkg/reconciler/pipelinerun/resources/apply.go +++ b/pkg/reconciler/pipelinerun/resources/apply.go @@ -249,6 +249,7 @@ func ApplyReplacements(ctx context.Context, p *v1beta1.PipelineSpec, replacement if p.Finally[i].TaskRef != nil && p.Finally[i].TaskRef.Params != nil { p.Finally[i].TaskRef.Params = replaceParamValues(p.Finally[i].TaskRef.Params, replacements, arrayReplacements, objectReplacements) } + p.Finally[i], replacements, arrayReplacements, objectReplacements = propagateParams(ctx, p.Finally[i], replacements, arrayReplacements, objectReplacements) } return p diff --git a/pkg/reconciler/pipelinerun/resources/apply_test.go b/pkg/reconciler/pipelinerun/resources/apply_test.go index 1ea270f7c53..5b2a193b1d3 100644 --- a/pkg/reconciler/pipelinerun/resources/apply_test.go +++ b/pkg/reconciler/pipelinerun/resources/apply_test.go @@ -96,6 +96,36 @@ func TestApplyParameters(t *testing.T) { }}, }, alpha: true, + }, { + name: "parameter propagation string into finally task", + original: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Script: `#!/usr/bin/env bash\necho "$(params.HELLO)"`, + }}, + }, + }, + }}, + }, + params: []v1beta1.Param{{Name: "HELLO", Value: *v1beta1.NewStructuredValues("hello param!")}}, + expected: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Script: `#!/usr/bin/env bash\necho "hello param!"`, + }}, + }, + }, + }}, + }, + alpha: true, }, { name: "parameter propagation array no task or task default winner pipeline", original: v1beta1.PipelineSpec{ @@ -126,6 +156,36 @@ func TestApplyParameters(t *testing.T) { }}, }, alpha: true, + }, { + name: "parameter propagation array finally task", + original: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "$(params.HELLO[*])"}, + }}, + }, + }, + }}, + }, + params: []v1beta1.Param{{Name: "HELLO", Value: *v1beta1.NewStructuredValues("hello", "param", "!!!")}}, + expected: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "hello", "param", "!!!"}, + }}, + }, + }, + }}, + }, + alpha: true, }, { name: "parameter propagation object no task or task default winner pipeline", original: v1beta1.PipelineSpec{ @@ -156,6 +216,36 @@ func TestApplyParameters(t *testing.T) { }}, }, alpha: true, + }, { + name: "parameter propagation object finally task", + original: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "$(params.myObject.key1) $(params.myObject.key2)"}, + }}, + }, + }, + }}, + }, + params: []v1beta1.Param{{Name: "myObject", Value: *v1beta1.NewObject(map[string]string{"key1": "hello", "key2": "world!"})}}, + expected: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "hello world!"}, + }}, + }, + }, + }}, + }, + alpha: true, }, { name: "parameter propagation with task default but no task winner pipeline", original: v1beta1.PipelineSpec{ @@ -194,6 +284,44 @@ func TestApplyParameters(t *testing.T) { }}, }, alpha: true, + }, { + name: "parameter propagation with task scoping Finally task", + original: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "HELLO", + Default: v1beta1.NewStructuredValues("default param!"), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Script: `#!/usr/bin/env bash\necho "$(params.HELLO)"`, + }}, + }, + }, + }}, + }, + params: []v1beta1.Param{{Name: "HELLO", Value: *v1beta1.NewStructuredValues("pipeline param!")}}, + expected: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "HELLO", + Default: v1beta1.NewStructuredValues("default param!"), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Script: `#!/usr/bin/env bash\necho "pipeline param!"`, + }}, + }, + }, + }}, + }, + alpha: true, }, { name: "parameter propagation array with task default but no task winner pipeline", original: v1beta1.PipelineSpec{ @@ -232,6 +360,44 @@ func TestApplyParameters(t *testing.T) { }}, }, alpha: true, + }, { + name: "parameter propagation array with task scoping Finally task", + original: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "HELLO", + Default: v1beta1.NewStructuredValues("default", "param!"), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "$(params.HELLO)"}, + }}, + }, + }, + }}, + }, + params: []v1beta1.Param{{Name: "HELLO", Value: *v1beta1.NewStructuredValues("pipeline", "param!")}}, + expected: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "HELLO", + Default: v1beta1.NewStructuredValues("default", "param!"), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "pipeline", "param!"}, + }}, + }, + }, + }}, + }, + alpha: true, }, { name: "parameter propagation array with task default and task winner task", original: v1beta1.PipelineSpec{ @@ -276,6 +442,50 @@ func TestApplyParameters(t *testing.T) { }}, }, alpha: true, + }, { + name: "Finally task parameter propagation array with task default and task winner task", + original: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + Params: []v1beta1.Param{ + {Name: "HELLO", Value: *v1beta1.NewStructuredValues("task", "param!")}, + }, + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "HELLO", + Default: v1beta1.NewStructuredValues("default", "param!"), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "$(params.HELLO)"}, + }}, + }, + }, + }}, + }, + params: []v1beta1.Param{{Name: "HELLO", Value: *v1beta1.NewStructuredValues("pipeline", "param!")}}, + expected: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + Params: []v1beta1.Param{ + {Name: "HELLO", Value: *v1beta1.NewStructuredValues("task", "param!")}, + }, + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "HELLO", + Default: v1beta1.NewStructuredValues("default", "param!"), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "task", "param!"}, + }}, + }, + }, + }}, + }, + alpha: true, }, { name: "parameter propagation with task default and task winner task", original: v1beta1.PipelineSpec{ @@ -320,6 +530,50 @@ func TestApplyParameters(t *testing.T) { }}, }, alpha: true, + }, { + name: "Finally task parameter propagation with task default and task winner task", + original: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + Params: []v1beta1.Param{ + {Name: "HELLO", Value: *v1beta1.NewStructuredValues("task param!")}, + }, + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "HELLO", + Default: v1beta1.NewStructuredValues("default param!"), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Script: `#!/usr/bin/env bash\necho "$(params.HELLO)"`, + }}, + }, + }, + }}, + }, + params: []v1beta1.Param{{Name: "HELLO", Value: *v1beta1.NewStructuredValues("pipeline param!")}}, + expected: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + Params: []v1beta1.Param{ + {Name: "HELLO", Value: *v1beta1.NewStructuredValues("task param!")}, + }, + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "HELLO", + Default: v1beta1.NewStructuredValues("default param!"), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Script: `#!/usr/bin/env bash\necho "task param!"`, + }}, + }, + }, + }}, + }, + alpha: true, }, { name: "parameter propagation object with task default but no task winner pipeline", original: v1beta1.PipelineSpec{ @@ -375,6 +629,61 @@ func TestApplyParameters(t *testing.T) { }}, }, alpha: true, + }, { + name: "Finally task parameter propagation object with task default but no task winner pipeline", + original: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "myobject", + Properties: map[string]v1beta1.PropertySpec{ + "key1": {Type: "string"}, + "key2": {Type: "string"}, + }, + Default: v1beta1.NewObject(map[string]string{ + "key1": "default", + "key2": "param!", + }), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "$(params.myobject.key1) $(params.myobject.key2)"}, + }}, + }, + }, + }}, + }, + params: []v1beta1.Param{{Name: "myobject", Value: *v1beta1.NewObject(map[string]string{ + "key1": "pipeline", + "key2": "param!!", + })}}, + expected: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "myobject", + Properties: map[string]v1beta1.PropertySpec{ + "key1": {Type: "string"}, + "key2": {Type: "string"}, + }, + Default: v1beta1.NewObject(map[string]string{ + "key1": "default", + "key2": "param!", + }), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "pipeline param!!"}, + }}, + }, + }, + }}, + }, + alpha: true, }, { name: "parameter propagation object with task default and task winner task", original: v1beta1.PipelineSpec{ @@ -439,6 +748,70 @@ func TestApplyParameters(t *testing.T) { }}, }, alpha: true, + }, { + name: "Finally task parameter propagation object with task default and task winner task", + original: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + Params: []v1beta1.Param{ + {Name: "myobject", Value: *v1beta1.NewObject(map[string]string{ + "key1": "task", + "key2": "param!", + })}, + }, + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "myobject", + Properties: map[string]v1beta1.PropertySpec{ + "key1": {Type: "string"}, + "key2": {Type: "string"}, + }, + Default: v1beta1.NewObject(map[string]string{ + "key1": "default", + "key2": "param!!", + }), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "$(params.myobject.key1) $(params.myobject.key2)"}, + }}, + }, + }, + }}, + }, + params: []v1beta1.Param{{Name: "myobject", Value: *v1beta1.NewObject(map[string]string{"key1": "pipeline", "key2": "param!!!"})}}, + expected: v1beta1.PipelineSpec{ + Finally: []v1beta1.PipelineTask{{ + Params: []v1beta1.Param{ + {Name: "myobject", Value: *v1beta1.NewObject(map[string]string{ + "key1": "task", + "key2": "param!", + })}, + }, + TaskSpec: &v1beta1.EmbeddedTask{ + TaskSpec: v1beta1.TaskSpec{ + Params: []v1beta1.ParamSpec{{ + Name: "myobject", + Properties: map[string]v1beta1.PropertySpec{ + "key1": {Type: "string"}, + "key2": {Type: "string"}, + }, + Default: v1beta1.NewObject(map[string]string{ + "key1": "default", + "key2": "param!!", + }), + }}, + Steps: []v1beta1.Step{{ + Name: "step1", + Image: "ubuntu", + Args: []string{"#!/usr/bin/env bash\n", "echo", "task param!"}, + }}, + }, + }, + }}, + }, + alpha: true, }, { name: "single parameter with when expression", original: v1beta1.PipelineSpec{