Skip to content

Commit a875692

Browse files
Handle nilgateway scenario
1 parent 8176411 commit a875692

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

azure-ipam/ipam.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,14 @@ func (p *IPAMPlugin) CmdAdd(args *cniSkel.CmdArgs) error {
136136
Mask: net.CIDRMask(ipNet.Bits(), 128), // nolint
137137
}
138138
}
139-
ipConfig.Gateway = (*gatewayIP)[i]
139+
140+
if gatewayIP != nil && len(*gatewayIP) > i {
141+
ipConfig.Gateway = (*gatewayIP)[i]
142+
} else {
143+
p.logger.Warn("No gateway IP returned from CNS, setting to nil", zap.Any("gatewayIP", gatewayIP), zap.Int("index", i))
144+
ipConfig.Gateway = nil
145+
}
146+
140147
cniResult.IPs[i] = ipConfig
141148
}
142149

azure-ipam/ipam_test.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,33 @@ func (c *MockCNSClient) RequestIPAddress(ctx context.Context, ipconfig cns.IPCon
8585
},
8686
}
8787
return result, nil
88+
case "invalidGateway":
89+
result := &cns.IPConfigResponse{
90+
PodIpInfo: cns.PodIpInfo{
91+
PodIPConfig: cns.IPSubnet{
92+
IPAddress: "10.0.1.10",
93+
PrefixLength: 24,
94+
},
95+
NetworkContainerPrimaryIPConfig: cns.IPConfiguration{
96+
IPSubnet: cns.IPSubnet{
97+
IPAddress: "10.0.1.0",
98+
PrefixLength: 24,
99+
},
100+
DNSServers: nil,
101+
GatewayIPAddress: "invalidgatewayip",
102+
},
103+
HostPrimaryIPInfo: cns.HostIPInfo{
104+
Gateway: "invalidgatewayip",
105+
PrimaryIP: "10.0.0.1",
106+
Subnet: "10.0.0.0/24",
107+
},
108+
},
109+
Response: cns.Response{
110+
ReturnCode: 0,
111+
Message: "",
112+
},
113+
}
114+
return result, nil
88115
default:
89116
result := &cns.IPConfigResponse{
90117
PodIpInfo: cns.PodIpInfo{
@@ -120,7 +147,7 @@ func (c *MockCNSClient) RequestIPs(ctx context.Context, ipconfig cns.IPConfigsRe
120147
switch ipconfig.InfraContainerID {
121148
case "failRequestCNSArgs":
122149
return nil, errFoo
123-
case "happyArgsSingle", "failProcessCNSRespSingleIP", "failRequestCNSArgsSingleIP", "nilGateway":
150+
case "happyArgsSingle", "failProcessCNSRespSingleIP", "failRequestCNSArgsSingleIP", "nilGateway", "invalidGateway":
124151
e := &client.CNSClientError{}
125152
e.Code = types.UnsupportedAPI
126153
e.Err = errUnsupportedAPI
@@ -407,8 +434,27 @@ func TestCmdAdd(t *testing.T) {
407434
wantErr: false,
408435
},
409436
{
410-
name: "CNI add with nil gateway IP",
411-
args: buildArgs("nilGateway", happyPodArgs, happyNetConfByteArr),
437+
name: "CNI add with nil gateway IP",
438+
args: buildArgs("nilGateway", happyPodArgs, happyNetConfByteArr),
439+
want: &types100.Result{
440+
CNIVersion: "1.0.0",
441+
Interfaces: nil,
442+
IPs: []*types100.IPConfig{
443+
{
444+
Address: net.IPNet{
445+
IP: net.IPv4(10, 0, 1, 10),
446+
Mask: net.CIDRMask(24, 32),
447+
},
448+
Gateway: nil, // No gateway
449+
},
450+
},
451+
DNS: cniTypes.DNS{},
452+
},
453+
wantErr: false,
454+
},
455+
{
456+
name: "CNI add with invalid gateway IP",
457+
args: buildArgs("invalidGateway", happyPodArgs, happyNetConfByteArr),
412458
wantErr: true,
413459
},
414460
{

azure-ipam/ipconfig/ipconfig.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
const (
16-
defaultV6Gateway = "fe80::1234:5678:9abc"
16+
defaultV6Gateway = "fe80::1234:5678:9abc"
1717
)
1818

1919
func CreateOrchestratorContext(args *cniSkel.CmdArgs) ([]byte, error) {
@@ -93,9 +93,13 @@ func ProcessIPConfigsResp(resp *cns.IPConfigsResponse) (*[]netip.Prefix, *[]net.
9393
gatewayStr = defaultV6Gateway
9494
}
9595

96-
gatewayIP = net.ParseIP(gatewayStr)
97-
if gatewayIP == nil {
98-
return nil, nil, errors.Errorf("failed to parse gateway IP %q for pod ip %s", gatewayStr, resp.PodIPInfo[i].PodIPConfig.IPAddress)
96+
if gatewayStr != "" {
97+
gatewayIP = net.ParseIP(gatewayStr)
98+
if gatewayIP == nil {
99+
return nil, nil, errors.Errorf("failed to parse gateway IP %q for pod ip %s", gatewayStr, resp.PodIPInfo[i].PodIPConfig.IPAddress)
100+
}
101+
} else {
102+
gatewayIP = nil
99103
}
100104
gatewaysIPs[i] = gatewayIP
101105
}

0 commit comments

Comments
 (0)