Skip to content

Commit

Permalink
use netaddr.IP for nodeIP and make sure that we don't use the same IP…
Browse files Browse the repository at this point in the history
… twice for the server nodes
  • Loading branch information
iwilltry42 committed Apr 14, 2021
1 parent 6d968cf commit ee9a515
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pkg/client/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/client/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
7 changes: 7 additions & 0 deletions pkg/runtimes/docker/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/runtimes/docker/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (c *Cluster) HasLoadBalancer() bool {
}

type NodeIP struct {
IP string
IP netaddr.IP
Static bool
}

Expand Down

0 comments on commit ee9a515

Please sign in to comment.