forked from rancher/rke
/
dind.go
127 lines (119 loc) · 4.2 KB
/
dind.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
package dind
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/rancher/rke/docker"
"github.com/rancher/rke/util"
"github.com/sirupsen/logrus"
)
const (
DINDImage = "docker:17.03-dind"
DINDContainerPrefix = "rke-dind"
DINDPlane = "dind"
DINDNetwork = "dind-network"
DINDSubnet = "172.18.0.0/16"
)
func StartUpDindContainer(ctx context.Context, dindAddress, dindNetwork, dindStorageDriver, dindDNS string) (string, error) {
cli, err := client.NewEnvClient()
if err != nil {
return "", err
}
// its recommended to use host's storage driver
dockerInfo, err := cli.Info(ctx)
if err != nil {
return "", err
}
storageDriver := dindStorageDriver
if len(storageDriver) == 0 {
storageDriver = dockerInfo.Driver
}
// Get dind container name
containerName := fmt.Sprintf("%s-%s", DINDContainerPrefix, dindAddress)
_, err = cli.ContainerInspect(ctx, containerName)
if err != nil {
if !client.IsErrNotFound(err) {
return "", err
}
if err := docker.UseLocalOrPull(ctx, cli, cli.DaemonHost(), DINDImage, DINDPlane, nil); err != nil {
return "", err
}
binds := []string{
fmt.Sprintf("/var/lib/kubelet-%s:/var/lib/kubelet:shared", containerName),
}
isLink, err := util.IsSymlink("/etc/resolv.conf")
if err != nil {
return "", err
}
if isLink {
logrus.Infof("[%s] symlinked [/etc/resolv.conf] file detected. Using [%s] as DNS server.", DINDPlane, dindDNS)
} else {
binds = append(binds, "/etc/resolv.conf:/etc/resolv.conf")
}
imageCfg := &container.Config{
Image: DINDImage,
Entrypoint: []string{
"sh",
"-c",
"mount --make-shared / && " +
"mount --make-shared /var/lib/docker && " +
"dockerd-entrypoint.sh --storage-driver=" + storageDriver,
},
Hostname: dindAddress,
}
hostCfg := &container.HostConfig{
Privileged: true,
Binds: binds,
// this gets ignored if resolv.conf is bind mounted. So it's ok to have it anyway.
DNS: []string{dindDNS},
// Calico needs this
Sysctls: map[string]string{
"net.ipv4.conf.all.rp_filter": "1",
},
}
resp, err := cli.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName)
if err != nil {
return "", fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, cli.DaemonHost(), err)
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return "", fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, cli.DaemonHost(), err)
}
logrus.Infof("[%s] Successfully started [%s] container on host [%s]", DINDPlane, containerName, cli.DaemonHost())
dindContainer, err := cli.ContainerInspect(ctx, containerName)
if err != nil {
return "", fmt.Errorf("Failed to get the address of container [%s] on host [%s]: %v", containerName, cli.DaemonHost(), err)
}
dindIPAddress := dindContainer.NetworkSettings.IPAddress
return dindIPAddress, nil
}
dindContainer, err := cli.ContainerInspect(ctx, containerName)
if err != nil {
return "", fmt.Errorf("Failed to get the address of container [%s] on host [%s]: %v", containerName, cli.DaemonHost(), err)
}
dindIPAddress := dindContainer.NetworkSettings.IPAddress
logrus.Infof("[%s] container [%s] is already running on host[%s]", DINDPlane, containerName, cli.DaemonHost())
return dindIPAddress, nil
}
func RmoveDindContainer(ctx context.Context, dindAddress string) error {
cli, err := client.NewEnvClient()
if err != nil {
return err
}
containerName := fmt.Sprintf("%s-%s", DINDContainerPrefix, dindAddress)
logrus.Infof("[%s] Removing dind container [%s] on host [%s]", DINDPlane, containerName, cli.DaemonHost())
_, err = cli.ContainerInspect(ctx, containerName)
if err != nil {
if !client.IsErrNotFound(err) {
return nil
}
}
if err := cli.ContainerRemove(ctx, containerName, types.ContainerRemoveOptions{
Force: true,
RemoveVolumes: true}); err != nil {
return fmt.Errorf("Failed to remove dind container [%s] on host [%s]: %v", containerName, cli.DaemonHost(), err)
}
logrus.Infof("[%s] Successfully Removed dind container [%s] on host [%s]", DINDPlane, containerName, cli.DaemonHost())
return nil
}