Skip to content

Commit

Permalink
Fix: Identify workspace usage in a Task
Browse files Browse the repository at this point in the history
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 4, 2024
1 parent 15b5b71 commit 9220a12
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/workspace/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,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 @@ -258,6 +262,18 @@ 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)
}
for _, p := range step.Params {
locationsToCheck.Insert(p.Value.ArrayVal...)
for k := range p.Value.ObjectVal {
locationsToCheck.Insert(p.Value.ObjectVal[k])
}
locationsToCheck.Insert(p.Value.StringVal)
}
}
return locationsToCheck
}
Expand All @@ -272,6 +288,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
98 changes: 98 additions & 0 deletions pkg/workspace/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,104 @@ 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 step Params for stepactions",
ts: &v1.TaskSpec{
Steps: []v1.Step{{
Name: "step-name",
Params: []v1.Param{{
Name: "path",
Value: *v1.NewStructuredValues("$(workspaces.shared.path)"),
}},
Ref: &v1.Ref{
Name: "step-action",
},
}},
},
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 9220a12

Please sign in to comment.