Skip to content

Commit 8a7ccf4

Browse files
chitrangpateltekton-robot
authored andcommitted
Fix: Identify workspace usage in a Task
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.
1 parent 6cbfbcd commit 8a7ccf4

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

pkg/workspace/apply.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,10 @@ func findWorkspaceSubstitutionLocationsInSidecars(sidecars []v1.Sidecar) sets.St
225225
for i := range sidecar.Command {
226226
locationsToCheck.Insert(sidecar.Command[i])
227227
}
228+
locationsToCheck.Insert(sidecar.WorkingDir)
229+
for _, e := range sidecar.Env {
230+
locationsToCheck.Insert(e.Value)
231+
}
228232
}
229233
return locationsToCheck
230234
}
@@ -241,6 +245,18 @@ func findWorkspaceSubstitutionLocationsInSteps(steps []v1.Step) sets.String {
241245
for i := range step.Command {
242246
locationsToCheck.Insert(step.Command[i])
243247
}
248+
249+
locationsToCheck.Insert(step.WorkingDir)
250+
for _, e := range step.Env {
251+
locationsToCheck.Insert(e.Value)
252+
}
253+
for _, p := range step.Params {
254+
locationsToCheck.Insert(p.Value.ArrayVal...)
255+
for k := range p.Value.ObjectVal {
256+
locationsToCheck.Insert(p.Value.ObjectVal[k])
257+
}
258+
locationsToCheck.Insert(p.Value.StringVal)
259+
}
244260
}
245261
return locationsToCheck
246262
}
@@ -255,6 +271,11 @@ func findWorkspaceSubstitutionLocationsInStepTemplate(stepTemplate *v1.StepTempl
255271
for i := range stepTemplate.Command {
256272
locationsToCheck.Insert(stepTemplate.Command[i])
257273
}
274+
275+
locationsToCheck.Insert(stepTemplate.WorkingDir)
276+
for _, e := range stepTemplate.Env {
277+
locationsToCheck.Insert(e.Value)
278+
}
258279
}
259280
return locationsToCheck
260281
}

pkg/workspace/apply_test.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,104 @@ func TestFindWorkspacesUsedByTask(t *testing.T) {
12251225
"steptemplate-args",
12261226
"steptemplate-command",
12271227
),
1228+
}, {
1229+
name: "workspace used in step env",
1230+
ts: &v1.TaskSpec{
1231+
Steps: []v1.Step{{
1232+
Name: "step-name",
1233+
Image: "step-image",
1234+
Env: []corev1.EnvVar{{
1235+
Name: "path",
1236+
Value: "$(workspaces.env-ws.path)",
1237+
}},
1238+
Command: []string{"ls"},
1239+
}},
1240+
},
1241+
want: sets.NewString(
1242+
"env-ws",
1243+
),
1244+
}, {
1245+
name: "workspace used in step workingDir",
1246+
ts: &v1.TaskSpec{
1247+
Steps: []v1.Step{{
1248+
Name: "step-name",
1249+
Image: "step-image",
1250+
WorkingDir: "$(workspaces.shared.path)",
1251+
Command: []string{"ls"},
1252+
}},
1253+
},
1254+
want: sets.NewString(
1255+
"shared",
1256+
),
1257+
}, {
1258+
name: "workspace used in step Params for stepactions",
1259+
ts: &v1.TaskSpec{
1260+
Steps: []v1.Step{{
1261+
Name: "step-name",
1262+
Params: []v1.Param{{
1263+
Name: "path",
1264+
Value: *v1.NewStructuredValues("$(workspaces.shared.path)"),
1265+
}},
1266+
Ref: &v1.Ref{
1267+
Name: "step-action",
1268+
},
1269+
}},
1270+
},
1271+
want: sets.NewString(
1272+
"shared",
1273+
),
1274+
}, {
1275+
name: "workspace used in sidecar env",
1276+
ts: &v1.TaskSpec{
1277+
Sidecars: []v1.Sidecar{{
1278+
Name: "step-name",
1279+
Image: "step-image",
1280+
Env: []corev1.EnvVar{{
1281+
Name: "path",
1282+
Value: "$(workspaces.env-ws.path)",
1283+
}},
1284+
Command: []string{"ls"},
1285+
}},
1286+
},
1287+
want: sets.NewString(
1288+
"env-ws",
1289+
),
1290+
}, {
1291+
name: "workspace used in sidecar workingDir",
1292+
ts: &v1.TaskSpec{
1293+
Sidecars: []v1.Sidecar{{
1294+
Name: "step-name",
1295+
Image: "step-image",
1296+
WorkingDir: "$(workspaces.shared.path)",
1297+
Command: []string{"ls"},
1298+
}},
1299+
},
1300+
want: sets.NewString(
1301+
"shared",
1302+
),
1303+
}, {
1304+
name: "workspace used in stepTemplate env",
1305+
ts: &v1.TaskSpec{
1306+
StepTemplate: &v1.StepTemplate{
1307+
Env: []corev1.EnvVar{{
1308+
Name: "path",
1309+
Value: "$(workspaces.env-ws.path)",
1310+
}},
1311+
},
1312+
},
1313+
want: sets.NewString(
1314+
"env-ws",
1315+
),
1316+
}, {
1317+
name: "workspace used in stepTemplate workingDir",
1318+
ts: &v1.TaskSpec{
1319+
StepTemplate: &v1.StepTemplate{
1320+
WorkingDir: "$(workspaces.shared.path)",
1321+
},
1322+
},
1323+
want: sets.NewString(
1324+
"shared",
1325+
),
12281326
}}
12291327
for _, tt := range tests {
12301328
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)