Skip to content

Commit

Permalink
Fix: Identify workspace usage in a Task
Browse files Browse the repository at this point in the history
Manual cherry-pick to avoid conflict with step-params.
Prior to this, when identifying whether a Task used a workspace,
we limited the check to command, args and scripts in steps,
stepTemplates and sidecars. However, the workspace path could also
be used as a param to a StepAction or env cariables in steps and
sidecars and also workingDirs. This PR fixes that.

Fixes #8008.
  • Loading branch information
chitrangpatel authored and tekton-robot committed Jun 5, 2024
1 parent 66f1f9e commit 40b1bbd
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/workspace/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ func findWorkspaceSubstitutionLocationsInSidecars(sidecars []v1.Sidecar) sets.St
for i := range sidecar.Command {
locationsToCheck.Insert(sidecar.Command[i])
}
locationsToCheck.Insert(sidecar.WorkingDir)
for _, e := range sidecar.Env {
locationsToCheck.Insert(e.Value)
}
}
return locationsToCheck
}
Expand All @@ -241,6 +245,11 @@ func findWorkspaceSubstitutionLocationsInSteps(steps []v1.Step) sets.String {
for i := range step.Command {
locationsToCheck.Insert(step.Command[i])
}

locationsToCheck.Insert(step.WorkingDir)
for _, e := range step.Env {
locationsToCheck.Insert(e.Value)
}
}
return locationsToCheck
}
Expand All @@ -255,6 +264,11 @@ func findWorkspaceSubstitutionLocationsInStepTemplate(stepTemplate *v1.StepTempl
for i := range stepTemplate.Command {
locationsToCheck.Insert(stepTemplate.Command[i])
}

locationsToCheck.Insert(stepTemplate.WorkingDir)
for _, e := range stepTemplate.Env {
locationsToCheck.Insert(e.Value)
}
}
return locationsToCheck
}
Expand Down
81 changes: 81 additions & 0 deletions pkg/workspace/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,87 @@ func TestFindWorkspacesUsedByTask(t *testing.T) {
"steptemplate-args",
"steptemplate-command",
),
}, {
name: "workspace used in step env",
ts: &v1.TaskSpec{
Steps: []v1.Step{{
Name: "step-name",
Image: "step-image",
Env: []corev1.EnvVar{{
Name: "path",
Value: "$(workspaces.env-ws.path)",
}},
Command: []string{"ls"},
}},
},
want: sets.NewString(
"env-ws",
),
}, {
name: "workspace used in step workingDir",
ts: &v1.TaskSpec{
Steps: []v1.Step{{
Name: "step-name",
Image: "step-image",
WorkingDir: "$(workspaces.shared.path)",
Command: []string{"ls"},
}},
},
want: sets.NewString(
"shared",
),
}, {
name: "workspace used in sidecar env",
ts: &v1.TaskSpec{
Sidecars: []v1.Sidecar{{
Name: "step-name",
Image: "step-image",
Env: []corev1.EnvVar{{
Name: "path",
Value: "$(workspaces.env-ws.path)",
}},
Command: []string{"ls"},
}},
},
want: sets.NewString(
"env-ws",
),
}, {
name: "workspace used in sidecar workingDir",
ts: &v1.TaskSpec{
Sidecars: []v1.Sidecar{{
Name: "step-name",
Image: "step-image",
WorkingDir: "$(workspaces.shared.path)",
Command: []string{"ls"},
}},
},
want: sets.NewString(
"shared",
),
}, {
name: "workspace used in stepTemplate env",
ts: &v1.TaskSpec{
StepTemplate: &v1.StepTemplate{
Env: []corev1.EnvVar{{
Name: "path",
Value: "$(workspaces.env-ws.path)",
}},
},
},
want: sets.NewString(
"env-ws",
),
}, {
name: "workspace used in stepTemplate workingDir",
ts: &v1.TaskSpec{
StepTemplate: &v1.StepTemplate{
WorkingDir: "$(workspaces.shared.path)",
},
},
want: sets.NewString(
"shared",
),
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 40b1bbd

Please sign in to comment.