Skip to content

Commit 90e0999

Browse files
authored
support CIDR notation in the NC PrimaryIP (#1320)
Signed-off-by: Evan Baker <[email protected]>
1 parent a096e02 commit 90e0999

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

cns/singletenantcontroller/conversion.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package kubecontroller
22

33
import (
44
"net"
5+
"net/netip" //nolint:gci // netip breaks gci??
56
"strconv"
7+
"strings"
68

79
"github.com/Azure/azure-container-networking/cns"
810
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
@@ -32,21 +34,25 @@ func CRDStatusToNCRequest(status *v1alpha.NodeNetworkConfigStatus) (cns.CreateNe
3234

3335
nc := status.NetworkContainers[0]
3436

35-
ip := net.ParseIP(nc.PrimaryIP)
36-
if ip == nil {
37-
return cns.CreateNetworkContainerRequest{}, errors.Wrapf(ErrInvalidPrimaryIP, "IP: %s", nc.PrimaryIP)
37+
primaryIP := nc.PrimaryIP
38+
// if the PrimaryIP is not a CIDR, append a /32
39+
if !strings.Contains(primaryIP, "/") {
40+
primaryIP += "/32"
3841
}
3942

40-
_, ipNet, err := net.ParseCIDR(nc.SubnetAddressSpace)
43+
primaryPrefix, err := netip.ParsePrefix(primaryIP)
4144
if err != nil {
42-
return cns.CreateNetworkContainerRequest{}, errors.Wrapf(err, "invalid SubnetAddressSpace %s", nc.SubnetAddressSpace)
45+
return cns.CreateNetworkContainerRequest{}, errors.Wrapf(err, "IP: %s", primaryIP)
4346
}
4447

45-
size, _ := ipNet.Mask.Size()
48+
secondaryPrefix, err := netip.ParsePrefix(nc.SubnetAddressSpace)
49+
if err != nil {
50+
return cns.CreateNetworkContainerRequest{}, errors.Wrapf(err, "invalid SubnetAddressSpace %s", nc.SubnetAddressSpace)
51+
}
4652

4753
subnet := cns.IPSubnet{
48-
IPAddress: ip.String(),
49-
PrefixLength: uint8(size),
54+
IPAddress: primaryPrefix.Addr().String(),
55+
PrefixLength: uint8(secondaryPrefix.Bits()),
5056
}
5157

5258
secondaryIPConfigs := map[string]cns.SecondaryIPConfig{}

cns/singletenantcontroller/conversion_test.go

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package kubecontroller
22

33
import (
4-
"reflect"
54
"strconv"
65
"testing"
76

87
"github.com/Azure/azure-container-networking/cns"
98
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
9+
"github.com/stretchr/testify/assert"
1010
)
1111

1212
const (
1313
uuid = "539970a2-c2dd-11ea-b3de-0242ac130004"
1414
defaultGateway = "10.0.0.2"
15-
ipInCIDR = "10.0.0.1/32"
15+
ipIsCIDR = "10.0.0.1/32"
1616
ipMalformed = "10.0.0.0.0"
1717
ncID = "160005ba-cd02-11ea-87d0-0242ac130003"
1818
primaryIP = "10.0.0.1"
@@ -74,24 +74,30 @@ var validRequest = cns.CreateNetworkContainerRequest{
7474
func TestConvertNNCStatusToNCRequest(t *testing.T) {
7575
tests := []struct {
7676
name string
77-
status v1alpha.NodeNetworkConfigStatus
78-
ncreq cns.CreateNetworkContainerRequest
77+
input v1alpha.NodeNetworkConfigStatus
78+
want cns.CreateNetworkContainerRequest
7979
wantErr bool
8080
}{
81+
{
82+
name: "valid",
83+
input: validStatus,
84+
wantErr: false,
85+
want: validRequest,
86+
},
8187
{
8288
name: "no nc",
83-
status: v1alpha.NodeNetworkConfigStatus{},
89+
input: v1alpha.NodeNetworkConfigStatus{},
8490
wantErr: false,
85-
ncreq: cns.CreateNetworkContainerRequest{},
91+
want: cns.CreateNetworkContainerRequest{},
8692
},
8793
{
8894
name: ">1 nc",
89-
status: invalidStatusMultiNC,
95+
input: invalidStatusMultiNC,
9096
wantErr: true,
9197
},
9298
{
9399
name: "malformed primary IP",
94-
status: v1alpha.NodeNetworkConfigStatus{
100+
input: v1alpha.NodeNetworkConfigStatus{
95101
NetworkContainers: []v1alpha.NetworkContainer{
96102
{
97103
PrimaryIP: ipMalformed,
@@ -110,7 +116,7 @@ func TestConvertNNCStatusToNCRequest(t *testing.T) {
110116
},
111117
{
112118
name: "malformed IP assignment",
113-
status: v1alpha.NodeNetworkConfigStatus{
119+
input: v1alpha.NodeNetworkConfigStatus{
114120
NetworkContainers: []v1alpha.NetworkContainer{
115121
{
116122
PrimaryIP: primaryIP,
@@ -129,34 +135,41 @@ func TestConvertNNCStatusToNCRequest(t *testing.T) {
129135
},
130136
{
131137
name: "IP is CIDR",
132-
status: v1alpha.NodeNetworkConfigStatus{
138+
input: v1alpha.NodeNetworkConfigStatus{
133139
NetworkContainers: []v1alpha.NetworkContainer{
134140
{
135-
PrimaryIP: ipInCIDR,
141+
PrimaryIP: ipIsCIDR,
136142
ID: ncID,
137143
IPAssignments: []v1alpha.IPAssignment{
138144
{
139145
Name: uuid,
140146
IP: testSecIP,
141147
},
142148
},
149+
SubnetName: subnetName,
150+
DefaultGateway: defaultGateway,
143151
SubnetAddressSpace: subnetAddressSpace,
152+
Version: version,
144153
},
145154
},
155+
Scaler: v1alpha.Scaler{
156+
BatchSize: 1,
157+
},
146158
},
147-
wantErr: true,
159+
wantErr: false,
160+
want: validRequest,
148161
},
149162
{
150163
name: "IP assignment is CIDR",
151-
status: v1alpha.NodeNetworkConfigStatus{
164+
input: v1alpha.NodeNetworkConfigStatus{
152165
NetworkContainers: []v1alpha.NetworkContainer{
153166
{
154167
PrimaryIP: primaryIP,
155168
ID: ncID,
156169
IPAssignments: []v1alpha.IPAssignment{
157170
{
158171
Name: uuid,
159-
IP: ipInCIDR,
172+
IP: ipIsCIDR,
160173
},
161174
},
162175
SubnetAddressSpace: subnetAddressSpace,
@@ -167,7 +180,7 @@ func TestConvertNNCStatusToNCRequest(t *testing.T) {
167180
},
168181
{
169182
name: "address space is not CIDR",
170-
status: v1alpha.NodeNetworkConfigStatus{
183+
input: v1alpha.NodeNetworkConfigStatus{
171184
NetworkContainers: []v1alpha.NetworkContainer{
172185
{
173186
PrimaryIP: primaryIP,
@@ -184,24 +197,17 @@ func TestConvertNNCStatusToNCRequest(t *testing.T) {
184197
},
185198
wantErr: true,
186199
},
187-
{
188-
name: "valid",
189-
status: validStatus,
190-
wantErr: false,
191-
ncreq: validRequest,
192-
},
193200
}
194201
for _, tt := range tests {
195202
tt := tt
196203
t.Run(tt.name, func(t *testing.T) {
197-
got, err := CRDStatusToNCRequest(&tt.status)
198-
if (err != nil) != tt.wantErr {
199-
t.Errorf("ConvertNNCStatusToNCRequest() error = %v, wantErr %v", err, tt.wantErr)
204+
got, err := CRDStatusToNCRequest(&tt.input)
205+
if tt.wantErr {
206+
assert.Error(t, err)
200207
return
201208
}
202-
if !reflect.DeepEqual(got, tt.ncreq) {
203-
t.Errorf("ConvertNNCStatusToNCRequest()\nhave: %+v\n want: %+v", got, tt.ncreq)
204-
}
209+
assert.NoError(t, err)
210+
assert.EqualValues(t, tt.want, got)
205211
})
206212
}
207213
}

0 commit comments

Comments
 (0)