From c68d79b6776bcccac704f8d77c7e2305d92798a5 Mon Sep 17 00:00:00 2001 From: sridhartigera <63839878+sridhartigera@users.noreply.github.com> Date: Wed, 18 Dec 2024 01:46:57 -0800 Subject: [PATCH] Fix netlink leak (#9609) --- cni-plugin/internal/pkg/testutils/utils_linux.go | 1 + cni-plugin/tests/calico_cni_k8s_test.go | 1 + cni-plugin/tests/calico_cni_test.go | 1 + felix/dataplane/linux/int_dataplane.go | 1 + felix/fv/etcd_restart_test.go | 1 + felix/fv/ipip_test.go | 1 + 6 files changed, 6 insertions(+) diff --git a/cni-plugin/internal/pkg/testutils/utils_linux.go b/cni-plugin/internal/pkg/testutils/utils_linux.go index e1b40fc5532..f88a812cd0c 100644 --- a/cni-plugin/internal/pkg/testutils/utils_linux.go +++ b/cni-plugin/internal/pkg/testutils/utils_linux.go @@ -287,6 +287,7 @@ func RunCNIPluginWithId( return err } + defer nlHandle.Close() contVeth, err = nlHandle.LinkByName(ifName) if err != nil { return err diff --git a/cni-plugin/tests/calico_cni_k8s_test.go b/cni-plugin/tests/calico_cni_k8s_test.go index 4c7720ff9c4..26dad3dead0 100644 --- a/cni-plugin/tests/calico_cni_k8s_test.go +++ b/cni-plugin/tests/calico_cni_k8s_test.go @@ -315,6 +315,7 @@ var _ = Describe("Kubernetes CNI tests", func() { // Assert if the host side route is programmed correctly. nlHandle, err := netlink.NewHandle(syscall.NETLINK_ROUTE) Expect(err).ShouldNot(HaveOccurred()) + defer nlHandle.Close() hostRoutes, err := netlinkutils.RouteListRetryEINTR(nlHandle, hostVeth, syscall.AF_INET) Expect(err).ShouldNot(HaveOccurred()) Expect(hostRoutes[0]).Should(Equal(netlink.Route{ diff --git a/cni-plugin/tests/calico_cni_test.go b/cni-plugin/tests/calico_cni_test.go index 31bdaa3a851..a06f9eea8da 100644 --- a/cni-plugin/tests/calico_cni_test.go +++ b/cni-plugin/tests/calico_cni_test.go @@ -159,6 +159,7 @@ var _ = Describe("CalicoCni", func() { // Assert if the host side route is programmed correctly. nlHandle, err := netlink.NewHandle(syscall.NETLINK_ROUTE) Expect(err).ShouldNot(HaveOccurred()) + defer nlHandle.Close() hostRoutes, err := netlinkutils.RouteListRetryEINTR(nlHandle, hostVeth, syscall.AF_INET) Expect(err).ShouldNot(HaveOccurred()) Expect(hostRoutes[0]).Should(Equal(netlink.Route{ diff --git a/felix/dataplane/linux/int_dataplane.go b/felix/dataplane/linux/int_dataplane.go index 71df22c00fa..7838b66c5d6 100644 --- a/felix/dataplane/linux/int_dataplane.go +++ b/felix/dataplane/linux/int_dataplane.go @@ -1124,6 +1124,7 @@ func findHostMTU(matchRegex *regexp.Regexp) (int, error) { return 0, err } + defer nlHandle.Delete() links, err := nlHandle.LinkList() if err != nil { log.WithError(err).Error("Failed to list interfaces. Unable to auto-detect MTU.") diff --git a/felix/fv/etcd_restart_test.go b/felix/fv/etcd_restart_test.go index 578c74f6b4e..94eccf0a248 100644 --- a/felix/fv/etcd_restart_test.go +++ b/felix/fv/etcd_restart_test.go @@ -63,6 +63,7 @@ var _ = Context("etcd connection interruption", func() { if err != nil { return err } + defer nlHandle.Close() links, err := netlinkutils.LinkListRetryEINTR(nlHandle) if err != nil { return err diff --git a/felix/fv/ipip_test.go b/felix/fv/ipip_test.go index 8e00223cdb1..c17d36271b5 100644 --- a/felix/fv/ipip_test.go +++ b/felix/fv/ipip_test.go @@ -73,6 +73,7 @@ var _ = infrastructure.DatastoreDescribe("_BPF-SAFE_ IPIP topology before adding if err != nil { return err } + defer nlHandle.Close() links, err := netlinkutils.LinkListRetryEINTR(nlHandle) if err != nil { return err