summaryrefslogtreecommitdiff
path: root/libpod/networking_common.go
diff options
context:
space:
mode:
authorDoug Rabson <dfr@rabson.org>2022-09-07 17:17:21 +0100
committerDoug Rabson <dfr@rabson.org>2022-09-12 16:28:47 +0100
commitf939f3fdfccaeeb976085e5082e56efe6eb6df62 (patch)
treeb8701d6fc00fcc5ab89c40812fe3974db70c9b1b /libpod/networking_common.go
parentc9de84080dd9570944dd4b7b0e46160bb83998bb (diff)
downloadpodman-f939f3fdfccaeeb976085e5082e56efe6eb6df62.tar.gz
podman-f939f3fdfccaeeb976085e5082e56efe6eb6df62.tar.bz2
podman-f939f3fdfccaeeb976085e5082e56efe6eb6df62.zip
libpod: Move resultToBasicNetworkConfig to networking_common.go
[NO NEW TESTS NEEDED] Signed-off-by: Doug Rabson <dfr@rabson.org>
Diffstat (limited to 'libpod/networking_common.go')
-rw-r--r--libpod/networking_common.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/libpod/networking_common.go b/libpod/networking_common.go
index 1dd799b40..e6509e66b 100644
--- a/libpod/networking_common.go
+++ b/libpod/networking_common.go
@@ -6,6 +6,7 @@ package libpod
import (
"fmt"
"regexp"
+ "sort"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/machine"
@@ -301,3 +302,46 @@ func (c *Container) getContainerNetworkInfo() (*define.InspectNetworkSettings, e
}
return settings, nil
}
+
+// resultToBasicNetworkConfig produces an InspectBasicNetworkConfig from a CNI
+// result
+func resultToBasicNetworkConfig(result types.StatusBlock) define.InspectBasicNetworkConfig {
+ config := define.InspectBasicNetworkConfig{}
+ interfaceNames := make([]string, 0, len(result.Interfaces))
+ for interfaceName := range result.Interfaces {
+ interfaceNames = append(interfaceNames, interfaceName)
+ }
+ // ensure consistent inspect results by sorting
+ sort.Strings(interfaceNames)
+ for _, interfaceName := range interfaceNames {
+ netInt := result.Interfaces[interfaceName]
+ for _, netAddress := range netInt.Subnets {
+ size, _ := netAddress.IPNet.Mask.Size()
+ if netAddress.IPNet.IP.To4() != nil {
+ // ipv4
+ if config.IPAddress == "" {
+ config.IPAddress = netAddress.IPNet.IP.String()
+ config.IPPrefixLen = size
+ config.Gateway = netAddress.Gateway.String()
+ } else {
+ config.SecondaryIPAddresses = append(config.SecondaryIPAddresses, define.Address{Addr: netAddress.IPNet.IP.String(), PrefixLength: size})
+ }
+ } else {
+ // ipv6
+ if config.GlobalIPv6Address == "" {
+ config.GlobalIPv6Address = netAddress.IPNet.IP.String()
+ config.GlobalIPv6PrefixLen = size
+ config.IPv6Gateway = netAddress.Gateway.String()
+ } else {
+ config.SecondaryIPv6Addresses = append(config.SecondaryIPv6Addresses, define.Address{Addr: netAddress.IPNet.IP.String(), PrefixLength: size})
+ }
+ }
+ }
+ if config.MacAddress == "" {
+ config.MacAddress = netInt.MacAddress.String()
+ } else {
+ config.AdditionalMacAddresses = append(config.AdditionalMacAddresses, netInt.MacAddress.String())
+ }
+ }
+ return config
+}