-
Notifications
You must be signed in to change notification settings - Fork 14
/
image_pull.go
61 lines (53 loc) · 1.19 KB
/
image_pull.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
package common
import (
"context"
"time"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/log"
"github.com/cybozu-go/well"
)
type imagePullCommand struct {
nodes []*cke.Node
img cke.Image
}
const (
pullMaxRetry = 3
pullWaitDuration = 10 * time.Second
)
// ImagePullCommand returns a Commander to pull an image on nodes.
func ImagePullCommand(nodes []*cke.Node, img cke.Image) cke.Commander {
return imagePullCommand{nodes, img}
}
func (c imagePullCommand) Run(ctx context.Context, inf cke.Infrastructure, _ string) error {
env := well.NewEnvironment(ctx)
for _, n := range c.nodes {
ce := inf.Engine(n.Address)
env.Go(func(ctx context.Context) error {
var err error
for i := 0; i < pullMaxRetry; i++ {
err = ce.PullImage(c.img)
if err == nil {
return nil
}
log.Warn("failed to pull image", map[string]interface{}{
"image": c.img.Name(),
log.FnError: err,
})
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(pullWaitDuration):
}
}
return err
})
}
env.Stop()
return env.Wait()
}
func (c imagePullCommand) Command() cke.Command {
return cke.Command{
Name: "image-pull",
Target: c.img.Name(),
}
}