Skip to content

Commit

Permalink
remove static ip in docker and podman
Browse files Browse the repository at this point in the history
  • Loading branch information
zhan9san committed Mar 10, 2022
1 parent e78c26f commit ecb1ad6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 219 deletions.
10 changes: 0 additions & 10 deletions pkg/drivers/kic/kic.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/download"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/style"
"k8s.io/minikube/pkg/minikube/sysinit"
Expand Down Expand Up @@ -96,15 +95,6 @@ func (d *Driver) Create() error {
out.WarningT("Unable to create dedicated network, this might result in cluster IP change after restart: {{.error}}", out.V{"error": err})
} else if gateway != nil {
params.Network = networkName
ip := gateway.To4()
// calculate the container IP based on guessing the machine index
index := driver.IndexFromMachineName(d.NodeConfig.MachineName)
if int(ip[3])+index > 255 {
return fmt.Errorf("too many machines to calculate an IP")
}
ip[3] += byte(index)
klog.Infof("calculated static IP %q for the %q container", ip.String(), d.NodeConfig.MachineName)
params.IP = ip.String()
}
drv := d.DriverName()

Expand Down
22 changes: 15 additions & 7 deletions pkg/drivers/kic/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,9 @@ func CreateContainerNode(p CreateParams) error {
// label th enode wuth the node ID
"--label", p.NodeLabel,
}
// to provide a static IP
if p.Network != "" && p.IP != "" {
// to provide a network
if p.Network != "" {
runArgs = append(runArgs, "--network", p.Network)
runArgs = append(runArgs, "--ip", p.IP)
}

memcgSwap := hasMemorySwapCgroup()
Expand Down Expand Up @@ -256,7 +255,7 @@ func CreateContainerNode(p CreateParams) error {
runArgs = append(runArgs, "--userns=host")
}

if err := createContainer(p.OCIBinary, p.Image, withRunArgs(runArgs...), withMounts(p.Mounts), withPortMappings(p.PortMappings)); err != nil {
if err := CreateContainer(p.OCIBinary, p.Image, WithRunArgs(runArgs...), withMounts(p.Mounts), withPortMappings(p.PortMappings)); err != nil {
return errors.Wrap(err, "create container")
}

Expand Down Expand Up @@ -296,7 +295,7 @@ func CreateContainerNode(p CreateParams) error {
}

// CreateContainer creates a container with "docker/podman run"
func createContainer(ociBin string, image string, opts ...createOpt) error {
func CreateContainer(ociBin string, image string, opts ...createOpt) error {
o := &createOpts{}
for _, opt := range opts {
o = opt(o)
Expand Down Expand Up @@ -512,15 +511,24 @@ func generatePortMappings(portMappings ...PortMapping) []string {
return result
}

// withRunArgs sets the args for docker run
// WithRunArgs sets the args for docker run
// as in the args portion of `docker run args... image containerArgs...`
func withRunArgs(args ...string) createOpt {
func WithRunArgs(args ...string) createOpt {
return func(r *createOpts) *createOpts {
r.RunArgs = args
return r
}
}

// WithRunArgs sets the containerArgs
// as in the args portion of `docker run args... image containerArgs...`
func WithContainerArgs(args ...string) createOpt {
return func(r *createOpts) *createOpts {
r.ContainerArgs = args
return r
}
}

// withMounts sets the container mounts
func withMounts(mounts []Mount) createOpt {
return func(r *createOpts) *createOpts {
Expand Down
1 change: 0 additions & 1 deletion pkg/drivers/kic/oci/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type CreateParams struct {
ExtraArgs []string // a list of any extra option to pass to oci binary during creation time, for example --expose 8080...
OCIBinary string // docker or podman
Network string // network name that the container will attach to
IP string // static IP to assign for th container in the cluster network
}

// createOpt is an option for Create
Expand Down
17 changes: 0 additions & 17 deletions pkg/minikube/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os"
"runtime"
"sort"
"strconv"
"strings"

"k8s.io/klog/v2"
Expand Down Expand Up @@ -345,19 +344,3 @@ func SetLibvirtURI(v string) {
os.Setenv("LIBVIRT_DEFAULT_URI", v)

}

// IndexFromMachineName returns the order of the container based on it is name
func IndexFromMachineName(machineName string) int {
// minikube or offline-docker-20210314040449-6655 or minion-m02
sp := strings.Split(machineName, "-")
m := sp[len(sp)-1] // minikube or 6655 or m02
if strings.HasPrefix(m, "m") { // likely minion node
m = strings.TrimPrefix(m, "m")
i, err := strconv.Atoi(m)
if err != nil {
return 1 // master node
}
return i // minion node
}
return 1 // master node
}
184 changes: 0 additions & 184 deletions pkg/minikube/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/registry"
)

Expand Down Expand Up @@ -206,186 +205,3 @@ func TestSuggest(t *testing.T) {
})
}
}

func TestIndexFromMachineName(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: "funny",
MachineName: "hahaha",
Want: 1},

{
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},
{
Name: "third-node",
MachineName: "minikube-m3",
Want: 3},
{
Name: "agent-node",
MachineName: "minikube-m007",
Want: 7},
{
Name: "byte-0",
MachineName: "offline-docker-20210314040449-6655",
Want: 1},
{
Name: "byte-255",
MachineName: "offline-docker-20210314040449-6654",
Want: 1},
}

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

}
})

}
}

// test indexFroMachine against cluster config
func TestIndexFromMachineNameClusterConfig(t *testing.T) {

testsCases := []struct {
ClusterConfig config.ClusterConfig
Want int
}{
{
ClusterConfig: config.ClusterConfig{Name: "minikube",
Nodes: []config.Node{
{
Name: "",
IP: "172.17.0.3",
Port: 8443,
KubernetesVersion: "v1.19.2",
ControlPlane: true,
Worker: true,
},
},
},
Want: 1,
},

{
ClusterConfig: config.ClusterConfig{Name: "p2",
Nodes: []config.Node{
{
Name: "",
IP: "172.17.0.3",
Port: 8443,
KubernetesVersion: "v1.19.2",
ControlPlane: true,
Worker: true,
},
{
Name: "m2",
IP: "172.17.0.4",
Port: 0,
KubernetesVersion: "v1.19.2",
ControlPlane: false,
Worker: true,
},
},
},
Want: 2,
},

{
ClusterConfig: config.ClusterConfig{Name: "p3",
Nodes: []config.Node{
{
Name: "",
IP: "172.17.0.3",
Port: 8443,
KubernetesVersion: "v1.19.2",
ControlPlane: true,
Worker: true,
},
{
Name: "m02",
IP: "172.17.0.4",
Port: 0,
KubernetesVersion: "v1.19.2",
ControlPlane: false,
Worker: true,
},
{
Name: "m03",
IP: "172.17.0.5",
Port: 0,
KubernetesVersion: "v1.19.2",
ControlPlane: false,
Worker: true,
},
},
},
Want: 3,
},

{
ClusterConfig: config.ClusterConfig{Name: "offline-docker-20210314040449-6654",
Nodes: []config.Node{
{
Name: "",
IP: "172.17.0.3",
Port: 8443,
KubernetesVersion: "v1.19.2",
ControlPlane: true,
Worker: true,
},
},
},
Want: 1,
},

{
ClusterConfig: config.ClusterConfig{Name: "offline-docker-20210314040449-6655",
Nodes: []config.Node{
{
Name: "",
IP: "172.17.0.3",
Port: 8443,
KubernetesVersion: "v1.19.2",
ControlPlane: true,
Worker: true,
},
},
},
Want: 1,
},
}

for _, tc := range testsCases {
got := IndexFromMachineName(config.MachineName(tc.ClusterConfig, tc.ClusterConfig.Nodes[len(tc.ClusterConfig.Nodes)-1]))
if got != tc.Want {
t.Errorf("expected IndexFromMachineName to be %d but got %d", tc.Want, got)
}

}
}
51 changes: 51 additions & 0 deletions test/integration/kic_custom_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,57 @@ func TestKicExistingNetwork(t *testing.T) {
}
}

// TestKicExistingNetworkNonFirstContainer verifies the docker driver and run with an existing network
func TestKicExistingNetworkNonFirstContainer(t *testing.T) {
if !KicDriver() {
t.Skip("only runs with docker driver")
}
// create custom network
networkName := "existing-network"
if _, err := oci.CreateNetwork(oci.Docker, networkName); err != nil {
t.Fatalf("error creating network: %v", err)
}
defer func() {
if err := oci.DeleteKICNetworks(oci.Docker); err != nil {
t.Logf("error deleting kic network, may need to delete manually: %v", err)
}
}()
profile := UniqueProfileName("existing-network")
ctx, cancel := context.WithTimeout(context.Background(), Minutes(5))
defer Cleanup(t, profile, cancel)

verifyNetworkExists(ctx, t, networkName)

// create first container
containerName := "first-container"
containerImage := "busybox"
runArgs := []string{
"--rm",
"-d",
"--name", containerName,
"--network", networkName,
"--entrypoint", "sleep",
}
containerArgs := []string{
"infinity",
}
if err := oci.CreateContainer(oci.Docker, containerImage, oci.WithRunArgs(runArgs...), oci.WithContainerArgs(containerArgs...)); err != nil {
t.Fatalf("error creating container: %v", err)
}
defer func() {
if err := oci.DeleteContainer(ctx, oci.Docker, containerName); err != nil {
t.Logf("error deleting container, may need to delete manually: %v", err)
}
}()

startArgs := []string{"start", "-p", profile, fmt.Sprintf("--network=%s", networkName)}
c := exec.CommandContext(ctx, Target(), startArgs...)
rr, err := Run(t, c)
if err != nil {
t.Fatalf("%v failed: %v\n%v", rr.Command(), err, rr.Output())
}
}

func verifyNetworkExists(ctx context.Context, t *testing.T, networkName string) {
c := exec.CommandContext(ctx, "docker", "network", "ls", "--format", "{{.Name}}")
rr, err := Run(t, c)
Expand Down

0 comments on commit ecb1ad6

Please sign in to comment.