Skip to content

Commit

Permalink
Refactor Sidecar Containers Construction If Script Exists
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
XinruZhang committed May 4, 2023
1 parent 09d422c commit 2c49eee
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 134 deletions.
12 changes: 6 additions & 6 deletions pkg/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func TestPodBuild(t *testing.T) {
Image: "busybox",
Command: []string{"sh"},
VolumeMounts: []corev1.VolumeMount{writeScriptsVolumeMount, binMount},
Args: []string{"-c", `scriptfile="/tekton/scripts/sidecar-script-0-9l9zj"
Args: []string{"-c", `scriptfile="/tekton/scripts/sidecar-script-0-mz4c7"
touch ${scriptfile} && chmod +x ${scriptfile}
cat > ${scriptfile} << '_EOF_'
IyEvYmluL3NoCmVjaG8gaGVsbG8gZnJvbSBzaWRlY2Fy
Expand Down Expand Up @@ -610,7 +610,7 @@ _EOF_
}, {
Name: "sidecar-sc-name",
Image: "sidecar-image",
Command: []string{"/tekton/scripts/sidecar-script-0-9l9zj"},
Command: []string{"/tekton/scripts/sidecar-script-0-mz4c7"},
VolumeMounts: []corev1.VolumeMount{scriptsVolumeMount},
}},
Volumes: append(implicitVolumes, scriptsVolume, binVolume, runVolume(0), downwardVolume, corev1.Volume{
Expand Down Expand Up @@ -2068,7 +2068,7 @@ func TestPodBuildwithAlphaAPIEnabled(t *testing.T) {
VolumeMounts: []corev1.VolumeMount{writeScriptsVolumeMount, binMount, debugScriptsVolumeMount},
Args: []string{"-c", `tmpfile="/tekton/debug/scripts/debug-continue"
touch ${tmpfile} && chmod +x ${tmpfile}
cat > ${tmpfile} << 'debug-continue-heredoc-randomly-generated-9l9zj'
cat > ${tmpfile} << 'debug-continue-heredoc-randomly-generated-mz4c7'
#!/bin/sh
set -e
Expand All @@ -2087,10 +2087,10 @@ else
echo "Last step (no. $stepNumber) has already been executed, breakpoint exiting !"
exit 0
fi
debug-continue-heredoc-randomly-generated-9l9zj
debug-continue-heredoc-randomly-generated-mz4c7
tmpfile="/tekton/debug/scripts/debug-fail-continue"
touch ${tmpfile} && chmod +x ${tmpfile}
cat > ${tmpfile} << 'debug-fail-continue-heredoc-randomly-generated-mz4c7'
cat > ${tmpfile} << 'debug-fail-continue-heredoc-randomly-generated-mssqb'
#!/bin/sh
set -e
Expand All @@ -2109,7 +2109,7 @@ else
echo "Last step (no. $stepNumber) has already been executed, breakpoint exiting !"
exit 0
fi
debug-fail-continue-heredoc-randomly-generated-mz4c7
debug-fail-continue-heredoc-randomly-generated-mssqb
`},
}

Expand Down
195 changes: 100 additions & 95 deletions pkg/pod/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/names"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const (
Expand Down Expand Up @@ -96,16 +95,6 @@ func convertScripts(shellImageLinux string, shellImageWin string, steps []v1beta
}

breakpoints := []string{}
sideCarSteps := []v1beta1.Step{}
for _, sidecar := range sidecars {
c := sidecar.ToK8sContainer()
sidecarStep := v1beta1.Step{
Script: sidecar.Script,
Timeout: &metav1.Duration{},
}
sidecarStep.SetContainerFields(*c)
sideCarSteps = append(sideCarSteps, sidecarStep)
}

// Add mounts for debug
if debugConfig != nil && len(debugConfig.Breakpoint) > 0 {
Expand All @@ -116,117 +105,133 @@ func convertScripts(shellImageLinux string, shellImageWin string, steps []v1beta
convertedStepContainers := convertListOfSteps(steps, &placeScriptsInit, &placeScripts, breakpoints, "script")

// Pass empty breakpoint list in "sidecar step to container" converter to not rewrite the scripts and add breakpoints to sidecar
sidecarContainers := convertListOfSteps(sideCarSteps, &placeScriptsInit, &placeScripts, []string{}, "sidecar-script")
sidecarContainers := convertListOfSidecars(sidecars, &placeScriptsInit, &placeScripts, "sidecar-script")
if placeScripts {
return &placeScriptsInit, convertedStepContainers, sidecarContainers
}
return nil, convertedStepContainers, sidecarContainers
}

// convertListOfSteps does the heavy lifting for convertScripts.
//
// It iterates through the list of steps (or sidecars), generates the script file name and heredoc termination string,
// adds an entry to the init container args, sets up the step container to run the script, and sets the volume mounts.
func convertListOfSteps(steps []v1beta1.Step, initContainer *corev1.Container, placeScripts *bool, breakpoints []string, namePrefix string) []corev1.Container {
containers := []corev1.Container{}
for i, s := range steps {
// Add debug mounts if breakpoints are present
if len(breakpoints) > 0 {
debugInfoVolumeMount := corev1.VolumeMount{
Name: debugInfoVolumeName,
MountPath: filepath.Join(debugInfoDir, fmt.Sprintf("%d", i)),
}
steps[i].VolumeMounts = append(steps[i].VolumeMounts, debugScriptsVolumeMount, debugInfoVolumeMount)
}

if s.Script == "" {
// Nothing to convert.
containers = append(containers, *steps[i].ToK8sContainer())
continue
}

// Check for a shebang, and add a default if it's not set.
// The shebang must be the first non-empty line.
cleaned := strings.TrimSpace(s.Script)
hasShebang := strings.HasPrefix(cleaned, "#!")
requiresWindows := strings.HasPrefix(cleaned, "#!win")

script := s.Script
if !hasShebang {
script = defaultScriptPreamble + s.Script
}
// placeScriptInContainer given a piece of script to be executed, placeScriptInContainer firstly modifies initContainer
// so that it capsules the target script into scriptFile, then it modifies the container so that it can execute the scriptFile
// in runtime.
func placeScriptInContainer(script, scriptFile string, c *corev1.Container, initContainer *corev1.Container) {
if script == "" {
return
}
cleaned := strings.TrimSpace(script)
hasShebang := strings.HasPrefix(cleaned, "#!")
requiresWindows := strings.HasPrefix(cleaned, "#!win")

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

// Append to the place-scripts script to place the
// script file in a known location in the scripts volume.
scriptFile := filepath.Join(scriptsDir, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("%s-%d", namePrefix, i)))
if requiresWindows {
command, args, script, scriptFile := extractWindowsScriptComponents(script, scriptFile)
initContainer.Args[1] += fmt.Sprintf(`@"
// Append to the place-scripts script to place the
// script file in a known location in the scripts volume.
if requiresWindows {
command, args, script, scriptFile := extractWindowsScriptComponents(script, scriptFile)
initContainer.Args[1] += fmt.Sprintf(`@"
%s
"@ | Out-File -FilePath %s
`, script, scriptFile)

steps[i].Command = command
// Append existing args field to end of derived args
args = append(args, steps[i].Args...)
steps[i].Args = args
} else {
// Only encode the script for linux scripts
// The decode-script subcommand of the entrypoint does not work under windows
script = encodeScript(script)
heredoc := "_EOF_" // underscores because base64 doesnt include them in its alphabet
initContainer.Args[1] += fmt.Sprintf(`scriptfile="%s"
c.Command = command
// Append existing args field to end of derived args
args = append(args, c.Args...)
c.Args = args
} else {
// Only encode the script for linux scripts
// The decode-script subcommand of the entrypoint does not work under windows
script = encodeScript(script)
heredoc := "_EOF_" // underscores because base64 doesn't include them in its alphabet
initContainer.Args[1] += fmt.Sprintf(`scriptfile="%s"
touch ${scriptfile} && chmod +x ${scriptfile}
cat > ${scriptfile} << '%s'
%s
%s
/tekton/bin/entrypoint decode-script "${scriptfile}"
`, scriptFile, heredoc, script, heredoc)

// Set the command to execute the correct script in the mounted
// volume.
// A previous merge with stepTemplate may have populated
// Command and Args, even though this is not normally valid, so
// we'll clear out the Args and overwrite Command.
steps[i].Command = []string{scriptFile}
}
steps[i].VolumeMounts = append(steps[i].VolumeMounts, scriptsVolumeMount)

containers = append(containers, *steps[i].ToK8sContainer())
// Set the command to execute the correct script in the mounted volume.
// A previous merge with stepTemplate may have populated
// Command and Args, even though this is not normally valid, so
// we'll clear out the Args and overwrite Command.
c.Command = []string{scriptFile}
}
c.VolumeMounts = append(c.VolumeMounts, scriptsVolumeMount)
}

// Place debug scripts if breakpoints are enabled
if len(breakpoints) > 0 {
// If breakpoint is not nil then should add the init container
// to write debug script files
*placeScripts = true

type script struct {
name string
content string
// placeDebugScriptInContainers inserts debug scripts into containers. It capsules those scripts to files in intiContainer,
// then execute those scripts in target containers.
func placeDebugScriptInContainers(containers []corev1.Container, initContainer *corev1.Container) {
for i := 0; i < len(containers); i++ {
debugInfoVolumeMount := corev1.VolumeMount{
Name: debugInfoVolumeName,
MountPath: filepath.Join(debugInfoDir, fmt.Sprintf("%d", i)),
}
debugScripts := []script{{
name: "continue",
content: defaultScriptPreamble + fmt.Sprintf(debugContinueScriptTemplate, len(steps), debugInfoDir, RunDir),
}, {
name: "fail-continue",
content: defaultScriptPreamble + fmt.Sprintf(debugFailScriptTemplate, len(steps), debugInfoDir, RunDir),
}}
(&containers[i]).VolumeMounts = append((&containers[i]).VolumeMounts, debugScriptsVolumeMount, debugInfoVolumeMount)
}

// Add debug or breakpoint related scripts to /tekton/debug/scripts
// Iterate through the debugScripts and add routine for each of them in the initContainer for their creation
for _, debugScript := range debugScripts {
tmpFile := filepath.Join(debugScriptsDir, fmt.Sprintf("%s-%s", "debug", debugScript.name))
heredoc := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("%s-%s-heredoc-randomly-generated", "debug", debugScript.name))
type script struct {
name string
content string
}
debugScripts := []script{{
name: "continue",
content: defaultScriptPreamble + fmt.Sprintf(debugContinueScriptTemplate, len(containers), debugInfoDir, RunDir),
}, {
name: "fail-continue",
content: defaultScriptPreamble + fmt.Sprintf(debugFailScriptTemplate, len(containers), debugInfoDir, RunDir),
}}

// Add debug or breakpoint related scripts to /tekton/debug/scripts
// Iterate through the debugScripts and add routine for each of them in the initContainer for their creation
for _, debugScript := range debugScripts {
tmpFile := filepath.Join(debugScriptsDir, fmt.Sprintf("%s-%s", "debug", debugScript.name))
heredoc := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("%s-%s-heredoc-randomly-generated", "debug", debugScript.name))

initContainer.Args[1] += fmt.Sprintf(initScriptDirective, tmpFile, heredoc, debugScript.content, heredoc)
}
}

initContainer.Args[1] += fmt.Sprintf(initScriptDirective, tmpFile, heredoc, debugScript.content, heredoc)
// convertListOfSidecars does the heavy lifting for convertScripts.
//
// It iterates through the list of sidecars, generates the script file name and heredoc termination string,
// adds an entry to the init container args, sets up the step container to run the script, and sets the volume mounts.
func convertListOfSidecars(sidecars []v1beta1.Sidecar, initContainer *corev1.Container, placeScripts *bool, namePrefix string) []corev1.Container {
containers := []corev1.Container{}
for i, s := range sidecars {
c := s.ToK8sContainer()
scriptFile := filepath.Join(scriptsDir, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("%s-%d", namePrefix, i)))
if s.Script != "" {
*placeScripts = true
placeScriptInContainer(s.Script, scriptFile, c, initContainer)
}
containers = append(containers, *c)
}
return containers
}

// convertListOfSteps does the heavy lifting for convertScripts.
//
// It iterates through the list of steps, generates the script file name and heredoc termination string,
// adds an entry to the init container args, sets up the step container to run the script, and sets the volume mounts.
func convertListOfSteps(steps []v1beta1.Step, initContainer *corev1.Container, placeScripts *bool, breakpoints []string, namePrefix string) []corev1.Container {
containers := []corev1.Container{}
for i, s := range steps {
c := steps[i].ToK8sContainer()
scriptFile := filepath.Join(scriptsDir, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("%s-%d", namePrefix, i)))
if s.Script != "" {
*placeScripts = true
placeScriptInContainer(s.Script, scriptFile, c, initContainer)
}
containers = append(containers, *c)
}
if len(breakpoints) > 0 {
*placeScripts = true
placeDebugScriptInContainers(containers, initContainer)
}
return containers
}

Expand Down
Loading

0 comments on commit 2c49eee

Please sign in to comment.