-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
pod.go
398 lines (353 loc) · 14 KB
/
pod.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
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 resources provides methods to convert a Build CRD to a k8s Pod
// resource.
package resources
import (
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/names"
"github.com/tektoncd/pipeline/pkg/pod"
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun/entrypoint"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
)
const (
workspaceDir = "/workspace"
homeDir = "/builder/home"
taskRunLabelKey = pipeline.GroupName + pipeline.TaskRunLabelKey
ManagedByLabelKey = "app.kubernetes.io/managed-by"
ManagedByLabelValue = "tekton-pipelines"
scriptsDir = "/builder/scripts"
)
// These are effectively const, but Go doesn't have such an annotation.
var (
groupVersionKind = schema.GroupVersionKind{
Group: v1alpha1.SchemeGroupVersion.Group,
Version: v1alpha1.SchemeGroupVersion.Version,
Kind: "TaskRun",
}
emptyVolumeSource = corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{},
}
// These are injected into all of the source/step containers.
implicitEnvVars = []corev1.EnvVar{{
Name: "HOME",
Value: homeDir,
}}
implicitVolumeMounts = []corev1.VolumeMount{{
Name: "workspace",
MountPath: workspaceDir,
}, {
Name: "home",
MountPath: homeDir,
}}
implicitVolumes = []corev1.Volume{{
Name: "workspace",
VolumeSource: emptyVolumeSource,
}, {
Name: "home",
VolumeSource: emptyVolumeSource,
}}
zeroQty = resource.MustParse("0")
// Random byte reader used for pod name generation.
// var for testing.
randReader = rand.Reader
// Volume definition attached to Pods generated from TaskRuns that have
// steps that specify a Script.
scriptsVolume = corev1.Volume{
Name: "place-scripts",
VolumeSource: emptyVolumeSource,
}
scriptsVolumeMount = corev1.VolumeMount{
Name: "place-scripts",
MountPath: scriptsDir,
}
)
const (
// Prefixes to add to the name of the init containers.
containerPrefix = "step-"
unnamedInitContainerPrefix = "step-unnamed-"
ReadyAnnotation = "tekton.dev/ready"
readyAnnotationValue = "READY"
sidecarPrefix = "sidecar-"
)
// MakePod converts TaskRun and TaskSpec objects to a Pod which implements the taskrun specified
// by the supplied CRD.
func MakePod(images pipeline.Images, taskRun *v1alpha1.TaskRun, taskSpec v1alpha1.TaskSpec, kubeclient kubernetes.Interface) (*corev1.Pod, error) {
var initContainers []corev1.Container
var volumes []corev1.Volume
if credsInitContainer, secretsVolumes, err := pod.CredsInit(images.CredsImage, taskRun.GetServiceAccountName(), taskRun.Namespace, kubeclient, implicitVolumeMounts, implicitEnvVars); err != nil {
return nil, err
} else if credsInitContainer != nil {
initContainers = append(initContainers, *credsInitContainer)
volumes = append(volumes, secretsVolumes...)
}
if workingDirInit := pod.WorkingDirInit(images.ShellImage, taskSpec.Steps); workingDirInit != nil {
initContainers = append(initContainers, *workingDirInit)
}
var podSteps []v1alpha1.Step
maxIndicesByResource := findMaxResourceRequest(taskSpec.Steps, corev1.ResourceCPU, corev1.ResourceMemory, corev1.ResourceEphemeralStorage)
placeScripts := false
placeScriptsInitContainer := corev1.Container{
Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("place-scripts"),
Image: images.ShellImage,
TTY: true,
Command: []string{"sh"},
Args: []string{"-c", ""},
VolumeMounts: []corev1.VolumeMount{scriptsVolumeMount},
}
for i, s := range taskSpec.Steps {
s.Env = append(implicitEnvVars, s.Env...)
// TODO(mattmoor): Check that volumeMounts match volumes.
// Add implicit volume mounts, unless the user has requested
// their own volume mount at that path.
requestedVolumeMounts := map[string]bool{}
for _, vm := range s.VolumeMounts {
requestedVolumeMounts[filepath.Clean(vm.MountPath)] = true
}
for _, imp := range implicitVolumeMounts {
if !requestedVolumeMounts[filepath.Clean(imp.MountPath)] {
s.VolumeMounts = append(s.VolumeMounts, imp)
}
}
// If the step specifies a Script, generate and invoke an
// executable script file containing each item in the script.
if s.Script != "" {
placeScripts = true
// Append to the place-scripts script to place the
// script file in a known location in the scripts volume.
tmpFile := filepath.Join(scriptsDir, names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("script-%d", i)))
// heredoc is the "here document" placeholder string
// used to cat script contents into the file. Typically
// this is the string "EOF" but if this value were
// "EOF" it would prevent users from including the
// string "EOF" in their own scripts. Instead we
// randomly generate a string to (hopefully) prevent
// collisions.
heredoc := names.SimpleNameGenerator.RestrictLengthWithRandomSuffix("script-heredoc-randomly-generated")
// NOTE: quotes around the heredoc string are
// important. Without them, ${}s in the file are
// interpreted as env vars and likely end up replaced
// with empty strings. See
// https://stackoverflow.com/a/27921346
placeScriptsInitContainer.Args[1] += fmt.Sprintf(`tmpfile="%s"
touch ${tmpfile} && chmod +x ${tmpfile}
cat > ${tmpfile} << '%s'
%s
%s
`, tmpFile, heredoc, s.Script, heredoc)
// The entrypoint redirecter has already run on this
// step, so we just need to replace the image's
// entrypoint (if any) with the script to run.
// Validation prevents step args from being passed, but
// just to be careful we'll replace any that survived
// entrypoint redirection here.
// TODO(jasonhall): It's confusing that entrypoint
// redirection isn't done as part of MakePod, and the
// interaction of these two modifications to container
// args might be confusing to debug in the future.
s.Args = append(s.Args, tmpFile)
for i := 0; i < len(s.Args); i++ {
if s.Args[i] == "-entrypoint" {
s.Args = append(s.Args[:i+1], tmpFile)
}
}
s.VolumeMounts = append(s.VolumeMounts, scriptsVolumeMount)
}
if s.WorkingDir == "" {
s.WorkingDir = workspaceDir
}
if s.Name == "" {
s.Name = fmt.Sprintf("%v%d", unnamedInitContainerPrefix, i)
} else {
s.Name = names.SimpleNameGenerator.RestrictLength(fmt.Sprintf("%v%v", containerPrefix, s.Name))
}
// use the container name to add the entrypoint binary as an
// init container.
// TODO(jasonhall): Entrypoint init container should be
// explicitly added as an init container, not added to steps
// then pulled out when translating to Pod containers.
if s.Name == names.SimpleNameGenerator.RestrictLength(fmt.Sprintf("%v%v", containerPrefix, entrypoint.InitContainerName)) {
initContainers = append(initContainers, s.Container)
} else {
zeroNonMaxResourceRequests(&s, i, maxIndicesByResource)
podSteps = append(podSteps, s)
}
}
// Add podTemplate Volumes to the explicitly declared use volumes
volumes = append(volumes, taskSpec.Volumes...)
volumes = append(volumes, taskRun.Spec.PodTemplate.Volumes...)
// Add our implicit volumes and any volumes needed for secrets to the explicitly
// declared user volumes.
volumes = append(volumes, implicitVolumes...)
// Add the volume shared to place a script file, if any step specified
// a script.
if placeScripts {
volumes = append(volumes, scriptsVolume)
initContainers = append(initContainers, placeScriptsInitContainer)
}
if err := v1alpha1.ValidateVolumes(volumes); err != nil {
return nil, err
}
// Generate a short random hex string.
b, err := ioutil.ReadAll(io.LimitReader(randReader, 3))
if err != nil {
return nil, err
}
gibberish := hex.EncodeToString(b)
mergedPodSteps, err := v1alpha1.MergeStepsWithStepTemplate(taskSpec.StepTemplate, podSteps)
if err != nil {
return nil, err
}
var mergedPodContainers []corev1.Container
for _, s := range mergedPodSteps {
mergedPodContainers = append(mergedPodContainers, s.Container)
}
for _, sc := range taskSpec.Sidecars {
sc.Name = names.SimpleNameGenerator.RestrictLength(fmt.Sprintf("%v%v", sidecarPrefix, sc.Name))
mergedPodContainers = append(mergedPodContainers, sc)
}
return &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
// We execute the build's pod in the same namespace as where the build was
// created so that it can access colocated resources.
Namespace: taskRun.Namespace,
// Generate a unique name based on the build's name.
// Add a unique suffix to avoid confusion when a build
// is deleted and re-created with the same name.
// We don't use RestrictLengthWithRandomSuffix here because k8s fakes don't support it.
Name: fmt.Sprintf("%s-pod-%s", taskRun.Name, gibberish),
// If our parent TaskRun is deleted, then we should be as well.
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(taskRun, groupVersionKind),
},
Annotations: makeAnnotations(taskRun),
Labels: makeLabels(taskRun),
},
Spec: corev1.PodSpec{
RestartPolicy: corev1.RestartPolicyNever,
InitContainers: initContainers,
Containers: mergedPodContainers,
ServiceAccountName: taskRun.GetServiceAccountName(),
Volumes: volumes,
NodeSelector: taskRun.Spec.PodTemplate.NodeSelector,
Tolerations: taskRun.Spec.PodTemplate.Tolerations,
Affinity: taskRun.Spec.PodTemplate.Affinity,
SecurityContext: taskRun.Spec.PodTemplate.SecurityContext,
RuntimeClassName: taskRun.Spec.PodTemplate.RuntimeClassName,
},
}, nil
}
type UpdatePod func(*corev1.Pod) (*corev1.Pod, error)
// AddReadyAnnotation adds the ready annotation if it is not present.
// Returns any error that comes back from the passed-in update func.
func AddReadyAnnotation(p *corev1.Pod, update UpdatePod) error {
if p.ObjectMeta.Annotations == nil {
p.ObjectMeta.Annotations = make(map[string]string)
}
if p.ObjectMeta.Annotations[ReadyAnnotation] != readyAnnotationValue {
p.ObjectMeta.Annotations[ReadyAnnotation] = readyAnnotationValue
_, err := update(p)
return err
}
return nil
}
func IsContainerStep(name string) bool {
return strings.HasPrefix(name, containerPrefix)
}
func IsContainerSidecar(name string) bool {
return strings.HasPrefix(name, sidecarPrefix)
}
// makeLabels constructs the labels we will propagate from TaskRuns to Pods.
func makeLabels(s *v1alpha1.TaskRun) map[string]string {
labels := make(map[string]string, len(s.ObjectMeta.Labels)+1)
// NB: Set this *before* passing through TaskRun labels. If the TaskRun
// has a managed-by label, it should override this default.
// Copy through the TaskRun's labels to the underlying Pod's.
labels[ManagedByLabelKey] = ManagedByLabelValue
for k, v := range s.ObjectMeta.Labels {
labels[k] = v
}
// NB: Set this *after* passing through TaskRun Labels. If the TaskRun
// specifies this label, it should be overridden by this value.
labels[taskRunLabelKey] = s.Name
return labels
}
// makeAnnotations constructs the annotations we will propagate from TaskRuns to Pods
// and adds any other annotations that will be needed to initialize a Pod.
func makeAnnotations(s *v1alpha1.TaskRun) map[string]string {
annotations := make(map[string]string, len(s.ObjectMeta.Annotations)+1)
for k, v := range s.ObjectMeta.Annotations {
annotations[k] = v
}
annotations[ReadyAnnotation] = ""
return annotations
}
// zeroNonMaxResourceRequests zeroes out the container's cpu, memory, or
// ephemeral storage resource requests if the container does not have the
// largest request out of all containers in the pod. This is done because Tekton
// overwrites each container's entrypoint to make containers effectively execute
// one at a time, so we want pods to only request the maximum resources needed
// at any single point in time. If no container has an explicit resource
// request, all requests are set to 0.
func zeroNonMaxResourceRequests(step *v1alpha1.Step, stepIndex int, maxIndicesByResource map[corev1.ResourceName]int) {
if step.Resources.Requests == nil {
step.Resources.Requests = corev1.ResourceList{}
}
for name, maxIdx := range maxIndicesByResource {
if maxIdx != stepIndex {
step.Resources.Requests[name] = zeroQty
}
}
}
// findMaxResourceRequest returns the index of the container with the maximum
// request for the given resource from among the given set of containers.
func findMaxResourceRequest(steps []v1alpha1.Step, resourceNames ...corev1.ResourceName) map[corev1.ResourceName]int {
maxIdxs := make(map[corev1.ResourceName]int, len(resourceNames))
maxReqs := make(map[corev1.ResourceName]resource.Quantity, len(resourceNames))
for _, name := range resourceNames {
maxIdxs[name] = -1
maxReqs[name] = zeroQty
}
for i, s := range steps {
for _, name := range resourceNames {
maxReq := maxReqs[name]
req, exists := s.Container.Resources.Requests[name]
if exists && req.Cmp(maxReq) > 0 {
maxIdxs[name] = i
maxReqs[name] = req
}
}
}
return maxIdxs
}
// TrimContainerNamePrefix trim the container name prefix to get the corresponding step name
func TrimContainerNamePrefix(containerName string) string {
return strings.TrimPrefix(containerName, containerPrefix)
}
// TrimSidecarNamePrefix trim the sidecar name prefix to get the corresponding
// sidecar name
func TrimSidecarNamePrefix(containerName string) string {
return strings.TrimPrefix(containerName, sidecarPrefix)
}