diff options
author | Paul Holzinger <pholzing@redhat.com> | 2021-10-13 21:52:55 +0200 |
---|---|---|
committer | Paul Holzinger <pholzing@redhat.com> | 2021-11-11 15:54:02 +0100 |
commit | 12c62b92ff2f63cb34dcb9c0555b96983e6aad94 (patch) | |
tree | 01e489ea273813b8ae0bd7302c9f91bda5c2f23a /libpod/network/internal/util/bridge.go | |
parent | 8fd31c674b02b800267b2a759e2406902fdb2723 (diff) | |
download | podman-12c62b92ff2f63cb34dcb9c0555b96983e6aad94.tar.gz podman-12c62b92ff2f63cb34dcb9c0555b96983e6aad94.tar.bz2 podman-12c62b92ff2f63cb34dcb9c0555b96983e6aad94.zip |
Make networking code reusable
To prevent code duplication when creating new network backends move
reusable code into a separate internal package.
This allows all network backends to use the same code as long as they
implement the new NetUtil interface.
Signed-off-by: Paul Holzinger <pholzing@redhat.com>
Diffstat (limited to 'libpod/network/internal/util/bridge.go')
-rw-r--r-- | libpod/network/internal/util/bridge.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/libpod/network/internal/util/bridge.go b/libpod/network/internal/util/bridge.go new file mode 100644 index 000000000..c054c7d4e --- /dev/null +++ b/libpod/network/internal/util/bridge.go @@ -0,0 +1,67 @@ +package util + +import ( + "net" + + "github.com/containers/podman/v3/libpod/define" + "github.com/containers/podman/v3/libpod/network/types" + "github.com/containers/podman/v3/libpod/network/util" + pkgutil "github.com/containers/podman/v3/pkg/util" + "github.com/pkg/errors" +) + +func CreateBridge(n NetUtil, network *types.Network, usedNetworks []*net.IPNet) error { + if network.NetworkInterface != "" { + bridges := GetBridgeInterfaceNames(n) + if pkgutil.StringInSlice(network.NetworkInterface, bridges) { + return errors.Errorf("bridge name %s already in use", network.NetworkInterface) + } + if !define.NameRegex.MatchString(network.NetworkInterface) { + return errors.Wrapf(define.RegexError, "bridge name %s invalid", network.NetworkInterface) + } + } else { + var err error + network.NetworkInterface, err = GetFreeDeviceName(n) + if err != nil { + return err + } + } + + 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 { + 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.IPAMOptions["driver"] = types.HostLocalIPAMDriver + return nil +} |