From 4ae6916b18be792355e3a24fa122912d61611f8f Mon Sep 17 00:00:00 2001 From: dougbtv Date: Thu, 10 Oct 2024 16:15:13 -0400 Subject: [PATCH] Tests change to net-attach-def library for gateway determination of default=true in status This is a test change to the library for determination of the default=true interface in the network-status annotation when multiple interfaces are present in the result. --- pkg/multus/multus.go | 2 ++ .../pkg/utils/net-attach-def.go | 33 ++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pkg/multus/multus.go b/pkg/multus/multus.go index 6ae6243c4..f7b1d4061 100644 --- a/pkg/multus/multus.go +++ b/pkg/multus/multus.go @@ -770,6 +770,8 @@ func CmdAdd(args *skel.CmdArgs, exec invoke.Exec, kubeClient *k8s.ClientInfo) (c } } + logging.Debugf("!bang the whole result ON ADD: %+v", result) + return result, nil } diff --git a/vendor/github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils/net-attach-def.go b/vendor/github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils/net-attach-def.go index 06502e81c..75ec553a2 100644 --- a/vendor/github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils/net-attach-def.go +++ b/vendor/github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/utils/net-attach-def.go @@ -136,6 +136,11 @@ func CreateNetworkStatuses(r cnitypes.Result, networkName string, defaultNetwork return nil, fmt.Errorf("error converting the type.Result to cni100.Result: %v", err) } + if len(result.Interfaces) == 1 { + networkStatus, err := CreateNetworkStatus(r, networkName, defaultNetwork, dev) + return []*v1.NetworkStatus{networkStatus}, err + } + // Discover default routes upfront and reuse them if necessary. var useDefaultRoute []string for _, route := range result.Routes { @@ -147,14 +152,40 @@ func CreateNetworkStatuses(r cnitypes.Result, networkName string, defaultNetwork // Same for DNS v1dns := convertDNS(result.DNS) + // Check for a gateway-associated interface, we'll use this later if we did to mark as the default. + gatewayInterfaceIndex := -1 + if defaultNetwork { + for _, ipConfig := range result.IPs { + if ipConfig.Gateway != nil && ipConfig.Interface != nil { + // Keep the index of the first interface that has a gateway + gatewayInterfaceIndex = *ipConfig.Interface + break + } + } + } + // Initialize NetworkStatus for each container interface (e.g. with sandbox present) indexOfFoundPodInterface := 0 foundFirstSandboxIface := false + didSetDefault := false for i, iface := range result.Interfaces { if iface.Sandbox != "" { + isDefault := false // default to false by default + + // If there's a gateway listed for this interface index found in the ips, we mark that interface as default, the first one we find. + if defaultNetwork && i == gatewayInterfaceIndex && !didSetDefault { + isDefault = true + didSetDefault = true + } + + // Otherwise, if we didn't find it, we use the first sandbox interface. + if defaultNetwork && gatewayInterfaceIndex == -1 && !foundFirstSandboxIface { + isDefault = true + } + ns := &v1.NetworkStatus{ Name: networkName, - Default: defaultNetwork && !foundFirstSandboxIface, + Default: isDefault, Interface: iface.Name, Mac: iface.Mac, Mtu: iface.Mtu,