forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal.go
386 lines (349 loc) · 9.41 KB
/
local.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
/*
Copyright 2019 The Kubernetes 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 main
import (
"context"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
coreapi "k8s.io/api/core/v1"
prowapi "k8s.io/test-infra/prow/apis/prowjobs/v1"
)
func realPath(p string) (string, error) {
return filepath.Abs(os.ExpandEnv(p))
}
func scanln(ctx context.Context) (string, error) {
ch := make(chan string)
go func() {
defer close(ch)
var out string
fmt.Scanln(&out)
ch <- out
}()
select {
case s := <-ch:
return s, nil
case <-ctx.Done():
os.Stdin.Close()
return "", ctx.Err()
}
}
func readMount(ctx context.Context, mount coreapi.VolumeMount) (string, error) {
fmt.Fprintf(os.Stderr, "local %s path (%q mount): ", mount.MountPath, mount.Name)
out, err := scanln(ctx)
if err != nil {
return "", fmt.Errorf("scan: %v", err)
}
return realPath(out)
}
func volume(pod coreapi.PodSpec, name string) *coreapi.Volume {
for _, v := range pod.Volumes {
if v.Name == name {
return &v
}
}
return nil
}
func pathAlias(r prowapi.Refs) string {
if r.PathAlias == "" {
return fmt.Sprintf("github.com/%s/%s", r.Org, r.Repo)
}
return r.PathAlias
}
func readRepo(ctx context.Context, path string) (string, error) {
wd, err := workingDir()
if err != nil {
return "", fmt.Errorf("workingDir: %v", err)
}
def, err := findRepo(wd, path)
if err != nil {
logrus.WithError(err).WithField("repo", path).Warn("could not find repo")
}
fmt.Fprintf(os.Stderr, "local /path/to/%s", path)
if def != "" {
fmt.Fprintf(os.Stderr, " [%s]", def)
}
fmt.Fprint(os.Stderr, ": ")
out, err := scanln(ctx)
if err != nil {
return "", fmt.Errorf("scan: %v", err)
}
if out == "" {
out = def
}
return realPath(out)
}
func workingDir() (string, error) {
if wd := os.Getenv("BUILD_WORKING_DIRECTORY"); wd != "" {
return wd, nil // running via bazel run
}
return os.Getwd() // running outside bazel
}
// findRepo will attempt to find a repo in logical locations under path.
//
// It will first try to find foo/bar somewhere under $PWD or a $PWD dir.
// AKA if $PWD is /go/src it will match /go/src/foo/bar, /go/foo/bar or /foo/bar
// Next it will look for the basename somewhere under $PWD or a $PWD dir.
// AKA if $PWD is /go/src it will match /go/src/bar, /go/bar or /bar
// If both of these strategies fail it will return an error.
func findRepo(wd, path string) (string, error) {
opwd, err := realPath(wd)
if err != nil {
return "", fmt.Errorf("wd not found: %v", err)
}
if strings.HasPrefix(path, "github.com/kubernetes/") {
path = strings.Replace(path, "github.com/kubernetes/", "k8s.io/", 1)
}
var old string
pwd := opwd
for old != pwd {
old = pwd
if strings.HasSuffix(pwd, "/"+path) {
return pwd, nil
}
pwd = filepath.Dir(pwd)
}
pwd = opwd
for old != pwd {
old = pwd
check := filepath.Join(pwd, path)
if info, err := os.Stat(check); err == nil && info.IsDir() {
return check, nil
}
pwd = filepath.Dir(pwd)
}
base := filepath.Base(path)
pwd = opwd
for old != pwd {
old = pwd
check := filepath.Join(pwd, base)
if info, err := os.Stat(check); err == nil && info.IsDir() {
return check, nil
}
pwd = filepath.Dir(pwd)
}
return "", errors.New("cannot find repo")
}
var baseArgs = []string{"docker", "run", "--rm=true"}
func checkPrivilege(ctx context.Context, cont coreapi.Container, allow bool) (bool, error) {
if cont.SecurityContext == nil {
return false, nil
}
if cont.SecurityContext.Privileged == nil {
return false, nil
}
if !*cont.SecurityContext.Privileged {
return false, nil
}
fmt.Fprint(os.Stderr, "Privileged jobs are unsafe. Remove from local run? [yes]: ")
out, err := scanln(ctx)
if err != nil {
return false, fmt.Errorf("scan: %v", err)
}
if out == "no" || out == "n" {
if !allow {
return false, errors.New("privileged jobs are disallowed")
}
logrus.Warn("DANGER: privileged containers are unsafe security risks. Please refactor")
return true, nil
}
return false, nil
}
func convertToLocal(ctx context.Context, log *logrus.Entry, pj prowapi.ProwJob, name string, allowPrivilege bool) ([]string, error) {
log.Info("Converting job into docker run command...")
var localArgs []string
localArgs = append(localArgs, baseArgs...)
localArgs = append(localArgs, "--name="+name)
container := pj.Spec.PodSpec.Containers[0]
decoration := pj.Spec.DecorationConfig
var entrypoint string
args := container.Command
args = append(args, container.Args...)
if len(args) > 0 && decoration != nil {
entrypoint = args[0]
args = args[1:]
}
if entrypoint == "" && decoration != nil {
return nil, errors.New("decorated jobs must specify command and/or args")
}
if entrypoint != "" {
localArgs = append(localArgs, "--entrypoint="+entrypoint)
}
for _, env := range container.Env {
localArgs = append(localArgs, "-e", env.Name+"="+env.Value)
}
// TODO(fejta): capabilities
priv, err := checkPrivilege(ctx, container, allowPrivilege)
if err != nil {
return nil, err
}
if priv {
localArgs = append(localArgs, "--privileged")
}
if container.Resources.Requests != nil {
// TODO(fejta): https://docs.docker.com/engine/reference/run/#runtime-constraints-on-resources
log.Warn("Ignoring resource requirements")
}
for _, mount := range container.VolumeMounts {
vol := volume(*pj.Spec.PodSpec, mount.Name)
if vol == nil {
return nil, fmt.Errorf("mount %q missing associated volume", mount.Name)
}
if vol.EmptyDir != nil {
localArgs = append(localArgs, "-v", mount.MountPath)
} else {
local, err := readMount(ctx, mount)
if err != nil {
return nil, fmt.Errorf("bad mount %q: %v", mount.Name, err)
}
arg := local + ":" + mount.MountPath
if mount.ReadOnly {
arg += ":ro"
}
localArgs = append(localArgs, "-v", arg)
}
}
var workingDir string
if decoration != nil {
var refs []prowapi.Refs
if pj.Spec.Refs != nil {
refs = append(refs, *pj.Spec.Refs)
}
refs = append(refs, pj.Spec.ExtraRefs...)
for _, ref := range refs {
path := pathAlias(ref)
repo, err := readRepo(ctx, path)
if err != nil {
return nil, fmt.Errorf("bad repo(%s): %v", path, err)
}
dest := filepath.Join("/go/src", path)
if workingDir == "" {
workingDir = dest
}
localArgs = append(localArgs, "-v", repo+":"+dest)
}
}
if workingDir == "" {
workingDir = container.WorkingDir
}
if workingDir != "" {
localArgs = append(localArgs, "-v", workingDir, "-w", workingDir)
}
for k, v := range pj.Labels {
localArgs = append(localArgs, "--label="+k+"="+v)
}
localArgs = append(localArgs, "--label=phaino=true")
image := pj.Spec.PodSpec.Containers[0].Image
localArgs = append(localArgs, image)
localArgs = append(localArgs, args...)
return localArgs, nil
}
func printArgs(localArgs []string) {
base := len(baseArgs)
for i, a := range localArgs {
if i < base {
fmt.Printf("%q ", a)
} else {
fmt.Printf("\\\n %q ", a)
}
}
fmt.Println()
}
func start(args []string) (*exec.Cmd, error) {
cmd := exec.Command(args[0], args[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Stdin = os.Stdin
return cmd, cmd.Start()
}
func kill(cid, signal string) error {
cmd := exec.Command("docker", "kill", "--signal="+signal, cid)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
return cmd.Run()
}
var (
nameLock sync.Mutex
nameId int
)
func containerID() string {
nameLock.Lock()
defer nameLock.Unlock()
nameId++
return fmt.Sprintf("phaino-%d-%d", os.Getpid(), nameId)
}
func convertJob(ctx context.Context, log *logrus.Entry, pj prowapi.ProwJob, priv, onlyPrint bool, timeout, grace time.Duration) error {
cid := containerID()
args, err := convertToLocal(ctx, log, pj, cid, priv)
if err != nil {
return fmt.Errorf("convert: %v", err)
}
printArgs(args)
if onlyPrint {
return nil
}
log.Info("Starting job...")
// TODO(fejta): default grace and timeout to the job's decoration_config
if timeout > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
cmd, err := start(args)
if err != nil {
return fmt.Errorf("start: %v", err)
}
log = log.WithField("container", cid)
ch := make(chan error)
go func() {
log.Info("Waiting for job to finish...")
ch <- cmd.Wait()
}()
select {
case err := <-ch:
return err
case <-ctx.Done():
// cancelled
}
if grace < time.Second {
log.WithField("grace", grace).Info("Increasing grace period to the 1s minimum")
grace = time.Second
}
log = log.WithFields(logrus.Fields{
"grace": grace,
"interrupt": ctx.Err(),
})
abort, cancel := context.WithTimeout(context.Background(), grace)
defer cancel()
if err := kill(cid, "SIGINT"); err != nil {
log.WithError(err).Error("Interrupt error")
} else {
log.Warn("Interrupted container...")
}
select {
case err := <-ch:
log.WithError(err).Info("Graceful exit after interrupt")
return err
case <-abort.Done():
}
if err := kill(cid, "SIGKILL"); err != nil {
return fmt.Errorf("kill: %v", err)
}
return fmt.Errorf("grace period expired, aborted: %v", ctx.Err())
}