diff --git a/docs/variables.md b/docs/variables.md index 4662def16c1..6708b886527 100644 --- a/docs/variables.md +++ b/docs/variables.md @@ -25,6 +25,7 @@ in `Tasks` and `Pipelines`. | `resources.outputs..path` | The path to the output resource's directory. | | `results..path` | The path to the file where a Task's result should be written. | | `workspaces..path` | The path to the mounted workspace. | +| `workspaces..claim` | The name of the PersistentVolumeClaim used as a volume source for the workspace or empty string if a volume source other than PersistentVolumeClaim was used. | | `workspaces..volume` | The name of the volume populating the workspace. | | `credentials.path` | The path to the location of credentials written by the `creds-init` init container. | diff --git a/pkg/reconciler/taskrun/resources/apply.go b/pkg/reconciler/taskrun/resources/apply.go index cc6c1f7404f..c2004106f61 100644 --- a/pkg/reconciler/taskrun/resources/apply.go +++ b/pkg/reconciler/taskrun/resources/apply.go @@ -96,8 +96,9 @@ func ApplyResources(spec *v1alpha1.TaskSpec, resolvedResources map[string]v1alph return ApplyReplacements(spec, replacements, map[string][]string{}) } -// ApplyWorkspaces applies the substitution from paths that the workspaces in w are mounted to and the -// volumes that wb are realized with in the task spec ts. +// ApplyWorkspaces applies the substitution from paths that the workspaces in w are mounted to, the +// volumes that wb are realized with in the task spec ts and the PersistentVolumeClaim names for the +// workspaces. func ApplyWorkspaces(spec *v1alpha1.TaskSpec, w []v1alpha1.WorkspaceDeclaration, wb []v1alpha1.WorkspaceBinding) *v1alpha1.TaskSpec { stringReplacements := map[string]string{} @@ -108,6 +109,13 @@ func ApplyWorkspaces(spec *v1alpha1.TaskSpec, w []v1alpha1.WorkspaceDeclaration, for name, vv := range v { stringReplacements[fmt.Sprintf("workspaces.%s.volume", name)] = vv.Name } + for _, w := range wb { + if w.PersistentVolumeClaim != nil { + stringReplacements[fmt.Sprintf("workspaces.%s.claim", w.Name)] = w.PersistentVolumeClaim.ClaimName + } else { + stringReplacements[fmt.Sprintf("workspaces.%s.claim", w.Name)] = "" + } + } return ApplyReplacements(spec, stringReplacements, map[string][]string{}) } diff --git a/pkg/reconciler/taskrun/resources/apply_test.go b/pkg/reconciler/taskrun/resources/apply_test.go index 7d38f388190..db9660d8cc7 100644 --- a/pkg/reconciler/taskrun/resources/apply_test.go +++ b/pkg/reconciler/taskrun/resources/apply_test.go @@ -609,6 +609,12 @@ func TestApplyWorkspaces(t *testing.T) { Env: []corev1.EnvVar{{ Name: "template-var", Value: "$(workspaces.myws.volume)", + }, { + Name: "pvc-name", + Value: "$(workspaces.myws.claim)", + }, { + Name: "non-pvc-name", + Value: "$(workspaces.otherws.claim)", }}, }, Steps: []v1alpha1.Step{{Container: corev1.Container{ @@ -671,6 +677,8 @@ func TestApplyWorkspaces(t *testing.T) { }} want := applyMutation(ts, func(spec *v1alpha1.TaskSpec) { spec.StepTemplate.Env[0].Value = "ws-9l9zj" + spec.StepTemplate.Env[1].Value = "foo" + spec.StepTemplate.Env[2].Value = "" spec.Steps[0].Name = "ws-9l9zj" spec.Steps[0].Image = "ws-mz4c7"