diff --git a/cmd/minikube/cmd/delete.go b/cmd/minikube/cmd/delete.go index 5293c64465..1f2e346341 100644 --- a/cmd/minikube/cmd/delete.go +++ b/cmd/minikube/cmd/delete.go @@ -252,7 +252,7 @@ func deletePossibleKicLeftOver(cname string, driverName string) { } // TODO: move to oci.DeleteContainer? - defaultNetwork := fmt.Sprintf("%s-network", cname) + defaultNetwork := cname err = oci.RemoveNetwork(defaultNetwork) if err != nil { glog.Warningf("error deleting network. :%v", errs) diff --git a/pkg/drivers/kic/kic.go b/pkg/drivers/kic/kic.go index 255e580edb..61b84edc8c 100644 --- a/pkg/drivers/kic/kic.go +++ b/pkg/drivers/kic/kic.go @@ -67,15 +67,6 @@ func NewDriver(c Config) *Driver { // Create a host using the driver's config func (d *Driver) Create() error { - defaultNetwork := fmt.Sprintf("%s-network", d.MachineName) - defaultIPRange := "192.168.39.0/24" - - err := oci.CreateNetwork(defaultNetwork, defaultIPRange) - if err != nil { - // use k8s network? fail? - return err - } - params := oci.CreateParams{ Name: d.NodeConfig.MachineName, Image: d.NodeConfig.ImageDigest, @@ -87,8 +78,13 @@ func (d *Driver) Create() error { ExtraArgs: []string{"--expose", fmt.Sprintf("%d", d.NodeConfig.APIServerPort)}, OCIBinary: d.NodeConfig.OCIBinary, APIServerPort: d.NodeConfig.APIServerPort, - Network: defaultNetwork, - IP: "192.168.39.2", + } + + if err := oci.CreateNetwork(d.MachineName, defaultIPRange); err != nil { + glog.Warningf("unable to create docker network; node ip may not be stable: %v", err) + } else { + params.Network = defaultNetwork + params.IP = "192.168.39.2" } // control plane specific options diff --git a/pkg/drivers/kic/oci/network.go b/pkg/drivers/kic/oci/network.go index 51e0925706..619c77b2a4 100644 --- a/pkg/drivers/kic/oci/network.go +++ b/pkg/drivers/kic/oci/network.go @@ -165,12 +165,15 @@ func dockerContainerIP(name string) (string, string, error) { // CreateNetwork creates a network func CreateNetwork(name, ipRange string) error { - // TODO: validate if exist? + // check if the network already exists + if networkExists(name) { + return nil + } // TODO: subnet conflict // TODO: configure gateway explictly subnet := fmt.Sprintf("--subnet=%s", ipRange) - _, err := runCmd(exec.Command(Docker, "network", "create", "--driver=bridge", subnet, name)) + _, err = runCmd(exec.Command(Docker, "network", "create", "--driver=bridge", subnet, name)) if err != nil { return errors.Wrapf(err, "error creating network") } @@ -180,12 +183,23 @@ func CreateNetwork(name, ipRange string) error { // RemoveNetwork removes a network func RemoveNetwork(name string) error { - // TODO: check if exist? - - _, err := runCmd(exec.Command(Docker, "network", "remove", name)) - if err != nil { - return errors.Wrapf(err, "error removing network") + if !networkExists { + return nil } - - return nil + _, err := runCmd(exec.Command(Docker, "network", "remove", name)) + return err +} + +func networkExists(name string) bool { + rr, err := runCmd(exec.Command(Docker, "network", "ls", "--format", "{{.Name}}")) + if err != nil { + return errors.Wrap(err, "listing networks") + } + networks := strings.Split(rr.Output(), "\n") + for _, n := range networks { + if n == name { + return true + } + } + return false } diff --git a/pkg/drivers/kic/oci/oci.go b/pkg/drivers/kic/oci/oci.go index 96731ceeb8..37d87f87c0 100644 --- a/pkg/drivers/kic/oci/oci.go +++ b/pkg/drivers/kic/oci/oci.go @@ -142,18 +142,18 @@ func CreateContainerNode(p CreateParams) error { "--label", p.NodeLabel, } - // network - if p.OCIBinary == Docker && runtime.GOOS == "linux" { // for now only docker on linux - runArgs = append(runArgs, "--network", p.Network) - runArgs = append(runArgs, "--ip", p.IP) - } - // volume if p.OCIBinary == Podman { // enable execing in /var // 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", p.Name)) } if p.OCIBinary == Docker { + // on linux, we can provide a static IP for docker + if runtime.GOOS == "linux" && p.Network != "" && p.IP != "" { + runArgs = append(runArgs, "--network", p.Network) + runArgs = append(runArgs, "--ip", p.IP) + } + runArgs = append(runArgs, "--volume", fmt.Sprintf("%s:/var", p.Name)) // ignore apparmore github actions docker: https://github.com/kubernetes/minikube/issues/7624 runArgs = append(runArgs, "--security-opt", "apparmor=unconfined") diff --git a/pkg/drivers/kic/types.go b/pkg/drivers/kic/types.go index 99acc614b5..7fc8a5b8d7 100644 --- a/pkg/drivers/kic/types.go +++ b/pkg/drivers/kic/types.go @@ -27,6 +27,8 @@ const ( Version = "v0.0.10" // SHA of the kic base image baseImageSHA = "f58e0c4662bac8a9b5dda7984b185bad8502ade5d9fa364bf2755d636ab51438" + + defaultIPRange = "192.168.39.0/24" ) var (