diff --git a/pkg/netutils/netutils.go b/pkg/netutils/netutils.go index 9f71d83da..bdc2710f1 100644 --- a/pkg/netutils/netutils.go +++ b/pkg/netutils/netutils.go @@ -206,7 +206,12 @@ func deleteDefaultGWResult(result map[string]interface{}, ipv4, ipv6 bool) (map[ return nil, err } } - result["routes"] = routes + + if len(routes) == 0 { + delete(result, "routes") + } else { + result["routes"] = routes + } return result, nil } diff --git a/pkg/netutils/netutils_test.go b/pkg/netutils/netutils_test.go index 14e26029a..6921d144b 100644 --- a/pkg/netutils/netutils_test.go +++ b/pkg/netutils/netutils_test.go @@ -641,6 +641,39 @@ var _ = Describe("netutil cnicache function testing", func() { Expect(len(result.Result.Routes)).To(Equal(5)) }) + It("verify ipv4 default gateway from single routes is removed/added from CNI 1.0.0 results", func() { + origResult := []byte(`{ + "kind": "cniCacheV1", + "result": { + "cniVersion": "1.0.0", + "dns": {}, + "interfaces": [ + { + "mac": "0a:c2:e6:3d:45:17", + "name": "net1", + "sandbox": "/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5" + } + ], + "ips": [ + { + "address": "10.1.1.103/24", + "interface": 0 + } + ], + "routes": [ + { + "dst": "0.0.0.0/0", + "gw": "10.1.1.1" + } + ] + } +}`) + newResult1, err := deleteDefaultGWCacheBytes(origResult, true, false) + Expect(err).NotTo(HaveOccurred()) + _, err = addDefaultGWCacheBytes(newResult1, []net.IP{net.ParseIP("10.1.1.1")}) + Expect(err).NotTo(HaveOccurred()) + }) + It("verify ipv6 default gateway is removed from CNI 1.0.0 results", func() { origResult := []byte(`{ "kind": "cniCacheV1", @@ -713,6 +746,39 @@ var _ = Describe("netutil cnicache function testing", func() { Expect(len(result.Result.Routes)).To(Equal(5)) }) + It("verify ipv6 default gateway from single routes is removed/added from CNI 1.0.0 results", func() { + origResult := []byte(`{ + "kind": "cniCacheV1", + "result": { + "cniVersion": "1.0.0", + "dns": {}, + "interfaces": [ + { + "mac": "0a:c2:e6:3d:45:17", + "name": "net1", + "sandbox": "/run/netns/bb74fcb9-989a-4589-b2df-ddd0384a8ee5" + } + ], + "ips": [ + { + "address": "10::1:1:103/64", + "interface": 0 + } + ], + "routes": [ + { + "dst": "::0/0", + "gw": "10::1:1:1" + } + ] + } +}`) + newResult1, err := deleteDefaultGWCacheBytes(origResult, false, true) + Expect(err).NotTo(HaveOccurred()) + _, err = addDefaultGWCacheBytes(newResult1, []net.IP{net.ParseIP("10::1:1:1")}) + Expect(err).NotTo(HaveOccurred()) + }) + It("verify ipv4 default gateway is added to CNI 0.1.0/0.2.0 results without routes", func() { origResult := []byte(`{ "kind": "cniCacheV1",