[Fix] Use default gateway, when bridge network doesn't have it (#666, @kuritka)
Signed-off-by: kuritka <kuritka@gmail.com>
This commit is contained in:
parent
eff262b3c7
commit
cbe187d008
@ -80,23 +80,11 @@ func (d Docker) GetNetwork(ctx context.Context, searchNet *k3d.ClusterNetwork) (
|
||||
|
||||
// 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)
|
||||
network.IPAM, err = d.parseIPAM(networkList[0].IPAM.Config[0])
|
||||
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,
|
||||
IPsUsed: []netaddr.IP{
|
||||
gateway,
|
||||
},
|
||||
}
|
||||
|
||||
for _, container := range networkList[0].Containers {
|
||||
if container.IPv4Address != "" {
|
||||
ip, err := netaddr.ParseIP(container.IPv4Address)
|
||||
@ -348,3 +336,24 @@ func (d Docker) getFreeSubnetPrefix(ctx context.Context) (netaddr.IPPrefix, erro
|
||||
return fakenet.IPAM.IPPrefix, nil
|
||||
|
||||
}
|
||||
|
||||
// parseIPAM Returns an IPAM structure with the subnet and gateway filled in. If some of the values
|
||||
// cannot be parsed, an error is returned. If gateway is empty, the function calculates the default gateway.
|
||||
func (d Docker) parseIPAM(config network.IPAMConfig) (ipam k3d.IPAM, err error){
|
||||
var gateway netaddr.IP
|
||||
ipam = k3d.IPAM{ IPsUsed: []netaddr.IP{}}
|
||||
|
||||
ipam.IPPrefix, err = netaddr.ParseIPPrefix(config.Subnet)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if config.Gateway == "" {
|
||||
gateway = ipam.IPPrefix.IP.Next()
|
||||
} else {
|
||||
gateway, err = netaddr.ParseIP(config.Gateway)
|
||||
}
|
||||
ipam.IPsUsed = append(ipam.IPsUsed, gateway)
|
||||
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user