Skip to content

Commit 8a105b9

Browse files
authored
Returning IPAddress instead of IPPrefix in CNI ADD response (#2038)
* Returning IPAddress instead of IPPrefix in CNI ADD response * Addressing review comments. * Resolving review comments * Using constants for prefixlength
1 parent 53ea9ec commit 8a105b9

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

cni/network/network.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"net"
1111
"os"
12+
"strconv"
1213
"time"
1314

1415
"github.com/Azure/azure-container-networking/aitelemetry"
@@ -41,6 +42,8 @@ const (
4142
// Supported IP version. Currently support only IPv4
4243
ipamV6 = "azure-vnet-ipamv6"
4344
defaultRequestTimeout = 15 * time.Second
45+
ipv4FullMask = 32
46+
ipv6FullMask = 128
4447
)
4548

4649
// CNI Operation Types
@@ -1341,20 +1344,30 @@ func convertNnsToCniResult(
13411344

13421345
intIndex := i
13431346
for _, ip := range ni.Ipaddresses {
1347+
ipAddr := net.ParseIP(ip.Ip)
13441348

1345-
ipWithPrefix := fmt.Sprintf("%s/%s", ip.Ip, ip.PrefixLength)
1346-
_, ipNet, err := net.ParseCIDR(ipWithPrefix)
1349+
prefixLength, err := strconv.Atoi(ip.PrefixLength)
13471350
if err != nil {
1348-
log.Logger.Error("Error while converting to cni result",
1351+
log.Logger.Error("Error parsing prefix length while converting to cni result",
1352+
zap.String("prefixLength", ip.PrefixLength),
13491353
zap.String("operation", operationName),
13501354
zap.String("pod", podName),
13511355
zap.Error(err))
13521356
continue
13531357
}
13541358

1359+
address := net.IPNet{
1360+
IP: ipAddr,
1361+
Mask: net.CIDRMask(prefixLength, ipv6FullMask),
1362+
}
1363+
1364+
if ipAddr.To4() != nil {
1365+
address.Mask = net.CIDRMask(prefixLength, ipv4FullMask)
1366+
}
1367+
13551368
gateway := net.ParseIP(ip.DefaultGateway)
13561369
ipConfig := &cniTypesCurr.IPConfig{
1357-
Address: *ipNet,
1370+
Address: address,
13581371
Gateway: gateway,
13591372
Interface: &intIndex,
13601373
}

0 commit comments

Comments
 (0)