From ee9a5157ab38655a1156c3d88c22221688c24c5e Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Wed, 14 Apr 2021 14:20:05 +0200 Subject: [PATCH] use netaddr.IP for nodeIP and make sure that we don't use the same IP twice for the server nodes --- pkg/client/cluster.go | 3 ++- pkg/client/ipam.go | 8 ++++---- pkg/runtimes/docker/network.go | 7 +++++++ pkg/runtimes/docker/translate.go | 4 ++-- pkg/types/types.go | 2 +- 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/client/cluster.go b/pkg/client/cluster.go index 78d6ed4ee..c5fa0b8c4 100644 --- a/pkg/client/cluster.go +++ b/pkg/client/cluster.go @@ -398,9 +398,10 @@ ClusterCreatOpts: if err != nil { return err } + cluster.Network.IPAM.IPsUsed = append(cluster.Network.IPAM.IPsUsed, ip) // make sure that we're not reusing the same IP next time node.IP.Static = true node.IP.IP = ip - node.Labels[k3d.LabelNodeStaticIP] = ip + node.Labels[k3d.LabelNodeStaticIP] = ip.String() } node.ServerOpts.KubeAPI = cluster.KubeAPI diff --git a/pkg/client/ipam.go b/pkg/client/ipam.go index e1e27878c..773fe0072 100644 --- a/pkg/client/ipam.go +++ b/pkg/client/ipam.go @@ -30,11 +30,11 @@ import ( "inet.af/netaddr" ) -func GetIP(ctx context.Context, runtime k3drt.Runtime, network *k3d.ClusterNetwork) (string, error) { +func GetIP(ctx context.Context, runtime k3drt.Runtime, network *k3d.ClusterNetwork) (netaddr.IP, error) { network, err := runtime.GetNetwork(ctx, network) if err != nil { - return "", err + return netaddr.IP{}, err } var ipsetbuilder netaddr.IPSetBuilder @@ -51,9 +51,9 @@ func GetIP(ctx context.Context, runtime k3drt.Runtime, network *k3d.ClusterNetwo ipset := ipsetbuilder.IPSet() - ip := ipset.Ranges()[0].From.String() + ip := ipset.Ranges()[0].From - log.Debugf("Found free IP %s in network %s", ip, network.Name) + log.Debugf("Found free IP %s in network %s", ip.String(), network.Name) return ip, nil } diff --git a/pkg/runtimes/docker/network.go b/pkg/runtimes/docker/network.go index b92b61993..b3e816a93 100644 --- a/pkg/runtimes/docker/network.go +++ b/pkg/runtimes/docker/network.go @@ -105,6 +105,13 @@ func (d Docker) GetNetwork(ctx context.Context, searchNet *k3d.ClusterNetwork) ( } } + // append the used IPs that we already know from the search network + // this is needed because the network inspect does not return the container list until the containers are actually started + // and we already need this when we create the containers + for _, used := range searchNet.IPAM.IPsUsed { + network.IPAM.IPsUsed = append(network.IPAM.IPsUsed, used) + } + // Only one Network allowed, but some functions don't care about this, so they can ignore the error and just use the first one returned if len(networkList) > 1 { return network, runtimeErr.ErrRuntimeNetworkMultiSameName diff --git a/pkg/runtimes/docker/translate.go b/pkg/runtimes/docker/translate.go index 8d9d8fe00..82f846bbc 100644 --- a/pkg/runtimes/docker/translate.go +++ b/pkg/runtimes/docker/translate.go @@ -124,12 +124,12 @@ func TranslateNodeToContainer(node *k3d.Node) (*NodeInDocker, error) { networkingConfig.EndpointsConfig = endpointsConfig /* Static IP */ - if node.IP.IP != "" && node.IP.Static { + if !node.IP.IP.IsZero() && node.IP.Static { epconf := networkingConfig.EndpointsConfig[node.Networks[0]] if epconf.IPAMConfig == nil { epconf.IPAMConfig = &network.EndpointIPAMConfig{} } - epconf.IPAMConfig.IPv4Address = node.IP.IP + epconf.IPAMConfig.IPv4Address = node.IP.IP.String() } if len(node.Networks) > 0 { diff --git a/pkg/types/types.go b/pkg/types/types.go index 73c7aa22a..5745ce1c9 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -324,7 +324,7 @@ func (c *Cluster) HasLoadBalancer() bool { } type NodeIP struct { - IP string + IP netaddr.IP Static bool }