From a4ee4186bd32270a14ee4deb079ff975bf5fd6b6 Mon Sep 17 00:00:00 2001 From: Medya Gh Date: Fri, 14 Feb 2020 01:01:53 -0800 Subject: [PATCH] create docker volumes explictily with a label --- pkg/drivers/kic/kic.go | 2 +- pkg/drivers/kic/oci/oci.go | 36 +++++++++++++++++++++++++++--------- pkg/drivers/kic/oci/types.go | 9 ++++++--- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/pkg/drivers/kic/kic.go b/pkg/drivers/kic/kic.go index bb76fbd5ef..0ecb6a2ade 100644 --- a/pkg/drivers/kic/kic.go +++ b/pkg/drivers/kic/kic.go @@ -65,7 +65,7 @@ func (d *Driver) Create() error { params := oci.CreateParams{ Name: d.NodeConfig.MachineName, Image: d.NodeConfig.ImageDigest, - ClusterLabel: oci.ClusterLabelKey + "=" + d.MachineName, + ClusterLabel: oci.ProfileLabelKey + "=" + d.MachineName, CPUs: strconv.Itoa(d.NodeConfig.CPU), Memory: strconv.Itoa(d.NodeConfig.Memory) + "mb", Envs: d.NodeConfig.Envs, diff --git a/pkg/drivers/kic/oci/oci.go b/pkg/drivers/kic/oci/oci.go index 9442eda7bc..7786b80755 100644 --- a/pkg/drivers/kic/oci/oci.go +++ b/pkg/drivers/kic/oci/oci.go @@ -57,24 +57,28 @@ func CreateContainerNode(p CreateParams) error { "-v", "/lib/modules:/lib/modules:ro", "--hostname", p.Name, // make hostname match container name "--name", p.Name, // ... and set the container name + "--label", fmt.Sprintf("%s=%s", CreatedByLabelKey, "true"), // label the node with the cluster ID "--label", p.ClusterLabel, // label the node with the role ID - "--label", fmt.Sprintf("%s=%s", nodeRoleKey, p.Role), - } - - // volume path in minikube home folder to mount to /var - hostVarVolPath := filepath.Join(localpath.MiniPath(), "machines", p.Name, "var") - if err := os.MkdirAll(hostVarVolPath, 0711); err != nil { - return errors.Wrapf(err, "create var dir %s", hostVarVolPath) + "--label", fmt.Sprintf("%s=%s", nodeRoleLabelKey, p.Role), } if p.OCIBinary == Podman { // enable execing in /var + // volume path in minikube home folder to mount to /var + hostVarVolPath := filepath.Join(localpath.MiniPath(), "machines", p.Name, "var") + if err := os.MkdirAll(hostVarVolPath, 0711); err != nil { + return errors.Wrapf(err, "create var dir %s", hostVarVolPath) + } // podman mounts var/lib with no-exec by default https://github.com/containers/libpod/issues/5103 runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var:exec", hostVarVolPath)) } if p.OCIBinary == Docker { - runArgs = append(runArgs, "--volume", "/var") + if err := createDockerVolume(p.Name); err != nil { + return errors.Wrapf(err, "creating volume for %s container", p.Name) + } + glog.Infof("Successfully created a docker volume %s", p.Name) + runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var", p.Name)) // setting resource limit in privileged mode is only supported by docker // podman error: "Error: invalid configuration, cannot set resources with rootless containers not using cgroups v2 unified mode" runArgs = append(runArgs, fmt.Sprintf("--cpus=%s", p.CPUs), fmt.Sprintf("--memory=%s", p.Memory)) @@ -109,6 +113,20 @@ func CreateContainerNode(p CreateParams) error { return nil } +// createDockerVolume creates a docker volume to be attached to the container with correct labels and prefixes based on profile name +// Caution ! if volume already exists does NOT return an error and will not apply the minikube labels on it. +// TODO: this should be fixed as a part of https://github.com/kubernetes/minikube/issues/6530 +func createDockerVolume(name string) error { + if err := PointToHostDockerDaemon(); err != nil { + return errors.Wrap(err, "point host docker-daemon") + } + cmd := exec.Command(Docker, "volume", "create", name, "--label", "name.minikube.sigs.k8s.io="+name, "--label", "craeted_by_minikube.minikube.sigs.k8s.io=true") + if out, err := cmd.CombinedOutput(); err != nil { + return errors.Wrapf(err, "output %s", string(out)) + } + return nil +} + // CreateContainer creates a container with "docker/podman run" func createContainer(ociBinary string, image string, opts ...createOpt) ([]string, error) { if err := PointToHostDockerDaemon(); err != nil { @@ -264,7 +282,7 @@ func ContainerID(ociBinary string, nameOrID string) (string, error) { // ListOwnedContainers lists all the containres that kic driver created on user's machine using a label func ListOwnedContainers(ociBinary string) ([]string, error) { - return listContainersByLabel(ociBinary, ClusterLabelKey) + return listContainersByLabel(ociBinary, ProfileLabelKey) } // inspect return low-level information on containers diff --git a/pkg/drivers/kic/oci/types.go b/pkg/drivers/kic/oci/types.go index 70ffbe5dbb..ffc71f57c0 100644 --- a/pkg/drivers/kic/oci/types.go +++ b/pkg/drivers/kic/oci/types.go @@ -21,12 +21,15 @@ const ( DefaultBindIPV4 = "127.0.0.1" Docker = "docker" Podman = "podman" - // ClusterLabelKey is applied to each node docker container for identification - ClusterLabelKey = "io.x-k8s.kic.cluster" + // ProfileLabelKey is applied to any container or volume created by a specific minikube profile name.minikube.sigs.k8s.io=PROFILE_NAME + ProfileLabelKey = "name.minikube.sigs.k8s.io" // NodeRoleKey is used to identify if it is control plane or worker - nodeRoleKey = "io.k8s.sigs.kic.role" + nodeRoleLabelKey = "role.minikube.sigs.k8s.io" + // CreatedByLabelKey is applied to any container/volume that is created by minikube created_by.minikube.sigs.k8s.io=true + CreatedByLabelKey = "created_by.minikube.sigs.k8s.io" ) +// CreateParams are parameters needed to create a container type CreateParams struct { Name string // used for container name and hostname Image string // container image to use to create the node.