From 4febe557692aeec8ca9d9b9cdc732772ba7d5876 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 20 Oct 2021 15:55:22 +0200 Subject: netavark IPAM assignment Add a new boltdb to handle IPAM assignment. The db structure is the following: Each network has their own bucket with the network name as bucket key. Inside the network bucket there is an ID bucket which maps the container ID (key) to a json array of ip addresses (value). The network bucket also has a bucket for each subnet, the subnet is used as key. Inside the subnet bucket an ip is used as key and the container ID as value. The db should be stored on a tmpfs to ensure we always have a clean state after a reboot. Signed-off-by: Paul Holzinger --- libpod/network/internal/util/bridge.go | 56 +++++++++++++++++--------------- libpod/network/internal/util/ip.go | 8 ----- libpod/network/internal/util/validate.go | 6 ++-- 3 files changed, 32 insertions(+), 38 deletions(-) (limited to 'libpod/network/internal') diff --git a/libpod/network/internal/util/bridge.go b/libpod/network/internal/util/bridge.go index c054c7d4e..476557050 100644 --- a/libpod/network/internal/util/bridge.go +++ b/libpod/network/internal/util/bridge.go @@ -27,41 +27,43 @@ func CreateBridge(n NetUtil, network *types.Network, usedNetworks []*net.IPNet) } } - if len(network.Subnets) == 0 { - freeSubnet, err := GetFreeIPv4NetworkSubnet(usedNetworks) - if err != nil { - return err - } - network.Subnets = append(network.Subnets, *freeSubnet) - } - // ipv6 enabled means dual stack, check if we already have - // a ipv4 or ipv6 subnet and add one if not. - if network.IPv6Enabled { - ipv4 := false - ipv6 := false - for _, subnet := range network.Subnets { - if util.IsIPv6(subnet.Subnet.IP) { - ipv6 = true - } - if util.IsIPv4(subnet.Subnet.IP) { - ipv4 = true - } - } - if !ipv4 { + if network.IPAMOptions["driver"] != types.DHCPIPAMDriver { + if len(network.Subnets) == 0 { freeSubnet, err := GetFreeIPv4NetworkSubnet(usedNetworks) if err != nil { return err } network.Subnets = append(network.Subnets, *freeSubnet) } - if !ipv6 { - freeSubnet, err := GetFreeIPv6NetworkSubnet(usedNetworks) - if err != nil { - return err + // ipv6 enabled means dual stack, check if we already have + // a ipv4 or ipv6 subnet and add one if not. + if network.IPv6Enabled { + ipv4 := false + ipv6 := false + for _, subnet := range network.Subnets { + if util.IsIPv6(subnet.Subnet.IP) { + ipv6 = true + } + if util.IsIPv4(subnet.Subnet.IP) { + ipv4 = true + } + } + if !ipv4 { + freeSubnet, err := GetFreeIPv4NetworkSubnet(usedNetworks) + if err != nil { + return err + } + network.Subnets = append(network.Subnets, *freeSubnet) + } + if !ipv6 { + freeSubnet, err := GetFreeIPv6NetworkSubnet(usedNetworks) + if err != nil { + return err + } + network.Subnets = append(network.Subnets, *freeSubnet) } - network.Subnets = append(network.Subnets, *freeSubnet) } + network.IPAMOptions["driver"] = types.HostLocalIPAMDriver } - network.IPAMOptions["driver"] = types.HostLocalIPAMDriver return nil } diff --git a/libpod/network/internal/util/ip.go b/libpod/network/internal/util/ip.go index ee759fd65..7fe35d3d4 100644 --- a/libpod/network/internal/util/ip.go +++ b/libpod/network/internal/util/ip.go @@ -68,11 +68,3 @@ func getRandomIPv6Subnet() (net.IPNet, error) { ip = append(ip, make([]byte, 8)...) return net.IPNet{IP: ip, Mask: net.CIDRMask(64, 128)}, nil } - -// NormalizeIP will transform the given ip to the 4 byte len ipv4 if possible -func NormalizeIP(ip *net.IP) { - ipv4 := ip.To4() - if ipv4 != nil { - *ip = ipv4 - } -} diff --git a/libpod/network/internal/util/validate.go b/libpod/network/internal/util/validate.go index 4dced8631..62c3f3951 100644 --- a/libpod/network/internal/util/validate.go +++ b/libpod/network/internal/util/validate.go @@ -38,7 +38,7 @@ func ValidateSubnet(s *types.Subnet, addGateway bool, usedNetworks []*net.IPNet) if !s.Subnet.Contains(s.Gateway) { return errors.Errorf("gateway %s not in subnet %s", s.Gateway, &s.Subnet) } - NormalizeIP(&s.Gateway) + util.NormalizeIP(&s.Gateway) } else if addGateway { ip, err := util.FirstIPInSubnet(net) if err != nil { @@ -52,13 +52,13 @@ func ValidateSubnet(s *types.Subnet, addGateway bool, usedNetworks []*net.IPNet) if !s.Subnet.Contains(s.LeaseRange.StartIP) { return errors.Errorf("lease range start ip %s not in subnet %s", s.LeaseRange.StartIP, &s.Subnet) } - NormalizeIP(&s.LeaseRange.StartIP) + util.NormalizeIP(&s.LeaseRange.StartIP) } if s.LeaseRange.EndIP != nil { if !s.Subnet.Contains(s.LeaseRange.EndIP) { return errors.Errorf("lease range end ip %s not in subnet %s", s.LeaseRange.EndIP, &s.Subnet) } - NormalizeIP(&s.LeaseRange.EndIP) + util.NormalizeIP(&s.LeaseRange.EndIP) } } return nil -- cgit v1.2.3-54-g00ecf