From 0e38563877d7edb66bc6617fc3a4afc48c6502c7 Mon Sep 17 00:00:00 2001 From: ronak-31 Date: Thu, 29 Jun 2023 13:56:26 +0530 Subject: [PATCH 1/4] Returning IPAddress instead of IPPrefix in CNI ADD response --- cni/network/network.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cni/network/network.go b/cni/network/network.go index 3f7e02054a..b478a1c980 100644 --- a/cni/network/network.go +++ b/cni/network/network.go @@ -9,6 +9,7 @@ import ( "fmt" "net" "os" + "strconv" "time" "github.com/Azure/azure-container-networking/aitelemetry" @@ -1258,18 +1259,25 @@ func convertNnsToCniResult( intIndex := i for _, ip := range ni.Ipaddresses { + ipAddr := net.ParseIP(ip.Ip) - ipWithPrefix := fmt.Sprintf("%s/%s", ip.Ip, ip.PrefixLength) - _, ipNet, err := net.ParseCIDR(ipWithPrefix) + var address net.IPNet + prefixLength, err := strconv.Atoi(ip.PrefixLength) if err != nil { - log.Printf("Error while converting to cni result for %s operation on pod %s. %s", - operationName, podName, err) + log.Printf("Error parsing prefixLength (%s) for %s operation on pod %s, err:%s.", + ip.PrefixLength, operationName, podName, err) continue } + if ipAddr.To4() != nil { + address = net.IPNet{IP: ipAddr, Mask: net.CIDRMask(prefixLength, 32)} + } else { + address = net.IPNet{IP: ipAddr, Mask: net.CIDRMask(prefixLength, 128)} + } + gateway := net.ParseIP(ip.DefaultGateway) ipConfig := &cniTypesCurr.IPConfig{ - Address: *ipNet, + Address: address, Gateway: gateway, Interface: &intIndex, } From 85d60fde0c213f8b7e76ee5b7b3cf51b379dd88d Mon Sep 17 00:00:00 2001 From: ronak-31 Date: Tue, 4 Jul 2023 10:37:01 +0530 Subject: [PATCH 2/4] Addressing review comments. --- cni/network/network.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cni/network/network.go b/cni/network/network.go index b478a1c980..0354250abc 100644 --- a/cni/network/network.go +++ b/cni/network/network.go @@ -1261,7 +1261,6 @@ func convertNnsToCniResult( for _, ip := range ni.Ipaddresses { ipAddr := net.ParseIP(ip.Ip) - var address net.IPNet prefixLength, err := strconv.Atoi(ip.PrefixLength) if err != nil { log.Printf("Error parsing prefixLength (%s) for %s operation on pod %s, err:%s.", @@ -1269,6 +1268,7 @@ func convertNnsToCniResult( continue } + var address net.IPNet if ipAddr.To4() != nil { address = net.IPNet{IP: ipAddr, Mask: net.CIDRMask(prefixLength, 32)} } else { From e37127093efb1338435ac72c6236480bb399cee0 Mon Sep 17 00:00:00 2001 From: ronak-31 Date: Fri, 14 Jul 2023 23:55:07 +0530 Subject: [PATCH 3/4] Resolving review comments --- cni/network/network.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cni/network/network.go b/cni/network/network.go index efa1ab4dc5..748704b65a 100644 --- a/cni/network/network.go +++ b/cni/network/network.go @@ -1354,11 +1354,13 @@ func convertNnsToCniResult( continue } - var address net.IPNet + address := net.IPNet{ + IP: ipAddr, + Mask: net.CIDRMask(prefixLength, 128), + } + if ipAddr.To4() != nil { - address = net.IPNet{IP: ipAddr, Mask: net.CIDRMask(prefixLength, 32)} - } else { - address = net.IPNet{IP: ipAddr, Mask: net.CIDRMask(prefixLength, 128)} + address.Mask = net.CIDRMask(prefixLength, 32) } gateway := net.ParseIP(ip.DefaultGateway) From 5d43dc427bd3b030a3fe5bd07f3e8e7f4c348b4e Mon Sep 17 00:00:00 2001 From: ronak-31 Date: Sat, 15 Jul 2023 04:09:27 +0530 Subject: [PATCH 4/4] Using constants for prefixlength --- cni/network/network.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cni/network/network.go b/cni/network/network.go index 748704b65a..180a0def86 100644 --- a/cni/network/network.go +++ b/cni/network/network.go @@ -42,6 +42,8 @@ const ( // Supported IP version. Currently support only IPv4 ipamV6 = "azure-vnet-ipamv6" defaultRequestTimeout = 15 * time.Second + ipv4FullMask = 32 + ipv6FullMask = 128 ) // CNI Operation Types @@ -1356,11 +1358,11 @@ func convertNnsToCniResult( address := net.IPNet{ IP: ipAddr, - Mask: net.CIDRMask(prefixLength, 128), + Mask: net.CIDRMask(prefixLength, ipv6FullMask), } if ipAddr.To4() != nil { - address.Mask = net.CIDRMask(prefixLength, 32) + address.Mask = net.CIDRMask(prefixLength, ipv4FullMask) } gateway := net.ParseIP(ip.DefaultGateway)