Skip to content

Commit

Permalink
Implement templating within stepTemplate.
Browse files Browse the repository at this point in the history
Utilize the applyContanierReplacements() logic for the stepTemplate
field. This was done for containerTemplate (now deprecated), but when
stepTemplate was introduced the templating functionality was forgotten.
  • Loading branch information
EliZucker authored and tekton-robot committed Jul 10, 2019
1 parent 4f15cb8 commit 254a715
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ func ApplyReplacements(spec *v1alpha1.TaskSpec, replacements map[string]string)
}

// Apply variable expansion to containerTemplate fields.
// Should eventually be removed; ContainerTemplate is the deprecated previous name of the StepTemplate field (#977).
if spec.ContainerTemplate != nil {
applyContainerReplacements(spec.ContainerTemplate, replacements)
}

// Apply variable expansion to stepTemplate fields.
if spec.StepTemplate != nil {
applyContainerReplacements(spec.StepTemplate, replacements)
}

// Apply variable expansion to the build's volumes
for i, v := range spec.Volumes {
spec.Volumes[i].Name = templating.ApplyReplacements(v.Name, replacements)
Expand Down
47 changes: 47 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var envTaskSpec = &v1alpha1.TaskSpec{
}},
}

// containerTemplate is deprecated but is functional (and tested) for now (#977).
var containerTemplateTaskSpec = &v1alpha1.TaskSpec{
ContainerTemplate: &corev1.Container{
Env: []corev1.EnvVar{{
Expand All @@ -99,6 +100,26 @@ var containerTemplateTaskSpec = &v1alpha1.TaskSpec{
}},
}

var stepTemplateTaskSpec = &v1alpha1.TaskSpec{
StepTemplate: &corev1.Container{
Env: []corev1.EnvVar{{
Name: "template-var",
Value: "${inputs.params.FOO}",
}},
},
Steps: []corev1.Container{{
Name: "simple-image",
Image: "${inputs.params.myimage}",
}, {
Name: "image-with-env-specified",
Image: "some-other-image",
Env: []corev1.EnvVar{{
Name: "template-var",
Value: "overridden-value",
}},
}},
}

var volumeMountTaskSpec = &v1alpha1.TaskSpec{
Steps: []corev1.Container{{
Name: "foo",
Expand Down Expand Up @@ -269,6 +290,7 @@ func TestApplyParameters(t *testing.T) {
spec.Steps[0].Image = "busybox:world"
}),
}, {
// containerTemplate is deprecated but is functional (and tested) for now (#977).
name: "containerTemplate parameter",
args: args{
ts: containerTemplateTaskSpec,
Expand All @@ -293,6 +315,31 @@ func TestApplyParameters(t *testing.T) {
spec.ContainerTemplate.Env[0].Value = "BAR"
spec.Steps[0].Image = "replaced-image-name"
}),
}, {
name: "stepTemplate parameter",
args: args{
ts: stepTemplateTaskSpec,
tr: &v1alpha1.TaskRun{
Spec: v1alpha1.TaskRunSpec{
Inputs: v1alpha1.TaskRunInputs{
Params: []v1alpha1.Param{{
Name: "FOO",
Value: "BAR",
}},
},
},
},
dp: []v1alpha1.ParamSpec{
{
Name: "myimage",
Default: "replaced-image-name",
},
},
},
want: applyMutation(stepTemplateTaskSpec, func(spec *v1alpha1.TaskSpec) {
spec.StepTemplate.Env[0].Value = "BAR"
spec.Steps[0].Image = "replaced-image-name"
}),
}, {
name: "with default parameter",
args: args{
Expand Down

0 comments on commit 254a715

Please sign in to comment.