Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: docker: fix network static #9094

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func deleteContainersAndVolumes(ociBin string) {
if len(errs) > 0 { // it will not error if there is nothing to delete
glog.Warningf("error pruning volumes by label %q (might be okay): %+v", delLabel, errs)
}

errs = oci.DeleteAllNetworksByKIC()
if errs != nil {
glog.Warningf("error deleting left over networks (might be okay).\nTo see the list of netowrks: 'docker network ls'\n:%v", errs)
}
}

// runDelete handles the executes the flow of "minikube delete"
Expand Down Expand Up @@ -258,6 +263,11 @@ func deletePossibleKicLeftOver(cname string, driverName string) {
glog.Warningf("error deleting volumes (might be okay).\nTo see the list of volumes run: 'docker volume ls'\n:%v", errs)
}

errs = oci.DeleteAllNetworksByKIC()
if errs != nil {
glog.Warningf("error deleting left over networks (might be okay).\nTo see the list of netowrks: 'docker network ls'\n:%v", errs)
}

if bin == oci.Podman {
// podman prune does not support --filter
return
Expand Down
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var mountCmd = &cobra.Command{
var ip net.IP
var err error
if mountIP == "" {
ip, err = cluster.HostIP(co.CP.Host)
ip, err = cluster.HostIP(co.CP.Host, co.Config.Name)
if err != nil {
exit.WithError("Error getting the host IP address to use from within the VM", err)
}
Expand Down
3 changes: 3 additions & 0 deletions hack/jenkins/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ sudo ./installers/check_install_golang.sh "1.14.6" "/usr/local" || true

docker rm -f -v $(docker ps -aq) >/dev/null 2>&1 || true
docker volume prune -f || true
docker network prune -f || true
docker system prune -f || true
docker system df || true


echo ">> Starting at $(date)"
echo ""
echo "arch: ${OS_ARCH}"
Expand Down
2 changes: 2 additions & 0 deletions hack/jenkins/cron/cleanup_and_reboot_Darwin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ killall java
# clean docker left overs
docker rm -f -v $(docker ps -aq) >/dev/null 2>&1 || true
docker volume prune -f || true
docker system prune -f || true
docker network prune -f || true
docker volume ls || true
docker system df || true

Expand Down
2 changes: 2 additions & 0 deletions hack/jenkins/cron/cleanup_and_reboot_Linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ killall java
# clean docker left overs
docker rm -f -v $(docker ps -aq) >/dev/null 2>&1 || true
docker volume prune -f || true
docker system prune -f || true
docker network prune -f || true
docker volume ls || true
docker system df || true

Expand Down
1 change: 1 addition & 0 deletions hack/preload-images/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (

func generateTarball(kubernetesVersion, containerRuntime, tarballFilename string) error {
driver := kic.NewDriver(kic.Config{
ClusterName: profile,
KubernetesVersion: kubernetesVersion,
ContainerRuntime: containerRuntime,
OCIBinary: oci.Docker,
Expand Down
27 changes: 23 additions & 4 deletions pkg/drivers/kic/kic.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/download"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/sysinit"
"k8s.io/minikube/pkg/util/retry"
)
Expand Down Expand Up @@ -65,6 +66,18 @@ func NewDriver(c Config) *Driver {
return d
}

// machineOrder returns the order of the container based on it is name
func machineOrder(machineName string) int {
// minikube-m02
sp := strings.Split(machineName, "-")
m := strings.Trim(sp[len(sp)-1], "m") // m02
i, err := strconv.Atoi(m)
if err != nil {
return 1
}
return i
}

// Create a host using the driver's config
func (d *Driver) Create() error {
params := oci.CreateParams{
Expand All @@ -80,12 +93,18 @@ func (d *Driver) Create() error {
APIServerPort: d.NodeConfig.APIServerPort,
}

defaultNetwork := d.MachineName
if err := oci.CreateNetwork(defaultNetwork, oci.DefaultIPRange, oci.DefaultGateway); err != nil {
glog.Warningf("unable to create docker network; node ip may not be stable: %v", err)
// one network bridge per cluster.
defaultNetwork := d.NodeConfig.ClusterName
if gateway, err := oci.CreateNetwork(d.OCIBinary, defaultNetwork); err != nil {
glog.Warningf("failed to create network: %v", err)
out.WarningT("Unable to create dedicated network, This might result in cluster IP change after restart.")
} else {
params.Network = defaultNetwork
params.IP = oci.DefaultIP
ip := gateway.To4()
// calculate the container IP based on its machine order
ip[3] = ip[3] + byte(machineOrder(d.NodeConfig.MachineName))
glog.Infof("calculated static IP %q for the %q container ", ip.String(), d.NodeConfig.MachineName)
params.IP = ip.String()
}

// control plane specific options
Expand Down
62 changes: 62 additions & 0 deletions pkg/drivers/kic/kic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2019 The Kubernetes Authors All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package kic

import (
"testing"
)

func TestMachineOrder(t *testing.T) {
testCases := []struct {
Name string
MachineName string
Want int
}{
{
Name: "default",
MachineName: "minikube",
Want: 1},
{
Name: "second-node",
MachineName: "minikube-m02",
Want: 2},
{
Name: "dash-profile",
MachineName: "my-dashy-minikube",
Want: 1},

{
Name: "dash-profile-second-node",
MachineName: "my-dashy-minikube-m02",
Want: 2},
{
Name: "michivious-user",
MachineName: "michivious-user-m02-m03",
Want: 3},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
got := machineOrder(tc.MachineName)
if got != tc.Want {
t.Errorf("want order %q but got %q", tc.Want, got)

}
})

}
}
15 changes: 15 additions & 0 deletions pkg/drivers/kic/oci/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,27 @@ var ErrWindowsContainers = &FailFastError{errors.New("docker container type is w
// ErrCPUCountLimit is thrown when docker daemon doesn't have enough CPUs for the requested container
var ErrCPUCountLimit = &FailFastError{errors.New("not enough CPUs is available for container")}

// ErrIPinUse is thrown when the container been given an IP used by another container
var ErrIPinUse = &FailFastError{errors.New("can't create with that IP, address already in use")}

// ErrExitedUnexpectedly is thrown when container is created/started without error but later it exists and it's status is not running anymore.
var ErrExitedUnexpectedly = errors.New("container exited unexpectedly")

// ErrDaemonInfo is thrown when docker/podman info is failing or not responding
var ErrDaemonInfo = errors.New("daemon info not responding")

// ErrNetworkSubnetTaken is thrown when a subnet is taken by another network
var ErrNetworkSubnetTaken = errors.New("subnet is taken")

// ErrNetworkNotFound is when given network was not found
var ErrNetworkNotFound = errors.New("kic network not found")

// ErrNetworkGatewayTaken is when given network gatway is taken
var ErrNetworkGatewayTaken = errors.New("network gateway is taken")

// ErrNetworkInUse is when trying to delete a network which is attached to another container
var ErrNetworkInUse = errors.New("can't delete network attached to a running container")

// LogContainerDebug will print relevant docker/podman infos after a container fails
func LogContainerDebug(ociBin string, name string) string {
rr, err := containerInspect(ociBin, name)
Expand Down
Loading