forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cni_wrap.go
71 lines (58 loc) · 1.74 KB
/
cni_wrap.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
// +build linux freebsd solaris openbsd darwin
package compute
import (
"context"
"fmt"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
)
// This approach was adopted from Weave's weaveproxy component. Proper credit goes to them.
var (
waitProg = "/.r/r"
waitVolume = "rancher-cni"
waitVolumeTarget = "/.r"
)
func modifyForCNI(c *client.Client, container *container.Config, hostConfig *container.HostConfig) error {
if container.Labels[cniWaitLabel] != "true" {
return nil
}
if err := setWaitEntrypoint(c, container); err != nil {
return err
}
return setWaitVolume(hostConfig, waitVolume, waitVolumeTarget, "ro")
}
func setWaitVolume(hostConfig *container.HostConfig, source, target, mode string) error {
var binds []string
for _, bind := range hostConfig.Binds {
s := strings.Split(bind, ":")
if len(s) >= 2 && s[1] == target {
continue
}
binds = append(binds, bind)
}
hostConfig.Binds = append(hostConfig.Binds, fmt.Sprintf("%s:%s:%s", source, target, mode))
return nil
}
func setWaitEntrypoint(c *client.Client, container *container.Config) error {
if len(container.Entrypoint) == 0 {
image, _, err := c.ImageInspectWithRaw(context.Background(), container.Image)
if err != nil {
return err
}
if len(container.Cmd) == 0 {
container.Cmd = image.Config.Cmd
}
if container.Entrypoint == nil {
container.Entrypoint = image.Config.Entrypoint
}
}
if len(container.Entrypoint) == 0 && len(container.Cmd) == 0 {
// No command, just let docker complain about it on start
return nil
}
if len(container.Entrypoint) == 0 || container.Entrypoint[0] != waitProg {
container.Entrypoint = append([]string{waitProg}, container.Entrypoint...)
}
return nil
}