diff --git a/examples/v1beta1/taskruns/steptemplate-variable-interop.yaml b/examples/v1beta1/taskruns/steptemplate-variable-interop.yaml new file mode 100644 index 00000000000..c673a330743 --- /dev/null +++ b/examples/v1beta1/taskruns/steptemplate-variable-interop.yaml @@ -0,0 +1,17 @@ +apiVersion: tekton.dev/v1beta1 +kind: TaskRun +metadata: + generateName: steptemplate-variables- +spec: + params: + - name: image + value: ubuntu + taskSpec: + params: + - name: image + stepTemplate: + image: $(params.image) + steps: + - name: foo + script: | + echo hello from $(params.image) diff --git a/pkg/apis/pipeline/v1beta1/container_types.go b/pkg/apis/pipeline/v1beta1/container_types.go index 1c96476835f..742b16bae7b 100644 --- a/pkg/apis/pipeline/v1beta1/container_types.go +++ b/pkg/apis/pipeline/v1beta1/container_types.go @@ -421,6 +421,31 @@ type StepTemplate struct { TTY bool `json:"tty,omitempty" protobuf:"varint,18,opt,name=tty"` } +// SetContainerFields sets the fields of the Step to the values of the corresponding fields in the Container +func (s *StepTemplate) SetContainerFields(c corev1.Container) { + s.Name = c.Name + s.Image = c.Image + s.Command = c.Command + s.Args = c.Args + s.WorkingDir = c.WorkingDir + s.Ports = c.Ports + s.EnvFrom = c.EnvFrom + s.Env = c.Env + s.Resources = c.Resources + s.VolumeMounts = c.VolumeMounts + s.VolumeDevices = c.VolumeDevices + s.LivenessProbe = c.LivenessProbe + s.ReadinessProbe = c.ReadinessProbe + s.StartupProbe = c.StartupProbe + s.Lifecycle = c.Lifecycle + s.TerminationMessagePath = c.TerminationMessagePath + s.ImagePullPolicy = c.ImagePullPolicy + s.SecurityContext = c.SecurityContext + s.Stdin = c.Stdin + s.StdinOnce = c.StdinOnce + s.TTY = c.TTY +} + // ToK8sContainer converts the StepTemplate to a Kubernetes Container struct func (s *StepTemplate) ToK8sContainer() *corev1.Container { return &corev1.Container{ diff --git a/pkg/apis/pipeline/v1beta1/step_replacements.go b/pkg/apis/pipeline/v1beta1/step_replacements.go index b39466837c5..3331d30d6fe 100644 --- a/pkg/apis/pipeline/v1beta1/step_replacements.go +++ b/pkg/apis/pipeline/v1beta1/step_replacements.go @@ -25,3 +25,10 @@ func ApplyStepReplacements(step *Step, stringReplacements map[string]string, arr step.Script = substitution.ApplyReplacements(step.Script, stringReplacements) applyStepReplacements(step, stringReplacements, arrayReplacements) } + +// ApplyStepTemplateReplacements applies variable interpolation on a StepTemplate (aka a container) +func ApplyStepTemplateReplacements(stepTemplate *StepTemplate, stringReplacements map[string]string, arrayReplacements map[string][]string) { + container := stepTemplate.ToK8sContainer() + applyContainerReplacements(container, stringReplacements, arrayReplacements) + stepTemplate.SetContainerFields(*container) +} diff --git a/pkg/reconciler/taskrun/resources/apply.go b/pkg/reconciler/taskrun/resources/apply.go index 6533950017e..c038fbc3915 100644 --- a/pkg/reconciler/taskrun/resources/apply.go +++ b/pkg/reconciler/taskrun/resources/apply.go @@ -248,10 +248,7 @@ func ApplyReplacements(spec *v1beta1.TaskSpec, stringReplacements map[string]str // Apply variable expansion to stepTemplate fields. if spec.StepTemplate != nil { - c := spec.StepTemplate.ToK8sContainer() - step := v1beta1.Step{} - step.SetContainerFields(*c) - v1beta1.ApplyStepReplacements(&step, stringReplacements, arrayReplacements) + v1beta1.ApplyStepTemplateReplacements(spec.StepTemplate, stringReplacements, arrayReplacements) } // Apply variable expansion to the build's volumes diff --git a/pkg/reconciler/taskrun/resources/apply_test.go b/pkg/reconciler/taskrun/resources/apply_test.go index e1b0c7fb949..d7e3636e752 100644 --- a/pkg/reconciler/taskrun/resources/apply_test.go +++ b/pkg/reconciler/taskrun/resources/apply_test.go @@ -62,6 +62,7 @@ var ( Name: "template-var", Value: `$(params["FOO"])`, }}, + Image: "$(params.myimage)", }, Steps: []v1beta1.Step{{ Name: "foo", @@ -526,6 +527,7 @@ func TestApplyParameters(t *testing.T) { }} want := applyMutation(simpleTaskSpec, func(spec *v1beta1.TaskSpec) { spec.StepTemplate.Env[0].Value = "world" + spec.StepTemplate.Image = "bar" spec.Steps[0].Image = "bar" spec.Steps[2].Image = "mydefault"