From bc6c8d5c76c775c3e8088631e7660e8a927b62c6 Mon Sep 17 00:00:00 2001 From: dougbtv Date: Thu, 25 Jul 2024 16:15:50 -0400 Subject: [PATCH 1/2] Updates to use CreateNetworkStauses from net-attach-def client for multiple interfaces in CNI results --- pkg/k8sclient/k8sclient_test.go | 54 ++++++++++++++++++++++----------- pkg/multus/multus.go | 11 ++++--- pkg/types/conf_test.go | 8 ++--- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/pkg/k8sclient/k8sclient_test.go b/pkg/k8sclient/k8sclient_test.go index 8bc712395..7e9a9332c 100644 --- a/pkg/k8sclient/k8sclient_test.go +++ b/pkg/k8sclient/k8sclient_test.go @@ -1199,11 +1199,14 @@ users: delegate, err := types.LoadDelegateNetConf([]byte(conf), nil, "0000:00:00.0", "") Expect(err).NotTo(HaveOccurred()) - delegateNetStatus, err := netutils.CreateNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin, nil) - GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus) + delegateNetStatuses, err := netutils.CreateNetworkStatuses(result, delegate.Conf.Name, delegate.MasterPlugin, nil) + GinkgoT().Logf("delegateNetStatuses %+v\n", delegateNetStatuses) Expect(err).NotTo(HaveOccurred()) - netstatus := []nettypes.NetworkStatus{*delegateNetStatus} + netstatus := make([]nettypes.NetworkStatus, 0) + for _, status := range delegateNetStatuses { + netstatus = append(netstatus, *status) + } fakePod := testutils.NewFakePod(fakePodName, "kube-system/net1", "") @@ -1254,11 +1257,14 @@ users: delegate, err := types.LoadDelegateNetConf([]byte(conf), nil, "0000:00:00.0", "") Expect(err).NotTo(HaveOccurred()) - delegateNetStatus, err := netutils.CreateNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin, nil) - GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus) + delegateNetStatuses, err := netutils.CreateNetworkStatuses(result, delegate.Conf.Name, delegate.MasterPlugin, nil) + GinkgoT().Logf("delegateNetStatuses %+v\n", delegateNetStatuses) Expect(err).NotTo(HaveOccurred()) - netstatus := []nettypes.NetworkStatus{*delegateNetStatus} + netstatus := make([]nettypes.NetworkStatus, 0) + for _, status := range delegateNetStatuses { + netstatus = append(netstatus, *status) + } fakePod := testutils.NewFakePod(fakePodName, "kube-system/net1", "") @@ -1312,11 +1318,14 @@ users: delegate, err := types.LoadDelegateNetConf([]byte(conf), nil, "0000:00:00.0", "") Expect(err).NotTo(HaveOccurred()) - delegateNetStatus, err := netutils.CreateNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin, nil) - GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus) + delegateNetStatuses, err := netutils.CreateNetworkStatuses(result, delegate.Conf.Name, delegate.MasterPlugin, nil) + GinkgoT().Logf("delegateNetStatuses %+v\n", delegateNetStatuses) Expect(err).NotTo(HaveOccurred()) - netstatus := []nettypes.NetworkStatus{*delegateNetStatus} + netstatus := make([]nettypes.NetworkStatus, 0) + for _, status := range delegateNetStatuses { + netstatus = append(netstatus, *status) + } fakePod := testutils.NewFakePod(fakePodName, "kube-system/net1", "") @@ -1394,11 +1403,14 @@ users: delegate, err := types.LoadDelegateNetConf([]byte(conf), nil, "0000:00:00.0", "") Expect(err).NotTo(HaveOccurred()) - delegateNetStatus, err := netutils.CreateNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin, nil) - GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus) + delegateNetStatuses, err := netutils.CreateNetworkStatuses(result, delegate.Conf.Name, delegate.MasterPlugin, nil) + GinkgoT().Logf("delegateNetStatuses %+v\n", delegateNetStatuses) Expect(err).NotTo(HaveOccurred()) - netstatus := []nettypes.NetworkStatus{*delegateNetStatus} + netstatus := make([]nettypes.NetworkStatus, 0) + for _, status := range delegateNetStatuses { + netstatus = append(netstatus, *status) + } fakePod := testutils.NewFakePod(fakePodName, "kube-system/net1", "") @@ -1450,11 +1462,14 @@ users: delegate, err := types.LoadDelegateNetConf([]byte(conf), nil, "", "") Expect(err).NotTo(HaveOccurred()) - delegateNetStatus, err := netutils.CreateNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin, nil) - GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus) + delegateNetStatuses, err := netutils.CreateNetworkStatuses(result, delegate.Conf.Name, delegate.MasterPlugin, nil) + GinkgoT().Logf("delegateNetStatuses %+v\n", delegateNetStatuses) Expect(err).NotTo(HaveOccurred()) - netstatus := []nettypes.NetworkStatus{*delegateNetStatus} + netstatus := make([]nettypes.NetworkStatus, 0) + for _, status := range delegateNetStatuses { + netstatus = append(netstatus, *status) + } fakePod := testutils.NewFakePod(fakePodName, "kube-system/net1", "") @@ -1505,11 +1520,14 @@ users: delegate, err := types.LoadDelegateNetConf([]byte(conf), nil, "0000:00:00.0", "") Expect(err).NotTo(HaveOccurred()) - delegateNetStatus, err := netutils.CreateNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin, nil) - GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus) + delegateNetStatuses, err := netutils.CreateNetworkStatuses(result, delegate.Conf.Name, delegate.MasterPlugin, nil) + GinkgoT().Logf("delegateNetStatuses %+v\n", delegateNetStatuses) Expect(err).NotTo(HaveOccurred()) - netstatus := []nettypes.NetworkStatus{*delegateNetStatus} + netstatus := make([]nettypes.NetworkStatus, 0) + for _, status := range delegateNetStatuses { + netstatus = append(netstatus, *status) + } fakePod := testutils.NewFakePod(fakePodName, "kube-system/net1", "") diff --git a/pkg/multus/multus.go b/pkg/multus/multus.go index 5452d9e7e..6ae6243c4 100644 --- a/pkg/multus/multus.go +++ b/pkg/multus/multus.go @@ -738,15 +738,18 @@ func CmdAdd(args *skel.CmdArgs, exec invoke.Exec, kubeClient *k8s.ClientInfo) (c logging.Debugf("CmdAdd: getDelegateDeviceInfo returned an error - err=%v", err) } - // create the network status, only in case Multus as kubeconfig + // Create the network statuses, only in case Multus has kubeconfig if kubeClient != nil && kc != nil { if !types.CheckSystemNamespaces(string(k8sArgs.K8S_POD_NAME), n.SystemNamespaces) { - delegateNetStatus, err := nadutils.CreateNetworkStatus(tmpResult, delegate.Name, delegate.MasterPlugin, devinfo) + delegateNetStatuses, err := nadutils.CreateNetworkStatuses(tmpResult, delegate.Name, delegate.MasterPlugin, devinfo) if err != nil { - return nil, cmdErr(k8sArgs, "error setting network status: %v", err) + return nil, cmdErr(k8sArgs, "error setting network statuses: %v", err) } - netStatus = append(netStatus, *delegateNetStatus) + // Append all returned statuses after dereferencing each + for _, status := range delegateNetStatuses { + netStatus = append(netStatus, *status) + } } } else if devinfo != nil { // Warn that devinfo exists but could not add it to downwards API diff --git a/pkg/types/conf_test.go b/pkg/types/conf_test.go index b47821632..a0ac3a666 100644 --- a/pkg/types/conf_test.go +++ b/pkg/types/conf_test.go @@ -704,9 +704,9 @@ var _ = Describe("config operations", func() { delegate, err := LoadDelegateNetConf([]byte(conf), nil, "0000:00:00.0", "") Expect(err).NotTo(HaveOccurred()) - delegateNetStatus, err := netutils.CreateNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin, nil) + delegateNetStatuses, err := netutils.CreateNetworkStatuses(result, delegate.Conf.Name, delegate.MasterPlugin, nil) - GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus) + GinkgoT().Logf("delegateNetStatuses %+v\n", delegateNetStatuses) Expect(err).NotTo(HaveOccurred()) }) @@ -737,9 +737,9 @@ var _ = Describe("config operations", func() { delegate, err := LoadDelegateNetConf([]byte(conf), nil, "0000:00:00.0", "") Expect(err).NotTo(HaveOccurred()) fmt.Println("result.Version: ", result.Version()) - delegateNetStatus, err := netutils.CreateNetworkStatus(result, delegate.Conf.Name, delegate.MasterPlugin, nil) + delegateNetStatuses, err := netutils.CreateNetworkStatuses(result, delegate.Conf.Name, delegate.MasterPlugin, nil) - GinkgoT().Logf("delegateNetStatus %+v\n", delegateNetStatus) + GinkgoT().Logf("delegateNetStatuses %+v\n", delegateNetStatuses) Expect(err).To(HaveOccurred()) }) From a28d1e469363f373969bfa61e0dc0d9c32186179 Mon Sep 17 00:00:00 2001 From: dougbtv Date: Thu, 1 Aug 2024 13:35:34 -0400 Subject: [PATCH 2/2] [bump] Bumps net-attach-def client lib to v1.7.1 --- go.mod | 2 +- go.sum | 4 +- .../pkg/utils/net-attach-def.go | 61 +++++++++++++++++++ vendor/modules.txt | 2 +- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 531d4c635..5dde85258 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/containernetworking/plugins v1.1.0 github.com/fsnotify/fsnotify v1.6.0 github.com/go-logr/logr v1.3.0 // indirect - github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.0 + github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.1 github.com/onsi/ginkgo/v2 v2.13.2 github.com/onsi/gomega v1.30.0 github.com/pkg/errors v0.9.1 // indirect diff --git a/go.sum b/go.sum index d1eca5f55..2dc43c4f3 100644 --- a/go.sum +++ b/go.sum @@ -825,8 +825,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jung-kurt/gofpdf v1.0.0/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes= -github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.0 h1:47q2PIbDYHmOaqLxgGnvpLq96v9UKDsJfNW6j/KbfpQ= -github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.0/go.mod h1:KDX0bPKeuhMakcNzLf2sWXKPNZ30wH01bsY/KJZLxFY= +github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.1 h1:n4FpoJ6aGDx8ULfya/C4ycrMDuPZlf7AtPyrT4+rIP4= +github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.1/go.mod h1:CM7HAH5PNuIsqjMN0fGc1ydM74Uj+0VZFhob620nklw= github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= 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 e865f396b..bd5630005 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 @@ -122,6 +122,67 @@ func GetNetworkStatus(pod *corev1.Pod) ([]v1.NetworkStatus, error) { return netStatuses, err } +// CreateNetworkStatuses creates an array of NetworkStatus from CNI result +// Not to be confused with CreateNetworkStatus (singular) +// This is the preferred method and picks up when CNI ADD results contain multiple container interfaces +func CreateNetworkStatuses(r cnitypes.Result, networkName string, defaultNetwork bool, dev *v1.DeviceInfo) ([]*v1.NetworkStatus, error) { + var networkStatuses []*v1.NetworkStatus + // indexMap is from original CNI result index to networkStatuses index + indexMap := make(map[int]int) + + // Convert whatever the IPAM result was into the current Result type + result, err := cni100.NewResultFromResult(r) + if err != nil { + return nil, fmt.Errorf("error converting the type.Result to cni100.Result: %v", err) + } + + // Discover default routes upfront and reuse them if necessary. + var useDefaultRoute []string + for _, route := range result.Routes { + if isDefaultRoute(route) { + useDefaultRoute = append(useDefaultRoute, route.GW.String()) + } + } + + // Same for DNS + v1dns := convertDNS(result.DNS) + + // Initialize NetworkStatus for each container interface (e.g. with sandbox present) + indexOfFoundPodInterface := 0 + for i, iface := range result.Interfaces { + if iface.Sandbox != "" { + ns := &v1.NetworkStatus{ + Name: networkName, + Default: defaultNetwork, + Interface: iface.Name, + Mac: iface.Mac, + Mtu: iface.Mtu, + IPs: []string{}, + Gateway: useDefaultRoute, + DeviceInfo: dev, + DNS: *v1dns, + } + networkStatuses = append(networkStatuses, ns) + // Map original index to the new slice index + indexMap[i] = indexOfFoundPodInterface + indexOfFoundPodInterface++ + } + } + + // Map IPs to network interface based on index + for _, ipConfig := range result.IPs { + if ipConfig.Interface != nil { + originalIndex := *ipConfig.Interface + if newIndex, ok := indexMap[originalIndex]; ok { + ns := networkStatuses[newIndex] + ns.IPs = append(ns.IPs, ipConfig.Address.IP.String()) + } + } + } + + return networkStatuses, nil +} + // CreateNetworkStatus create NetworkStatus from CNI result func CreateNetworkStatus(r cnitypes.Result, networkName string, defaultNetwork bool, dev *v1.DeviceInfo) (*v1.NetworkStatus, error) { netStatus := &v1.NetworkStatus{} diff --git a/vendor/modules.txt b/vendor/modules.txt index 26cb7a525..0b0127be7 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -114,7 +114,7 @@ github.com/josharian/intern # github.com/json-iterator/go v1.1.12 ## explicit; go 1.12 github.com/json-iterator/go -# github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.0 +# github.com/k8snetworkplumbingwg/network-attachment-definition-client v1.7.1 ## explicit; go 1.21 github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1