Skip to content

Commit

Permalink
Add variable substitution for PVC name
Browse files Browse the repository at this point in the history
There may be use cases when a Task should initiate a PipelineRun or TaskRun.
It can be beneficial to use the **same PVC** in the PipelineRun or TaskRun
that is initiated, e.g. populated with cached data or a cloned git repository.

When using a workspace with a PersistentVolumeClaim as volume source, the name
of the PVC is known, and can be passed as a _param_ in the PipelineRun or
TaskRun. But when a VolumeClaimTemplate is used as a volume source, the name
is generated. In such use cases, it is useful to be able to "look up" the PVC
name using variable substitution.

Fixes #2505
  • Loading branch information
jlpettersson authored and tekton-robot committed Apr 29, 2020
1 parent 1c532fa commit ab391e7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ in `Tasks` and `Pipelines`.
| `resources.outputs.<resource name>.path` | The path to the output resource's directory. |
| `results.<result name>.path` | The path to the file where a Task's result should be written. |
| `workspaces.<workspace name>.path` | The path to the mounted workspace. |
| `workspaces.<workspace name>.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.<workspace name>.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. |

Expand Down
12 changes: 10 additions & 2 deletions pkg/reconciler/taskrun/resources/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -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{})
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/reconciler/taskrun/resources/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit ab391e7

Please sign in to comment.