Skip to content

Commit

Permalink
Merge pull request #1274 from s1061123/fix/gateway-nil
Browse files Browse the repository at this point in the history
Fix CNI cache update function to prevent nil access
  • Loading branch information
dougbtv authored May 9, 2024
2 parents 4457289 + 5fe1249 commit 7821764
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/netutils/netutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
66 changes: 66 additions & 0 deletions pkg/netutils/netutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 7821764

Please sign in to comment.