runtimes/docker/network: only read ipam config if exists
- fixes #576 - IPAM config is empty for e.g. the "host" network
This commit is contained in:
parent
5383ba3f6f
commit
b4c910b729
@ -74,42 +74,48 @@ func (d Docker) GetNetwork(ctx context.Context, searchNet *k3d.ClusterNetwork) (
|
|||||||
return nil, runtimeErr.ErrRuntimeNetworkNotExists
|
return nil, runtimeErr.ErrRuntimeNetworkNotExists
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix, err := netaddr.ParseIPPrefix(networkList[0].IPAM.Config[0].Subnet)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
gateway, err := netaddr.ParseIP(networkList[0].IPAM.Config[0].Gateway)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
network := &k3d.ClusterNetwork{
|
network := &k3d.ClusterNetwork{
|
||||||
Name: networkList[0].Name,
|
Name: networkList[0].Name,
|
||||||
ID: networkList[0].ID,
|
ID: networkList[0].ID,
|
||||||
IPAM: k3d.IPAM{
|
}
|
||||||
|
|
||||||
|
// for networks that have an IPAM config, we inspect that as well (e.g. "host" network doesn't have it)
|
||||||
|
if len(networkList[0].IPAM.Config) > 0 {
|
||||||
|
prefix, err := netaddr.ParseIPPrefix(networkList[0].IPAM.Config[0].Subnet)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
gateway, err := netaddr.ParseIP(networkList[0].IPAM.Config[0].Gateway)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
network.IPAM = k3d.IPAM{
|
||||||
IPPrefix: prefix,
|
IPPrefix: prefix,
|
||||||
IPsUsed: []netaddr.IP{
|
IPsUsed: []netaddr.IP{
|
||||||
gateway,
|
gateway,
|
||||||
},
|
},
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, container := range networkList[0].Containers {
|
|
||||||
if container.IPv4Address != "" {
|
|
||||||
ip, err := netaddr.ParseIP(container.IPv4Address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
network.IPAM.IPsUsed = append(network.IPAM.IPsUsed, ip)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// append the used IPs that we already know from the search network
|
for _, container := range networkList[0].Containers {
|
||||||
// this is needed because the network inspect does not return the container list until the containers are actually started
|
if container.IPv4Address != "" {
|
||||||
// and we already need this when we create the containers
|
ip, err := netaddr.ParseIP(container.IPv4Address)
|
||||||
for _, used := range searchNet.IPAM.IPsUsed {
|
if err != nil {
|
||||||
network.IPAM.IPsUsed = append(network.IPAM.IPsUsed, used)
|
return nil, err
|
||||||
|
}
|
||||||
|
network.IPAM.IPsUsed = append(network.IPAM.IPsUsed, ip)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Debugf("Network %s does not have an IPAM config", network.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
// 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
|
||||||
|
Loading…
Reference in New Issue
Block a user