Skip to content

Commit 4d2babc

Browse files
committed
Add variable expansion for params in Projected Volume fields
A [projected volume](https://kubernetes.io/docs/concepts/storage/volumes/#projected) can mount/project files from `Secrets`, `ConfigMaps` and `ServiceAccountTokens`. Is is good if the end user can choose the name of `Secrets`, `ConfigMaps` and the audience of `ServiceAccountTokens`. With this commit, the task author can use `params` for `secret.name`, `configmap.name` and `serviceaccounttoken.audience` in a Projected Volume. See examples of use cases in #2597 Fixes #2597
1 parent 1fbac2a commit 4d2babc

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

docs/tasks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ The `description` field is an optional field that allows you to add an informati
542542

543543
`Tasks` allow you to substitute variable names for the following entities:
544544

545-
- [Parameters and resources]](#substituting-parameters-and-resources)
545+
- [Parameters and resources](#substituting-parameters-and-resources)
546546
- [`Array` parameters](#substituting-array-parameters)
547547
- [`Workspaces`](#substituting-workspace-paths)
548548
- [`Volume` names and types](#substituting-volume-names-and-paths)

pkg/reconciler/taskrun/resources/apply.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ func ApplyReplacements(spec *v1beta1.TaskSpec, stringReplacements map[string]str
166166
if v.PersistentVolumeClaim != nil {
167167
spec.Volumes[i].PersistentVolumeClaim.ClaimName = substitution.ApplyReplacements(v.PersistentVolumeClaim.ClaimName, stringReplacements)
168168
}
169+
if v.Projected != nil {
170+
for _, s := range spec.Volumes[i].Projected.Sources {
171+
if s.ConfigMap != nil {
172+
s.ConfigMap.Name = substitution.ApplyReplacements(s.ConfigMap.Name, stringReplacements)
173+
}
174+
if s.Secret != nil {
175+
s.Secret.Name = substitution.ApplyReplacements(s.Secret.Name, stringReplacements)
176+
}
177+
if s.ServiceAccountToken != nil {
178+
s.ServiceAccountToken.Audience = substitution.ApplyReplacements(s.ServiceAccountToken.Audience, stringReplacements)
179+
}
180+
}
181+
}
169182
}
170183

171184
// Apply variable substitution to the sidecar definitions

pkg/reconciler/taskrun/resources/apply_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,27 @@ var (
159159
ClaimName: "$(inputs.params.FOO)",
160160
},
161161
},
162+
}, {
163+
Name: "some-projected-volumes",
164+
VolumeSource: corev1.VolumeSource{
165+
Projected: &corev1.ProjectedVolumeSource{
166+
Sources: []corev1.VolumeProjection{{
167+
ConfigMap: &corev1.ConfigMapProjection{
168+
LocalObjectReference: corev1.LocalObjectReference{
169+
Name: "$(inputs.params.FOO)",
170+
},
171+
},
172+
Secret: &corev1.SecretProjection{
173+
LocalObjectReference: corev1.LocalObjectReference{
174+
Name: "$(inputs.params.FOO)",
175+
},
176+
},
177+
ServiceAccountToken: &corev1.ServiceAccountTokenProjection{
178+
Audience: "$(inputs.params.FOO)",
179+
},
180+
}},
181+
},
182+
},
162183
}},
163184
Resources: &v1beta1.TaskResources{
164185
Inputs: []v1beta1.TaskResource{{
@@ -515,6 +536,9 @@ func TestApplyParameters(t *testing.T) {
515536
spec.Volumes[0].VolumeSource.ConfigMap.LocalObjectReference.Name = "world"
516537
spec.Volumes[1].VolumeSource.Secret.SecretName = "world"
517538
spec.Volumes[2].VolumeSource.PersistentVolumeClaim.ClaimName = "world"
539+
spec.Volumes[3].VolumeSource.Projected.Sources[0].ConfigMap.Name = "world"
540+
spec.Volumes[3].VolumeSource.Projected.Sources[0].Secret.Name = "world"
541+
spec.Volumes[3].VolumeSource.Projected.Sources[0].ServiceAccountToken.Audience = "world"
518542

519543
spec.Sidecars[0].Container.Image = "bar"
520544
spec.Sidecars[0].Container.Env[0].Value = "world"

0 commit comments

Comments
 (0)