1
0
mirror of https://github.com/rancher/rke.git synced 2025-04-28 11:36:27 +00:00
rke/dind/dind.go

109 lines
3.6 KiB
Go
Raw Normal View History

2018-07-10 19:21:27 +00:00
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/sirupsen/logrus"
)
const (
DINDImage = "docker:17.03-dind"
DINDContainerPrefix = "rke-dind"
2018-07-10 19:21:27 +00:00
DINDPlane = "dind"
DINDNetwork = "dind-network"
DINDSubnet = "172.18.0.0/16"
)
func StartUpDindContainer(ctx context.Context, dindAddress, dindNetwork string) (string, error) {
2018-07-10 19:21:27 +00:00
cli, err := client.NewEnvClient()
if err != nil {
return "", err
2018-07-10 19:21:27 +00:00
}
// its recommended to use host's storage driver
dockerInfo, err := cli.Info(ctx)
if err != nil {
return "", err
2018-07-10 19:21:27 +00:00
}
storageDriver := dockerInfo.Driver
// Get dind container name
containerName := fmt.Sprintf("%s-%s", DINDContainerPrefix, dindAddress)
2018-07-10 19:21:27 +00:00
_, err = cli.ContainerInspect(ctx, containerName)
if err != nil {
if !client.IsErrNotFound(err) {
return "", err
2018-07-10 19:21:27 +00:00
}
if err := docker.UseLocalOrPull(ctx, cli, cli.DaemonHost(), DINDImage, DINDPlane, nil); err != nil {
return "", err
2018-07-10 19:21:27 +00:00
}
binds := []string{
fmt.Sprintf("/var/lib/kubelet-%s:/var/lib/kubelet:shared", containerName),
"/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,
2018-07-10 19:21:27 +00:00
}
hostCfg := &container.HostConfig{
Privileged: true,
Binds: binds,
}
resp, err := cli.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName)
2018-07-10 19:21:27 +00:00
if err != nil {
return "", fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, cli.DaemonHost(), err)
2018-07-10 19:21:27 +00:00
}
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)
2018-07-10 19:21:27 +00:00
}
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)
2018-07-10 19:21:27 +00:00
}
dindIPAddress := dindContainer.NetworkSettings.IPAddress
2018-07-10 19:21:27 +00:00
logrus.Infof("[%s] container [%s] is already running on host[%s]", DINDPlane, containerName, cli.DaemonHost())
return dindIPAddress, nil
2018-07-10 19:21:27 +00:00
}
func RmoveDindContainer(ctx context.Context, dindAddress string) error {
cli, err := client.NewEnvClient()
if err != nil {
return err
}
containerName := fmt.Sprintf("%s-%s", DINDContainerPrefix, dindAddress)
2018-07-10 19:21:27 +00:00
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
}