-
Notifications
You must be signed in to change notification settings - Fork 566
/
driver.go
629 lines (568 loc) · 21.7 KB
/
driver.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package driver
import (
"bufio"
"context"
"fmt"
"net/url"
"os"
"os/exec"
"os/user"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/pachyderm/pachyderm/v2/src/internal/client"
col "github.com/pachyderm/pachyderm/v2/src/internal/collection"
"github.com/pachyderm/pachyderm/v2/src/internal/dbutil"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/pachsql"
"github.com/pachyderm/pachyderm/v2/src/internal/ppsdb"
"github.com/pachyderm/pachyderm/v2/src/internal/ppsutil"
"github.com/pachyderm/pachyderm/v2/src/internal/proc"
"github.com/pachyderm/pachyderm/v2/src/internal/serviceenv"
"github.com/pachyderm/pachyderm/v2/src/internal/task"
"github.com/pachyderm/pachyderm/v2/src/pfs"
"github.com/pachyderm/pachyderm/v2/src/pps"
"github.com/pachyderm/pachyderm/v2/src/server/worker/common"
"github.com/pachyderm/pachyderm/v2/src/server/worker/logs"
)
// TODO(2.0 optional):
// Prometheus stats? (refer to old driver code and tests)
// capture logs (refer to old driver code and tests).
// In general, need to spend some time walking through the old driver
// tests to see what can be reused.
func PreprocessingTaskNamespace(pipelineInfo *pps.PipelineInfo) string {
namespace := "preprocessing"
if pipelineInfo != nil {
pipeline := fmt.Sprintf("pipeline-%s", url.QueryEscape(ppsdb.VersionKey(pipelineInfo.Pipeline, pipelineInfo.Version)))
namespace = path.Join(namespace, pipeline)
}
return namespace
}
func ProcessingTaskNamespace(pipelineInfo *pps.PipelineInfo) string {
return fmt.Sprintf("processing/pipeline-%s", url.QueryEscape(ppsdb.VersionKey(pipelineInfo.Pipeline, pipelineInfo.Version)))
}
// Driver provides an interface for common functions needed by worker code, and
// captures the relevant objects necessary to provide these functions so that
// users do not need to keep track of as many variables. In addition, this
// interface can be used to mock out external calls to make unit-testing
// simpler.
type Driver interface {
Jobs() col.PostgresCollection
Pipelines() col.PostgresCollection
NewTaskSource() task.Source
NewPreprocessingTaskDoer(string, task.Cache) task.Doer
NewProcessingTaskDoer(string, task.Cache) task.Doer
// Returns the PipelineInfo for the pipeline that this worker belongs to
PipelineInfo() *pps.PipelineInfo
// Returns the kubernetes namespace that the worker is deployed in
Namespace() string
// Returns the path that will contain the input filesets for the job
InputDir() string
// Returns the pachd API client for the driver
PachClient() *client.APIClient
// Returns the number of workers to be used
ExpectedNumWorkers() (int64, error)
// WithContext clones the current driver and applies the context to its
// pachClient. The pachClient context will be used for other blocking
// operations as well.
WithContext(context.Context) Driver
// WithActiveData swaps the given scratch directory into the 'active' input
// directory used when running user code. This also locks a mutex so that no
// two datums can be active concurrently.
WithActiveData([]*common.Input, string, func() error) error
// UserCodeEnv returns the set of environment variables to construct when
// launching the configured user process.
UserCodeEnv(string, *pfs.Commit, []*common.Input, string) []string
RunUserCode(context.Context, logs.TaggedLogger, []string) error
RunUserErrorHandlingCode(context.Context, logs.TaggedLogger, []string) error
// TODO: provide a more generic interface for modifying jobs, and
// some quality-of-life functions for common operations.
DeleteJob(context.Context, *pachsql.Tx, *pps.JobInfo) error
UpdateJobState(*pps.Job, pps.JobState, string) error
GetJobInfo(job *pps.Job) (*pps.JobInfo, error)
// TODO: figure out how to not expose this - currently only used for a few
// operations in the map spawner
NewSQLTx(func(context.Context, *pachsql.Tx) error) error
// Returns the image ID associated with a container running in the worker pod
GetContainerImageID(context.Context, string) (string, error)
}
type driver struct {
env serviceenv.ServiceEnv
ctx context.Context
pachClient *client.APIClient
pipelineInfo *pps.PipelineInfo
activeDataMutex *sync.Mutex
jobs col.PostgresCollection
pipelines col.PostgresCollection
// User and group IDs used for running user code, determined in the constructor
uid *uint32
gid *uint32
// The root directory to use when setting the user code path. This is normally
// "/", but can be overridden by tests.
rootDir string
// The directory to store input data - this is typically static but can be
// overridden by tests.
inputDir string
}
// NewDriver constructs a Driver object using the given clients and pipeline
// settings. It makes blocking calls to determine the user/group to use with
// the user code on the current worker node, as well as determining if
// enterprise features are activated (for exporting stats).
func NewDriver(
ctx context.Context,
env serviceenv.ServiceEnv,
pachClient *client.APIClient,
pipelineInfo *pps.PipelineInfo,
rootPath string,
) (Driver, error) {
pfsPath := filepath.Join(rootPath, client.PPSInputPrefix)
if err := os.MkdirAll(pfsPath, 0777); err != nil {
return nil, errors.EnsureStack(err)
}
jobs := ppsdb.Jobs(env.GetDBClient(), env.GetPostgresListener())
pipelines := ppsdb.Pipelines(env.GetDBClient(), env.GetPostgresListener())
result := &driver{
env: env,
ctx: ctx,
pachClient: pachClient,
pipelineInfo: pipelineInfo,
activeDataMutex: &sync.Mutex{},
jobs: jobs,
pipelines: pipelines,
rootDir: rootPath,
inputDir: pfsPath,
}
if pipelineInfo.Details.Transform.User != "" {
user, err := lookupDockerUser(pipelineInfo.Details.Transform.User)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, errors.EnsureStack(err)
}
// If `user` is `nil`, `uid` and `gid` will get set, and we won't
// customize the user that executes the worker process.
if user != nil { // user is nil when err is an os.ErrNotExist is true in which case we use root
uid, err := strconv.ParseUint(user.Uid, 10, 32)
if err != nil {
return nil, errors.EnsureStack(err)
}
uid32 := uint32(uid)
result.uid = &uid32
gid, err := strconv.ParseUint(user.Gid, 10, 32)
if err != nil {
return nil, errors.EnsureStack(err)
}
gid32 := uint32(gid)
result.gid = &gid32
}
}
return result, nil
}
// lookupDockerUser looks up users given the argument to a Dockerfile USER directive.
// According to Docker's docs this directive looks like:
// USER <user>[:<group>] or
// USER <UID>[:<GID>]
func lookupDockerUser(userArg string) (_ *user.User, retErr error) {
userParts := strings.Split(userArg, ":")
userOrUID := userParts[0]
groupOrGID := ""
if len(userParts) > 1 {
groupOrGID = userParts[1]
}
passwd, err := os.Open("/etc/passwd")
if err != nil {
return nil, errors.EnsureStack(err)
}
defer func() {
if err := passwd.Close(); err != nil && retErr == nil {
retErr = errors.EnsureStack(err)
}
}()
scanner := bufio.NewScanner(passwd)
for scanner.Scan() {
text := scanner.Text()
parts := strings.Split(text, ":")
if got, want := len(parts), 6; got < want {
return nil, errors.Errorf("malformed /etc/passwd line %q: got %d fields, want at least %d", text, got, want)
}
if parts[0] == userOrUID || parts[2] == userOrUID {
result := &user.User{
Username: parts[0],
Uid: parts[2],
Gid: parts[3],
Name: parts[4],
HomeDir: parts[5],
}
if groupOrGID != "" {
if parts[0] == userOrUID {
// groupOrGid is a group
group, err := lookupGroup(groupOrGID)
if err != nil {
return nil, errors.Wrapf(err, "lookup group for group %v (for user id %v)", groupOrGID, userOrUID)
}
result.Gid = group.Gid
} else {
// groupOrGid is a gid
result.Gid = groupOrGID
}
}
return result, nil
}
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrapf(err, "scanning /etc/passwd")
}
return nil, errors.Errorf("guess uid from transform user by reading /etc/passwd: user %s not found", userArg)
}
func lookupGroup(group string) (_ *user.Group, retErr error) {
groupFile, err := os.Open("/etc/group")
if err != nil {
return nil, errors.EnsureStack(err)
}
defer func() {
if err := groupFile.Close(); err != nil && retErr == nil {
retErr = errors.EnsureStack(err)
}
}()
scanner := bufio.NewScanner(groupFile)
for scanner.Scan() {
text := scanner.Text()
parts := strings.Split(text, ":")
if got, want := len(parts), 3; got < want {
return nil, errors.Errorf("malformed /etc/group line %q, got %d fields, want at least %d", text, got, want)
}
if parts[0] == group {
return &user.Group{
Gid: parts[2],
Name: parts[0],
}, nil
}
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrapf(err, "scanning /etc/group")
}
return nil, errors.Errorf("guess gid from transform user: group %s not found", group)
}
func (d *driver) WithContext(ctx context.Context) Driver {
result := &driver{}
*result = *d
result.ctx = ctx
return result
}
func (d *driver) Jobs() col.PostgresCollection {
return d.jobs
}
func (d *driver) Pipelines() col.PostgresCollection {
return d.pipelines
}
func (d *driver) NewTaskSource() task.Source {
etcdPrefix := path.Join(d.env.Config().EtcdPrefix, d.env.Config().PPSEtcdPrefix)
taskService := d.env.GetTaskService(etcdPrefix)
return taskService.NewSource(ProcessingTaskNamespace(d.pipelineInfo))
}
func (d *driver) NewPreprocessingTaskDoer(groupID string, cache task.Cache) task.Doer {
etcdPrefix := path.Join(d.env.Config().EtcdPrefix, d.env.Config().PPSEtcdPrefix)
taskService := d.env.GetTaskService(etcdPrefix)
return taskService.NewDoer(PreprocessingTaskNamespace(d.pipelineInfo), groupID, cache)
}
func (d *driver) NewProcessingTaskDoer(groupID string, cache task.Cache) task.Doer {
etcdPrefix := path.Join(d.env.Config().EtcdPrefix, d.env.Config().PPSEtcdPrefix)
taskService := d.env.GetTaskService(etcdPrefix)
return taskService.NewDoer(ProcessingTaskNamespace(d.pipelineInfo), groupID, cache)
}
func (d *driver) ExpectedNumWorkers() (int64, error) {
latestPipelineInfo := &pps.PipelineInfo{}
if err := d.Pipelines().ReadOnly().Get(d.ctx, d.PipelineInfo().SpecCommit, latestPipelineInfo); err != nil {
return 0, errors.EnsureStack(err)
}
numWorkers := latestPipelineInfo.Parallelism
if numWorkers == 0 {
numWorkers = 1
}
return int64(numWorkers), nil
}
func (d *driver) PipelineInfo() *pps.PipelineInfo {
return d.pipelineInfo
}
func (d *driver) Namespace() string {
return d.env.Config().Namespace
}
func (d *driver) InputDir() string {
return d.inputDir
}
func (d *driver) PachClient() *client.APIClient {
return d.pachClient.WithCtx(d.ctx)
}
func (d *driver) NewSQLTx(cb func(context.Context, *pachsql.Tx) error) error {
return dbutil.WithTx(d.ctx, d.env.GetDBClient(), cb)
}
func (d *driver) RunUserCode(
ctx context.Context,
logger logs.TaggedLogger,
environ []string,
) (retErr error) {
logger.Logf("beginning to run user code")
defer func(start time.Time) {
if retErr != nil {
if ctxErr := context.Cause(ctx); ctxErr != nil {
logger.Errf("errored running user code after %v: %v because %v", time.Since(start), retErr, ctxErr)
} else {
logger.Errf("errored running user code after %v: %v", time.Since(start), retErr)
}
} else {
logger.Logf("finished running user code after %v", time.Since(start))
}
}(time.Now())
if len(d.pipelineInfo.Details.Transform.Cmd) == 0 {
return errors.New("invalid pipeline transform, no command specified")
}
// Run user code
cmd := exec.CommandContext(ctx, d.pipelineInfo.Details.Transform.Cmd[0], d.pipelineInfo.Details.Transform.Cmd[1:]...)
if d.pipelineInfo.Details.Transform.Stdin != nil {
cmd.Stdin = strings.NewReader(strings.Join(d.pipelineInfo.Details.Transform.Stdin, "\n") + "\n")
}
stdout, stderr := logger.WithUserCode().Writer("stdout"), logger.WithUserCode().Writer("stderr")
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Env = environ
cmd.SysProcAttr = makeSysProcAttr(d.uid, d.gid)
// By default PWD will be the working dir for the container, so we don't need to set Dir explicitly.
// If the pipeline or worker config explicitly sets the value, then override the container working dir.
if d.pipelineInfo.Details.Transform.WorkingDir != "" || d.rootDir != "/" {
cmd.Dir = filepath.Join(d.rootDir, d.pipelineInfo.Details.Transform.WorkingDir)
}
err := cmd.Start()
if err != nil {
return errors.EnsureStack(err)
}
monctx, endMonitoring := context.WithCancelCause(ctx)
go proc.MonitorProcessGroup(logger.Context(monctx), cmd.Process.Pid)
killChildren := makeProcessGroupKiller(ctx, logger, cmd.Process.Pid)
if ok, err := blockUntilWaitable(cmd.Process.Pid); ok {
// Since cmd.Process.Pid is dead, we can kill its children now.
killChildren()
} else if err != nil {
// The error can occur before cmd.Process.Pid exits, so it's not safe to kill the
// children yet. Because of this error, we lose the ability to kill subprocesses
// that the user code might have spawned, and those children can hold onto stdout
// and prevent Wait from ever returning. This log message will indicate the
// presence of that problem.
logger.Logf("blockUntilWaitable: %v", err)
}
err = cmd.Wait()
killChildren()
stdout.Close()
stderr.Close()
endMonitoring(errors.New("child exited"))
// Print final rusage metrics.
printRusage(logger.Context(ctx), cmd.ProcessState)
// We ignore broken pipe errors, these occur very occasionally if a user
// specifies Stdin but their process doesn't actually read everything from
// Stdin. This is a fairly common thing to do, bash by default ignores
// broken pipe errors.
if err != nil && !strings.Contains(err.Error(), "broken pipe") {
// (if err is an acceptable return code, don't return err)
exiterr := &exec.ExitError{}
if errors.As(err, &exiterr) {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
for _, returnCode := range d.pipelineInfo.Details.Transform.AcceptReturnCode {
if int(returnCode) == status.ExitStatus() {
return nil
}
}
}
}
return errors.EnsureStack(err)
}
return nil
}
func (d *driver) RunUserErrorHandlingCode(
ctx context.Context,
logger logs.TaggedLogger,
environ []string,
) (retErr error) {
logger.Logf("beginning to run user error handling code")
defer func(start time.Time) {
if retErr != nil {
if ctxErr := context.Cause(ctx); ctxErr != nil {
logger.Errf("errored running user error handling code after %v: %v because %v", time.Since(start), retErr, ctxErr)
} else {
logger.Errf("errored running user error handling code after %v: %v", time.Since(start), retErr)
}
} else {
logger.Logf("finished running user error handling code after %v", time.Since(start))
}
}(time.Now())
cmd := exec.CommandContext(ctx, d.pipelineInfo.Details.Transform.ErrCmd[0], d.pipelineInfo.Details.Transform.ErrCmd[1:]...)
if d.pipelineInfo.Details.Transform.ErrStdin != nil {
cmd.Stdin = strings.NewReader(strings.Join(d.pipelineInfo.Details.Transform.ErrStdin, "\n") + "\n")
}
stdout, stderr := logger.WithUserCode().Writer("stdout"), logger.WithUserCode().Writer("stderr")
cmd.Stdout = stdout
cmd.Stderr = stderr
cmd.Env = environ
cmd.SysProcAttr = makeSysProcAttr(d.uid, d.gid)
cmd.Dir = d.pipelineInfo.Details.Transform.WorkingDir
err := cmd.Start()
if err != nil {
return errors.EnsureStack(err)
}
monctx, endMonitoring := context.WithCancelCause(ctx)
go proc.MonitorProcessGroup(logger.Context(monctx), cmd.Process.Pid)
killChildren := makeProcessGroupKiller(ctx, logger, cmd.Process.Pid)
if ok, err := blockUntilWaitable(cmd.Process.Pid); ok {
// Since cmd.Process.Pid is dead, we can kill its children now.
killChildren()
} else if err != nil {
// The error can occur before cmd.Process.Pid exits, so it's not safe to kill the
// children yet. Because of this error, we lose the ability to kill subprocesses
// that the user code might have spawned, and those children can hold onto stdout
// and prevent Wait from ever returning. This log message will indicate the
// presence of that problem.
logger.Logf("blockUntilWaitable: %v", err)
}
err = cmd.Wait()
killChildren()
stdout.Close()
stderr.Close()
endMonitoring(errors.New("child exited"))
// Print final rusage metrics.
printRusage(logger.Context(ctx), cmd.ProcessState)
// We ignore broken pipe errors, these occur very occasionally if a user
// specifies Stdin but their process doesn't actually read everything from
// Stdin. This is a fairly common thing to do, bash by default ignores
// broken pipe errors.
if err != nil && !strings.Contains(err.Error(), "broken pipe") {
// (if err is an acceptable return code, don't return err)
exiterr := &exec.ExitError{}
if errors.As(err, &exiterr) {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
for _, returnCode := range d.pipelineInfo.Details.Transform.AcceptReturnCode {
if int(returnCode) == status.ExitStatus() {
return nil
}
}
}
}
return errors.EnsureStack(err)
}
return nil
}
func (d *driver) UpdateJobState(job *pps.Job, state pps.JobState, reason string) error {
return d.NewSQLTx(func(ctx context.Context, sqlTx *pachsql.Tx) error {
jobInfo := &pps.JobInfo{}
if err := d.Jobs().ReadWrite(sqlTx).Get(ctx, ppsdb.JobKey(job), jobInfo); err != nil {
return errors.EnsureStack(err)
}
return errors.EnsureStack(ppsutil.UpdateJobState(ctx, d.Pipelines().ReadWrite(sqlTx), d.Jobs().ReadWrite(sqlTx), jobInfo, state, reason))
})
}
func (d *driver) GetJobInfo(job *pps.Job) (*pps.JobInfo, error) {
jobInfo := &pps.JobInfo{}
if err := d.Jobs().ReadOnly().Get(d.ctx, ppsdb.JobKey(job), jobInfo); err != nil {
return nil, errors.EnsureStack(err)
}
return jobInfo, nil
}
// DeleteJob is identical to updateJobState, except that jobInfo points to a job
// that should be deleted rather than marked failed. Jobs may be deleted if
// their output commit is deleted.
func (d *driver) DeleteJob(ctx context.Context, sqlTx *pachsql.Tx, jobInfo *pps.JobInfo) error {
return errors.EnsureStack(d.Jobs().ReadWrite(sqlTx).Delete(ctx, ppsdb.JobKey(jobInfo.Job)))
}
func (d *driver) unlinkData(inputs []*common.Input) error {
entries, err := os.ReadDir(d.InputDir())
if err != nil {
return errors.EnsureStack(err)
}
for _, entry := range entries {
if entry.Name() == client.PPSScratchSpace {
continue // don't delete scratch space
}
if err := os.RemoveAll(filepath.Join(d.InputDir(), entry.Name())); err != nil {
return errors.EnsureStack(err)
}
}
return nil
}
func (d *driver) UserCodeEnv(
jobID string,
outputCommit *pfs.Commit,
inputs []*common.Input,
pachToken string,
) []string {
result := os.Environ()
for _, input := range inputs {
result = append(result, fmt.Sprintf("%s=%s", input.Name, filepath.Join(d.InputDir(), input.Name, input.FileInfo.File.Path)))
result = append(result, fmt.Sprintf("%s_COMMIT=%s", input.Name, input.FileInfo.File.Commit.Id))
if input.JoinOn != "" {
result = append(result, fmt.Sprintf("PACH_DATUM_%s_JOIN_ON=%s", input.Name, input.JoinOn))
}
if input.GroupBy != "" {
result = append(result, fmt.Sprintf("PACH_DATUM_%s_GROUP_BY=%s", input.Name, input.GroupBy))
}
}
result = append(result, fmt.Sprintf("%s=%s", client.DatumIDEnv, common.DatumID(inputs)))
if jobID != "" {
result = append(result, fmt.Sprintf("%s=%s", client.JobIDEnv, jobID))
pipeline := &pps.Pipeline{
Project: outputCommit.Repo.Project,
Name: outputCommit.Repo.Name,
}
if ppsutil.ContainsS3Inputs(d.PipelineInfo().Details.Input) || d.PipelineInfo().Details.S3Out {
// TODO(msteffen) Instead of reading S3GATEWAY_PORT directly, worker/main.go
// should pass its ServiceEnv to worker.NewAPIServer, which should store it
// in 'a'. However, requiring worker.APIServer to have a ServiceEnv would
// break the worker.APIServer initialization in newTestAPIServer (in
// worker/worker_test.go), which uses mock clients but has no good way to
// mock a ServiceEnv. Once we can create mock ServiceEnvs, we should store
// a ServiceEnv in worker.APIServer, rewrite newTestAPIServer and
// NewAPIServer, and then change this code.
result = append(
result,
fmt.Sprintf("S3_ENDPOINT=http://%s.%s:%s",
ppsutil.SidecarS3GatewayService(pipeline, jobID),
d.Namespace(),
os.Getenv("S3GATEWAY_PORT"),
),
)
// Set AWS_... creds vars in addition to PACH_PIPELINE_TOKEN so that any
// S3 clients running in the user code use these and successfully connect
// by default
if pachToken != "" {
result = append(result, "AWS_ACCESS_KEY_ID="+pachToken)
result = append(result, "AWS_SECRET_ACCESS_KEY="+pachToken)
} else {
// If auth is off, clients can use any creds with Pachyderm's S3
// gateway, as long as the ID and secret match. However, many clients
// (e.g. the AWS cli) require _some_ nonempty creds; this default value
// allows those clients to work in pipelines if Pachyderm auth is off.
result = append(result, "AWS_ACCESS_KEY_ID=default")
result = append(result, "AWS_SECRET_ACCESS_KEY=default")
}
}
}
if pachToken != "" {
result = append(result, "PACH_TOKEN="+pachToken)
}
if outputCommit != nil {
result = append(result, fmt.Sprintf("%s=%s", client.OutputCommitIDEnv, outputCommit.Id))
}
return result
}
func (d *driver) GetContainerImageID(ctx context.Context, containerName string) (string, error) {
pod, err := d.env.GetKubeClient().CoreV1().Pods(d.env.Config().Namespace).Get(
ctx,
d.env.Config().WorkerSpecificConfiguration.PodName,
metav1.GetOptions{})
for _, containerStatus := range pod.Status.ContainerStatuses {
if containerStatus.Name == containerName {
imageID := containerStatus.ImageID
return imageID, nil
}
}
return "", errors.Wrapf(err, "failed to get image id for container %s", containerName)
}