Skip to content

Commit

Permalink
Add support for tempalting volume names and volume config names
Browse files Browse the repository at this point in the history
why: Build supported this feature and one of the build yaml test relies on
this feature
what: Taskrun can template volumes names and config volume sources
  • Loading branch information
Shash Reddy committed Feb 14, 2019
1 parent 87ff366 commit fa078a6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,14 @@ func ApplyReplacements(build *buildv1alpha1.Build, replacements map[string]strin
steps[i].VolumeMounts[iv].SubPath = templating.ApplyReplacements(v.SubPath, replacements)
}
}

// Apply variable expansion to the build's volumes
for i, v := range build.Spec.Volumes {
build.Spec.Volumes[i].Name = templating.ApplyReplacements(v.Name, replacements)
if v.VolumeSource.ConfigMap != nil {
build.Spec.Volumes[i].ConfigMap.Name = templating.ApplyReplacements(v.ConfigMap.Name, replacements)
}
}

return build
}
70 changes: 70 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,73 @@ func TestApplyResources(t *testing.T) {
})
}
}

func TestVolumeReplacement(t *testing.T) {
tests := []struct {
name string
b *buildv1alpha1.Build
repl map[string]string
want *buildv1alpha1.Build
}{{
name: "volume replacement",
b: &buildv1alpha1.Build{
Spec: buildv1alpha1.BuildSpec{
Volumes: []corev1.Volume{{
Name: "${foo}",
}},
},
},
want: &buildv1alpha1.Build{
Spec: buildv1alpha1.BuildSpec{
Volumes: []corev1.Volume{{
Name: "bar",
}},
},
},
repl: map[string]string{"foo": "bar"},
}, {
name: "volume configmap",
b: &buildv1alpha1.Build{
Spec: buildv1alpha1.BuildSpec{
Volumes: []corev1.Volume{{
Name: "${name}",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
corev1.LocalObjectReference{"${configmapname}"},
nil,
nil,
nil,
},
}},
},
},
},
repl: map[string]string{
"name": "myname",
"configmapname": "cfgmapname",
},
want: &buildv1alpha1.Build{
Spec: buildv1alpha1.BuildSpec{
Volumes: []corev1.Volume{{
Name: "myname",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
corev1.LocalObjectReference{"cfgmapname"},
nil,
nil,
nil,
},
}},
},
},
},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ApplyReplacements(tt.b, tt.repl)
if d := cmp.Diff(got, tt.want); d != "" {
t.Errorf("ApplyResources() diff %s", d)
}
})
}
}
13 changes: 13 additions & 0 deletions pkg/reconciler/v1alpha1/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var (
tb.InputsResource("workspace", v1alpha1.PipelineResourceTypeGit),
tb.InputsParam("myarg"), tb.InputsParam("myarghasdefault", tb.ParamDefault("dont see me")),
tb.InputsParam("myarghasdefault2", tb.ParamDefault("thedefault")),
tb.InputsParam("configmapname"),
),
tb.TaskOutputs(tb.OutputsResource("myimage", v1alpha1.PipelineResourceTypeImage)),
tb.Step("mycontainer", "myimage", tb.Command("/mycmd"), tb.Args(
Expand All @@ -105,6 +106,10 @@ var (
tb.Step("myothercontainer", "myotherimage", tb.Command("/mycmd"), tb.Args(
"--my-other-arg=${inputs.resources.workspace.url}",
)),
tb.TaskVolume("volume-configmap", tb.VolumeSource(corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
corev1.LocalObjectReference{"${inputs.params.configmapname}"}, nil, nil, nil},
})),
))

gitResource = tb.PipelineResource("git-resource", "foo", tb.PipelineResourceSpec(
Expand Down Expand Up @@ -169,6 +174,7 @@ func TestReconcile(t *testing.T) {
tb.TaskRunInputs(
tb.TaskRunInputsParam("myarg", "foo"),
tb.TaskRunInputsParam("myarghasdefault", "bar"),
tb.TaskRunInputsParam("configmapname", "configbar"),
tb.TaskRunInputsResource("workspace", tb.TaskResourceBindingRef(gitResource.Name)),
),
tb.TaskRunOutputs(tb.TaskRunOutputsResource("myimage", tb.TaskResourceBindingRef("image-resource"))),
Expand Down Expand Up @@ -315,6 +321,13 @@ func TestReconcile(t *testing.T) {
tb.EnvVar("ENTRYPOINT_OPTIONS", `{"args":["/mycmd","--my-other-arg=https://foo.git"],"process_log":"/tools/process-log.txt","marker_file":"/tools/marker-file.txt"}`),
tb.VolumeMount(toolsMount),
),
tb.BuildVolume(corev1.Volume{
Name: "volume-configmap",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
corev1.LocalObjectReference{"configbar"}, nil, nil, nil},
},
}),
tb.BuildVolume(getToolsVolume(taskRunTemplating.Name)),
),
),
Expand Down

0 comments on commit fa078a6

Please sign in to comment.