Skip to content

Commit 7cec96b

Browse files
committed
Refactor Sidecar Containers Construction If Script Exists
Prior to this commit, in order to reuse the code, we convert v1beta1 Sidecar object to v1beta1.Step and then construct containers out of those steps when the script field is specified. As we are switching the storage version to v1, some fields are deprecated in v1.Step (but not Sidecar), thus it does not make sense to convert sidercar to step. While reusing as much code as possible, this commit refactor the code to seperate the container construction process for steps and sidecars.
1 parent 09d422c commit 7cec96b

File tree

3 files changed

+314
-136
lines changed

3 files changed

+314
-136
lines changed

pkg/pod/pod_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func TestPodBuild(t *testing.T) {
575575
Image: "busybox",
576576
Command: []string{"sh"},
577577
VolumeMounts: []corev1.VolumeMount{writeScriptsVolumeMount, binMount},
578-
Args: []string{"-c", `scriptfile="/tekton/scripts/sidecar-script-0-9l9zj"
578+
Args: []string{"-c", `scriptfile="/tekton/scripts/sidecar-script-0-mz4c7"
579579
touch ${scriptfile} && chmod +x ${scriptfile}
580580
cat > ${scriptfile} << '_EOF_'
581581
IyEvYmluL3NoCmVjaG8gaGVsbG8gZnJvbSBzaWRlY2Fy
@@ -610,7 +610,7 @@ _EOF_
610610
}, {
611611
Name: "sidecar-sc-name",
612612
Image: "sidecar-image",
613-
Command: []string{"/tekton/scripts/sidecar-script-0-9l9zj"},
613+
Command: []string{"/tekton/scripts/sidecar-script-0-mz4c7"},
614614
VolumeMounts: []corev1.VolumeMount{scriptsVolumeMount},
615615
}},
616616
Volumes: append(implicitVolumes, scriptsVolume, binVolume, runVolume(0), downwardVolume, corev1.Volume{
@@ -2068,7 +2068,7 @@ func TestPodBuildwithAlphaAPIEnabled(t *testing.T) {
20682068
VolumeMounts: []corev1.VolumeMount{writeScriptsVolumeMount, binMount, debugScriptsVolumeMount},
20692069
Args: []string{"-c", `tmpfile="/tekton/debug/scripts/debug-continue"
20702070
touch ${tmpfile} && chmod +x ${tmpfile}
2071-
cat > ${tmpfile} << 'debug-continue-heredoc-randomly-generated-9l9zj'
2071+
cat > ${tmpfile} << 'debug-continue-heredoc-randomly-generated-mz4c7'
20722072
#!/bin/sh
20732073
set -e
20742074
@@ -2087,10 +2087,10 @@ else
20872087
echo "Last step (no. $stepNumber) has already been executed, breakpoint exiting !"
20882088
exit 0
20892089
fi
2090-
debug-continue-heredoc-randomly-generated-9l9zj
2090+
debug-continue-heredoc-randomly-generated-mz4c7
20912091
tmpfile="/tekton/debug/scripts/debug-fail-continue"
20922092
touch ${tmpfile} && chmod +x ${tmpfile}
2093-
cat > ${tmpfile} << 'debug-fail-continue-heredoc-randomly-generated-mz4c7'
2093+
cat > ${tmpfile} << 'debug-fail-continue-heredoc-randomly-generated-mssqb'
20942094
#!/bin/sh
20952095
set -e
20962096
@@ -2109,7 +2109,7 @@ else
21092109
echo "Last step (no. $stepNumber) has already been executed, breakpoint exiting !"
21102110
exit 0
21112111
fi
2112-
debug-fail-continue-heredoc-randomly-generated-mz4c7
2112+
debug-fail-continue-heredoc-randomly-generated-mssqb
21132113
`},
21142114
}
21152115

pkg/pod/script.go

+117-97
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
2626
"github.com/tektoncd/pipeline/pkg/names"
2727
corev1 "k8s.io/api/core/v1"
28-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2928
)
3029

3130
const (
@@ -71,7 +70,6 @@ var (
7170

7271
// convertScripts converts any steps and sidecars that specify a Script field into a normal Container.
7372
func convertScripts(shellImageLinux string, shellImageWin string, steps []v1beta1.Step, sidecars []v1beta1.Sidecar, debugConfig *v1beta1.TaskRunDebug) (*corev1.Container, []corev1.Container, []corev1.Container) {
74-
placeScripts := false
7573
// Place scripts is an init container used for creating scripts in the
7674
// /tekton/scripts directory which would be later used by the step containers
7775
// as a Command
@@ -96,140 +94,162 @@ func convertScripts(shellImageLinux string, shellImageWin string, steps []v1beta
9694
}
9795

9896
breakpoints := []string{}
99-
sideCarSteps := []v1beta1.Step{}
100-
for _, sidecar := range sidecars {
101-
c := sidecar.ToK8sContainer()
102-
sidecarStep := v1beta1.Step{
103-
Script: sidecar.Script,
104-
Timeout: &metav1.Duration{},
105-
}
106-
sidecarStep.SetContainerFields(*c)
107-
sideCarSteps = append(sideCarSteps, sidecarStep)
108-
}
10997

11098
// Add mounts for debug
11199
if debugConfig != nil && len(debugConfig.Breakpoint) > 0 {
112100
breakpoints = debugConfig.Breakpoint
113101
placeScriptsInit.VolumeMounts = append(placeScriptsInit.VolumeMounts, debugScriptsVolumeMount)
114102
}
115103

116-
convertedStepContainers := convertListOfSteps(steps, &placeScriptsInit, &placeScripts, breakpoints, "script")
104+
convertedStepContainers := convertListOfSteps(steps, &placeScriptsInit, breakpoints, "script")
105+
sidecarContainers := convertListOfSidecars(sidecars, &placeScriptsInit, "sidecar-script")
117106

118-
// Pass empty breakpoint list in "sidecar step to container" converter to not rewrite the scripts and add breakpoints to sidecar
119-
sidecarContainers := convertListOfSteps(sideCarSteps, &placeScriptsInit, &placeScripts, []string{}, "sidecar-script")
120-
if placeScripts {
107+
if hasScripts(steps, sidecars, breakpoints) {
121108
return &placeScriptsInit, convertedStepContainers, sidecarContainers
122109
}
123110
return nil, convertedStepContainers, sidecarContainers
124111
}
125112

126-
// convertListOfSteps does the heavy lifting for convertScripts.
127-
//
128-
// It iterates through the list of steps (or sidecars), generates the script file name and heredoc termination string,
129-
// adds an entry to the init container args, sets up the step container to run the script, and sets the volume mounts.
130-
func convertListOfSteps(steps []v1beta1.Step, initContainer *corev1.Container, placeScripts *bool, breakpoints []string, namePrefix string) []corev1.Container {
131-
containers := []corev1.Container{}
132-
for i, s := range steps {
133-
// Add debug mounts if breakpoints are present
134-
if len(breakpoints) > 0 {
135-
debugInfoVolumeMount := corev1.VolumeMount{
136-
Name: debugInfoVolumeName,
137-
MountPath: filepath.Join(debugInfoDir, fmt.Sprintf("%d", i)),
138-
}
139-
steps[i].VolumeMounts = append(steps[i].VolumeMounts, debugScriptsVolumeMount, debugInfoVolumeMount)
113+
// hasScripts determines if we need to generate sripts in InitContainer given steps, sidecars and breakpoints.
114+
func hasScripts(steps []v1beta1.Step, sidecars []v1beta1.Sidecar, breakpoints []string) bool {
115+
for _, s := range steps {
116+
if s.Script != "" {
117+
return true
140118
}
141-
142-
if s.Script == "" {
143-
// Nothing to convert.
144-
containers = append(containers, *steps[i].ToK8sContainer())
145-
continue
119+
}
120+
for _, s := range sidecars {
121+
if s.Script != "" {
122+
return true
146123
}
124+
}
125+
if len(breakpoints) > 0 {
126+
return true
127+
}
128+
return false
129+
}
147130

148-
// Check for a shebang, and add a default if it's not set.
149-
// The shebang must be the first non-empty line.
150-
cleaned := strings.TrimSpace(s.Script)
151-
hasShebang := strings.HasPrefix(cleaned, "#!")
152-
requiresWindows := strings.HasPrefix(cleaned, "#!win")
153-
154-
script := s.Script
155-
if !hasShebang {
156-
script = defaultScriptPreamble + s.Script
157-
}
131+
// placeScriptInContainer given a piece of script to be executed, placeScriptInContainer firstly modifies initContainer
132+
// so that it capsules the target script into scriptFile, then it modifies the container so that it can execute the scriptFile
133+
// in runtime.
134+
func placeScriptInContainer(script, scriptFile string, c *corev1.Container, initContainer *corev1.Container) {
135+
if script == "" {
136+
return
137+
}
138+
cleaned := strings.TrimSpace(script)
139+
hasShebang := strings.HasPrefix(cleaned, "#!")
140+
requiresWindows := strings.HasPrefix(cleaned, "#!win")
158141

159-
// At least one step uses a script, so we should return a
160-
// non-nil init container.
161-
*placeScripts = true
142+
if !hasShebang {
143+
script = defaultScriptPreamble + script
144+
}
162145

163-
// Append to the place-scripts script to place the
164-
// script file in a known location in the scripts volume.
165-
scriptFile := filepath.Join(scriptsDir, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("%s-%d", namePrefix, i)))
166-
if requiresWindows {
167-
command, args, script, scriptFile := extractWindowsScriptComponents(script, scriptFile)
168-
initContainer.Args[1] += fmt.Sprintf(`@"
146+
// Append to the place-scripts script to place the
147+
// script file in a known location in the scripts volume.
148+
if requiresWindows {
149+
command, args, script, scriptFile := extractWindowsScriptComponents(script, scriptFile)
150+
initContainer.Args[1] += fmt.Sprintf(`@"
169151
%s
170152
"@ | Out-File -FilePath %s
171153
`, script, scriptFile)
172154

173-
steps[i].Command = command
174-
// Append existing args field to end of derived args
175-
args = append(args, steps[i].Args...)
176-
steps[i].Args = args
177-
} else {
178-
// Only encode the script for linux scripts
179-
// The decode-script subcommand of the entrypoint does not work under windows
180-
script = encodeScript(script)
181-
heredoc := "_EOF_" // underscores because base64 doesnt include them in its alphabet
182-
initContainer.Args[1] += fmt.Sprintf(`scriptfile="%s"
155+
c.Command = command
156+
// Append existing args field to end of derived args
157+
args = append(args, c.Args...)
158+
c.Args = args
159+
} else {
160+
// Only encode the script for linux scripts
161+
// The decode-script subcommand of the entrypoint does not work under windows
162+
script = encodeScript(script)
163+
heredoc := "_EOF_" // underscores because base64 doesn't include them in its alphabet
164+
initContainer.Args[1] += fmt.Sprintf(`scriptfile="%s"
183165
touch ${scriptfile} && chmod +x ${scriptfile}
184166
cat > ${scriptfile} << '%s'
185167
%s
186168
%s
187169
/tekton/bin/entrypoint decode-script "${scriptfile}"
188170
`, scriptFile, heredoc, script, heredoc)
189171

190-
// Set the command to execute the correct script in the mounted
191-
// volume.
192-
// A previous merge with stepTemplate may have populated
193-
// Command and Args, even though this is not normally valid, so
194-
// we'll clear out the Args and overwrite Command.
195-
steps[i].Command = []string{scriptFile}
196-
}
197-
steps[i].VolumeMounts = append(steps[i].VolumeMounts, scriptsVolumeMount)
172+
// Set the command to execute the correct script in the mounted volume.
173+
// A previous merge with stepTemplate may have populated
174+
// Command and Args, even though this is not normally valid, so
175+
// we'll clear out the Args and overwrite Command.
176+
c.Command = []string{scriptFile}
177+
}
178+
c.VolumeMounts = append(c.VolumeMounts, scriptsVolumeMount)
179+
}
198180

199-
containers = append(containers, *steps[i].ToK8sContainer())
181+
// placeDebugScriptInContainers inserts debug scripts into containers. It capsules those scripts to files in intiContainer,
182+
// then execute those scripts in target containers.
183+
func placeDebugScriptInContainers(containers []corev1.Container, initContainer *corev1.Container) {
184+
for i := 0; i < len(containers); i++ {
185+
debugInfoVolumeMount := corev1.VolumeMount{
186+
Name: debugInfoVolumeName,
187+
MountPath: filepath.Join(debugInfoDir, fmt.Sprintf("%d", i)),
188+
}
189+
(&containers[i]).VolumeMounts = append((&containers[i]).VolumeMounts, debugScriptsVolumeMount, debugInfoVolumeMount)
200190
}
201191

202-
// Place debug scripts if breakpoints are enabled
203-
if len(breakpoints) > 0 {
204-
// If breakpoint is not nil then should add the init container
205-
// to write debug script files
206-
*placeScripts = true
192+
type script struct {
193+
name string
194+
content string
195+
}
196+
debugScripts := []script{{
197+
name: "continue",
198+
content: defaultScriptPreamble + fmt.Sprintf(debugContinueScriptTemplate, len(containers), debugInfoDir, RunDir),
199+
}, {
200+
name: "fail-continue",
201+
content: defaultScriptPreamble + fmt.Sprintf(debugFailScriptTemplate, len(containers), debugInfoDir, RunDir),
202+
}}
203+
204+
// Add debug or breakpoint related scripts to /tekton/debug/scripts
205+
// Iterate through the debugScripts and add routine for each of them in the initContainer for their creation
206+
for _, debugScript := range debugScripts {
207+
tmpFile := filepath.Join(debugScriptsDir, fmt.Sprintf("%s-%s", "debug", debugScript.name))
208+
heredoc := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("%s-%s-heredoc-randomly-generated", "debug", debugScript.name))
209+
210+
initContainer.Args[1] += fmt.Sprintf(initScriptDirective, tmpFile, heredoc, debugScript.content, heredoc)
211+
}
212+
}
207213

208-
type script struct {
209-
name string
210-
content string
211-
}
212-
debugScripts := []script{{
213-
name: "continue",
214-
content: defaultScriptPreamble + fmt.Sprintf(debugContinueScriptTemplate, len(steps), debugInfoDir, RunDir),
215-
}, {
216-
name: "fail-continue",
217-
content: defaultScriptPreamble + fmt.Sprintf(debugFailScriptTemplate, len(steps), debugInfoDir, RunDir),
218-
}}
219-
220-
// Add debug or breakpoint related scripts to /tekton/debug/scripts
221-
// Iterate through the debugScripts and add routine for each of them in the initContainer for their creation
222-
for _, debugScript := range debugScripts {
223-
tmpFile := filepath.Join(debugScriptsDir, fmt.Sprintf("%s-%s", "debug", debugScript.name))
224-
heredoc := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("%s-%s-heredoc-randomly-generated", "debug", debugScript.name))
225-
226-
initContainer.Args[1] += fmt.Sprintf(initScriptDirective, tmpFile, heredoc, debugScript.content, heredoc)
214+
// convertListOfSidecars does the heavy lifting for convertScripts.
215+
//
216+
// It iterates through the list of sidecars, generates the script file name and heredoc termination string,
217+
// adds an entry to the init container args, sets up the step container to run the script, and sets the volume mounts.
218+
func convertListOfSidecars(sidecars []v1beta1.Sidecar, initContainer *corev1.Container, namePrefix string) []corev1.Container {
219+
containers := []corev1.Container{}
220+
for i, s := range sidecars {
221+
c := s.ToK8sContainer()
222+
if s.Script != "" {
223+
placeScriptInContainer(s.Script, getScriptFile(scriptsDir, fmt.Sprintf("%s-%d", namePrefix, i)), c, initContainer)
227224
}
225+
containers = append(containers, *c)
228226
}
227+
return containers
228+
}
229229

230+
// convertListOfSteps does the heavy lifting for convertScripts.
231+
//
232+
// It iterates through the list of steps, generates the script file name and heredoc termination string,
233+
// adds an entry to the init container args, sets up the step container to run the script, and sets the volume mounts.
234+
func convertListOfSteps(steps []v1beta1.Step, initContainer *corev1.Container, breakpoints []string, namePrefix string) []corev1.Container {
235+
containers := []corev1.Container{}
236+
for i, s := range steps {
237+
c := steps[i].ToK8sContainer()
238+
if s.Script != "" {
239+
placeScriptInContainer(s.Script, getScriptFile(scriptsDir, fmt.Sprintf("%s-%d", namePrefix, i)), c, initContainer)
240+
}
241+
containers = append(containers, *c)
242+
}
243+
if len(breakpoints) > 0 {
244+
placeDebugScriptInContainers(containers, initContainer)
245+
}
230246
return containers
231247
}
232248

249+
func getScriptFile(scriptsDir, scriptName string) string {
250+
return filepath.Join(scriptsDir, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(scriptName))
251+
}
252+
233253
// encodeScript encodes a script field into a format that avoids kubernetes' built-in processing of container args,
234254
// which can mangle dollar signs and unexpectedly replace variable references in the user's script.
235255
func encodeScript(script string) string {

0 commit comments

Comments
 (0)