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 authored and knative-prow-robot committed Feb 15, 2019
1 parent 3255e31 commit 095868c
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
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)
}
})
}
}
14 changes: 13 additions & 1 deletion pkg/reconciler/v1alpha1/taskrun/taskrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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 @@ -106,6 +107,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 @@ -171,6 +176,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 @@ -457,7 +463,13 @@ func TestReconcile(t *testing.T) {
BlockOwnerDeletion: &boolTrue,
}},
wantServiceAccountName: "",
wantPodVolume: getVolumes(),
wantPodVolume: append([]corev1.Volume{{
Name: "volume-configmap",
VolumeSource: corev1.VolumeSource{
ConfigMap: &corev1.ConfigMapVolumeSource{
corev1.LocalObjectReference{"configbar"}, nil, nil, nil},
},
}}, getVolumes()...),
wantSteps: []corev1.Container{{
Name: "build-step-credential-initializer-mz4c7",
Image: "override-with-creds:latest",
Expand Down

0 comments on commit 095868c

Please sign in to comment.