-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
script.go
310 lines (279 loc) · 11.4 KB
/
script.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pod
import (
"encoding/base64"
"fmt"
"path/filepath"
"strconv"
"strings"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/names"
corev1 "k8s.io/api/core/v1"
)
const (
scriptsVolumeName = "tekton-internal-scripts"
debugScriptsVolumeName = "tekton-internal-debug-scripts"
debugInfoVolumeName = "tekton-internal-debug-info"
scriptsDir = "/tekton/scripts"
debugScriptsDir = "/tekton/debug/scripts"
defaultScriptPreamble = "#!/bin/sh\nset -e\n"
debugInfoDir = "/tekton/debug/info"
)
var (
// Volume definition attached to Pods generated from TaskRuns that have
// steps that specify a Script.
scriptsVolume = corev1.Volume{
Name: scriptsVolumeName,
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}
scriptsVolumeMount = corev1.VolumeMount{
Name: scriptsVolumeName,
MountPath: scriptsDir,
ReadOnly: true,
}
writeScriptsVolumeMount = corev1.VolumeMount{
Name: scriptsVolumeName,
MountPath: scriptsDir,
ReadOnly: false,
}
debugScriptsVolume = corev1.Volume{
Name: debugScriptsVolumeName,
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}
debugScriptsVolumeMount = corev1.VolumeMount{
Name: debugScriptsVolumeName,
MountPath: debugScriptsDir,
}
debugInfoVolume = corev1.Volume{
Name: debugInfoVolumeName,
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}
)
// convertScripts creates an init container that mounts any Scripts specified by
// the input Steps and Sidecars. It returns the init container, plus two slices of Containers
// representing the Steps and Sidecars, respectively, that use the scripts from the init container.
// Other inputs:
// - shellImageLinux and shellImageWindows: the images that should be used by the init container,
// depending on the OS the Task will run on
// - debugConfig: the TaskRun's debug configuration
// - setSecurityContext: whether the init container should include a security context that will
// allow it to run in a namespace with "restricted" pod security admission
func convertScripts(shellImageLinux string, shellImageWin string, steps []v1.Step, sidecars []v1.Sidecar, debugConfig *v1.TaskRunDebug, setSecurityContext bool) (*corev1.Container, []corev1.Container, []corev1.Container) {
// Place scripts is an init container used for creating scripts in the
// /tekton/scripts directory which would be later used by the step containers
// as a Command
requiresWindows := checkWindowsRequirement(steps, sidecars)
shellImage := shellImageLinux
shellCommand := "sh"
shellArg := "-c"
securityContext := linuxSecurityContext
// Set windows variants for Image, Command and Args
if requiresWindows {
shellImage = shellImageWin
shellCommand = "pwsh"
shellArg = "-Command"
securityContext = windowsSecurityContext
}
placeScriptsInit := corev1.Container{
Name: "place-scripts",
Image: shellImage,
Command: []string{shellCommand},
Args: []string{shellArg, ""},
VolumeMounts: []corev1.VolumeMount{writeScriptsVolumeMount, binMount},
}
if setSecurityContext {
placeScriptsInit.SecurityContext = securityContext
}
// Add mounts for debug
if debugConfig != nil && debugConfig.NeedsDebug() {
placeScriptsInit.VolumeMounts = append(placeScriptsInit.VolumeMounts, debugScriptsVolumeMount)
}
convertedStepContainers := convertListOfSteps(steps, &placeScriptsInit, debugConfig, "script")
sidecarContainers := convertListOfSidecars(sidecars, &placeScriptsInit, "sidecar-script")
if hasScripts(steps, sidecars, debugConfig) {
return &placeScriptsInit, convertedStepContainers, sidecarContainers
}
return nil, convertedStepContainers, sidecarContainers
}
// convertListOfSidecars 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 []v1.Sidecar, initContainer *corev1.Container, namePrefix string) []corev1.Container {
containers := []corev1.Container{}
for i, s := range sidecars {
c := s.ToK8sContainer()
if s.Script != "" {
placeScriptInContainer(s.Script, getScriptFile(scriptsDir, fmt.Sprintf("%s-%d", namePrefix, i)), c, initContainer)
}
containers = append(containers, *c)
}
return containers
}
// convertListOfSteps 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 []v1.Step, initContainer *corev1.Container, debugConfig *v1.TaskRunDebug, namePrefix string) []corev1.Container {
containers := []corev1.Container{}
for i, s := range steps {
c := steps[i].ToK8sContainer()
if s.Script != "" {
placeScriptInContainer(s.Script, getScriptFile(scriptsDir, fmt.Sprintf("%s-%d", namePrefix, i)), c, initContainer)
}
containers = append(containers, *c)
}
if debugConfig != nil && debugConfig.NeedsDebugOnFailure() {
placeDebugScriptInContainers(containers, initContainer)
}
return containers
}
func getScriptFile(scriptsDir, scriptName string) string {
return filepath.Join(scriptsDir, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(scriptName))
}
// 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")
if !hasShebang {
script = defaultScriptPreamble + script
}
// 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)
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.
c.Command = []string{scriptFile}
}
c.VolumeMounts = append(c.VolumeMounts, scriptsVolumeMount)
}
// encodeScript encodes a script field into a format that avoids kubernetes' built-in processing of container args,
// which can mangle dollar signs and unexpectedly replace variable references in the user's script.
func encodeScript(script string) string {
return base64.StdEncoding.EncodeToString([]byte(script))
}
// placeDebugScriptInContainers inserts debug scripts into containers. It capsules those scripts to files in initContainer,
// then executes those scripts in target containers.
func placeDebugScriptInContainers(containers []corev1.Container, initContainer *corev1.Container) {
for i := range len(containers) {
debugInfoVolumeMount := corev1.VolumeMount{
Name: debugInfoVolumeName,
MountPath: filepath.Join(debugInfoDir, strconv.Itoa(i)),
}
(&containers[i]).VolumeMounts = append((&containers[i]).VolumeMounts, debugScriptsVolumeMount, debugInfoVolumeMount)
}
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)
}
}
// hasScripts determines if we need to generate scripts in InitContainer given steps, sidecars and breakpoints.
func hasScripts(steps []v1.Step, sidecars []v1.Sidecar, debugConfig *v1.TaskRunDebug) bool {
for _, s := range steps {
if s.Script != "" {
return true
}
}
for _, s := range sidecars {
if s.Script != "" {
return true
}
}
return debugConfig != nil && debugConfig.NeedsDebug()
}
func checkWindowsRequirement(steps []v1.Step, sidecars []v1.Sidecar) bool {
// Detect windows shebangs
for _, step := range steps {
cleaned := strings.TrimSpace(step.Script)
if strings.HasPrefix(cleaned, "#!win") {
return true
}
}
// If no step needs windows, then check sidecars to be sure
for _, sidecar := range sidecars {
cleaned := strings.TrimSpace(sidecar.Script)
if strings.HasPrefix(cleaned, "#!win") {
return true
}
}
return false
}
func extractWindowsScriptComponents(script string, fileName string) ([]string, []string, string, string) {
// Set the command to execute the correct script in the mounted volume.
shebangLine := strings.Split(script, "\n")[0]
splitLine := strings.Split(shebangLine, " ")
var command, args []string
if len(splitLine) > 1 {
strippedCommand := splitLine[1:]
command = strippedCommand[0:1]
// Handle legacy powershell limitation
if strings.HasPrefix(command[0], "powershell") {
fileName += ".ps1"
}
if len(strippedCommand) > 1 {
args = strippedCommand[1:]
args = append(args, fileName)
} else {
args = []string{fileName}
}
} else {
// If no interpreter is specified then strip the shebang and
// create a .cmd file
fileName += ".cmd"
commandLines := strings.Split(script, "\n")[1:]
script = strings.Join(commandLines, "\n")
command = []string{fileName}
args = []string{}
}
return command, args, script, fileName
}