Skip to content

Commit 385cc8b

Browse files
authored
Add prefixlength from SubnetAddressRange (#651)
* Add prefixlength from SubnetAddressRange
1 parent df9c086 commit 385cc8b

File tree

5 files changed

+84
-43
lines changed

5 files changed

+84
-43
lines changed

cns/requestcontroller/kubecontroller/crdrequestcontroller_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@ const (
2323
existingNNCName = "nodenetconfig_1"
2424
existingPodName = "pod_1"
2525
hostNetworkPodName = "pod_hostNet"
26-
allocatedPodIP = "10.0.0.1/32"
26+
allocatedPodIP = "10.0.0.2"
2727
allocatedUUID = "539970a2-c2dd-11ea-b3de-0242ac130004"
2828
allocatedUUID2 = "01a5dd00-cd5d-11ea-87d0-0242ac130003"
2929
networkContainerID = "24fcd232-0364-41b0-8027-6e6ef9aeabc6"
3030
existingNamespace = k8sNamespace
3131
nonexistingNNCName = "nodenetconfig_nonexisting"
3232
nonexistingPodName = "pod_nonexisting"
3333
nonexistingNamespace = "namespace_nonexisting"
34-
ncPrimaryIP = "10.0.0.1/32"
34+
ncPrimaryIP = "10.0.0.1"
35+
subnetRange = "10.0.0.0/24"
3536
)
3637

3738
// MockAPI is a mock of kubernete's API server
@@ -607,6 +608,7 @@ func TestInitRequestController(t *testing.T) {
607608
IP: allocatedPodIP,
608609
},
609610
},
611+
SubnetAddressSpace: subnetRange,
610612
},
611613
},
612614
},

cns/requestcontroller/kubecontroller/crdtranslator.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func CRDStatusToNCRequest(crdStatus nnc.NodeNetworkConfigStatus) (cns.CreateNetw
1919
err error
2020
ip net.IP
2121
ipNet *net.IPNet
22-
bits int
22+
size int
2323
numNCsSupported int
2424
numNCs int
2525
)
@@ -37,21 +37,23 @@ func CRDStatusToNCRequest(crdStatus nnc.NodeNetworkConfigStatus) (cns.CreateNetw
3737
ncRequest.NetworkContainerid = nc.ID
3838
ncRequest.NetworkContainerType = cns.Docker
3939

40-
// Convert "10.0.0.1/32" into "10.0.0.1" and prefix length
41-
// Todo, this will be changed soon and only ipaddress will be passed
42-
if ip, ipNet, err = net.ParseCIDR(nc.PrimaryIP); err != nil {
43-
return ncRequest, err
40+
if ip = net.ParseIP(nc.PrimaryIP); ip == nil {
41+
return ncRequest, fmt.Errorf("Invalid PrimaryIP %s:", nc.PrimaryIP)
4442
}
45-
_, bits = ipNet.Mask.Size()
4643

44+
if _, ipNet, err = net.ParseCIDR(nc.SubnetAddressSpace); err != nil {
45+
return ncRequest, fmt.Errorf("Invalid SubnetAddressSpace %s:, err:%s", nc.SubnetAddressSpace, err)
46+
}
47+
48+
size, _ = ipNet.Mask.Size()
4749
ipSubnet.IPAddress = ip.String()
48-
ipSubnet.PrefixLength = uint8(bits)
50+
ipSubnet.PrefixLength = uint8(size)
4951
ncRequest.IPConfiguration.IPSubnet = ipSubnet
5052
ncRequest.IPConfiguration.GatewayIPAddress = nc.DefaultGateway
5153

5254
for _, ipAssignment = range nc.IPAssignments {
53-
if ip, _, err = net.ParseCIDR(ipAssignment.IP); err != nil {
54-
return ncRequest, err
55+
if ip = net.ParseIP(ipAssignment.IP); ip == nil {
56+
return ncRequest, fmt.Errorf("Invalid SecondaryIP %s:", ipAssignment.IP)
5557
}
5658

5759
secondaryIPConfig = cns.SecondaryIPConfig{

cns/requestcontroller/kubecontroller/crdtranslator_test.go

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
)
99

1010
const (
11-
ncID = "160005ba-cd02-11ea-87d0-0242ac130003"
12-
ipCIDR = "10.0.0.1/32"
13-
ipCIDRString = "10.0.0.1"
14-
ipCIDRMaskLength = 32
15-
ipNotCIDR = "10.0.0.1"
16-
ipMalformed = "10.0.0.0.0"
17-
defaultGateway = "10.0.0.2"
18-
subnetName = "subnet1"
11+
ncID = "160005ba-cd02-11ea-87d0-0242ac130003"
12+
primaryIp = "10.0.0.1"
13+
ipInCIDR = "10.0.0.1/32"
14+
ipMalformed = "10.0.0.0.0"
15+
defaultGateway = "10.0.0.2"
16+
subnetName = "subnet1"
17+
subnetAddressSpace = "10.0.0.0/24"
18+
subnetPrefixLen = 24
19+
testSecIp1 = "10.0.0.2"
1920
)
2021

2122
func TestStatusToNCRequestMalformedPrimaryIP(t *testing.T) {
@@ -32,9 +33,10 @@ func TestStatusToNCRequestMalformedPrimaryIP(t *testing.T) {
3233
IPAssignments: []nnc.IPAssignment{
3334
{
3435
Name: allocatedUUID,
35-
IP: ipCIDR,
36+
IP: testSecIp1,
3637
},
3738
},
39+
SubnetAddressSpace: subnetAddressSpace,
3840
},
3941
},
4042
}
@@ -56,14 +58,15 @@ func TestStatusToNCRequestMalformedIPAssignment(t *testing.T) {
5658
status = nnc.NodeNetworkConfigStatus{
5759
NetworkContainers: []nnc.NetworkContainer{
5860
{
59-
PrimaryIP: ipCIDR,
61+
PrimaryIP: primaryIp,
6062
ID: ncID,
6163
IPAssignments: []nnc.IPAssignment{
6264
{
6365
Name: allocatedUUID,
6466
IP: ipMalformed,
6567
},
6668
},
69+
SubnetAddressSpace: subnetAddressSpace,
6770
},
6871
},
6972
}
@@ -76,7 +79,7 @@ func TestStatusToNCRequestMalformedIPAssignment(t *testing.T) {
7679
}
7780
}
7881

79-
func TestStatusToNCRequestPrimaryIPNotCIDR(t *testing.T) {
82+
func TestStatusToNCRequestPrimaryIPInCIDR(t *testing.T) {
8083
var (
8184
status nnc.NodeNetworkConfigStatus
8285
err error
@@ -85,14 +88,15 @@ func TestStatusToNCRequestPrimaryIPNotCIDR(t *testing.T) {
8588
status = nnc.NodeNetworkConfigStatus{
8689
NetworkContainers: []nnc.NetworkContainer{
8790
{
88-
PrimaryIP: ipNotCIDR,
91+
PrimaryIP: ipInCIDR,
8992
ID: ncID,
9093
IPAssignments: []nnc.IPAssignment{
9194
{
9295
Name: allocatedUUID,
93-
IP: ipCIDR,
96+
IP: testSecIp1,
9497
},
9598
},
99+
SubnetAddressSpace: subnetAddressSpace,
96100
},
97101
},
98102
}
@@ -114,14 +118,45 @@ func TestStatusToNCRequestIPAssignmentNotCIDR(t *testing.T) {
114118
status = nnc.NodeNetworkConfigStatus{
115119
NetworkContainers: []nnc.NetworkContainer{
116120
{
117-
PrimaryIP: ipCIDR,
121+
PrimaryIP: primaryIp,
118122
ID: ncID,
119123
IPAssignments: []nnc.IPAssignment{
120124
{
121125
Name: allocatedUUID,
122-
IP: ipNotCIDR,
126+
IP: ipInCIDR,
123127
},
124128
},
129+
SubnetAddressSpace: subnetAddressSpace,
130+
},
131+
},
132+
}
133+
134+
// Test with ip assignment not in CIDR form
135+
_, err = CRDStatusToNCRequest(status)
136+
137+
if err == nil {
138+
t.Fatalf("Expected translation of CRD status with ip assignment not CIDR, to fail.")
139+
}
140+
}
141+
142+
func TestStatusToNCRequestWithIncorrectSubnetAddressSpace(t *testing.T) {
143+
var (
144+
status nnc.NodeNetworkConfigStatus
145+
err error
146+
)
147+
148+
status = nnc.NodeNetworkConfigStatus{
149+
NetworkContainers: []nnc.NetworkContainer{
150+
{
151+
PrimaryIP: primaryIp,
152+
ID: ncID,
153+
IPAssignments: []nnc.IPAssignment{
154+
{
155+
Name: allocatedUUID,
156+
IP: testSecIp1,
157+
},
158+
},
159+
SubnetAddressSpace: "10.0.0.0", // not a cidr range
125160
},
126161
},
127162
}
@@ -147,17 +182,17 @@ func TestStatusToNCRequestSuccess(t *testing.T) {
147182
status = nnc.NodeNetworkConfigStatus{
148183
NetworkContainers: []nnc.NetworkContainer{
149184
{
150-
PrimaryIP: ipCIDR,
185+
PrimaryIP: primaryIp,
151186
ID: ncID,
152187
IPAssignments: []nnc.IPAssignment{
153188
{
154189
Name: allocatedUUID,
155-
IP: ipCIDR,
190+
IP: testSecIp1,
156191
},
157192
},
158193
SubnetName: subnetName,
159194
DefaultGateway: defaultGateway,
160-
SubnetAddressSpace: "", // Not currently set by DNC Request Controller
195+
SubnetAddressSpace: subnetAddressSpace,
161196
},
162197
},
163198
}
@@ -169,12 +204,12 @@ func TestStatusToNCRequestSuccess(t *testing.T) {
169204
t.Fatalf("Expected translation of CRD status to succeed, got error :%v", err)
170205
}
171206

172-
if ncRequest.IPConfiguration.IPSubnet.IPAddress != ipCIDRString {
173-
t.Fatalf("Expected ncRequest's ipconfiguration to have the ip %v but got %v", ipCIDRString, ncRequest.IPConfiguration.IPSubnet.IPAddress)
207+
if ncRequest.IPConfiguration.IPSubnet.IPAddress != primaryIp {
208+
t.Fatalf("Expected ncRequest's ipconfiguration to have the ip %v but got %v", primaryIp, ncRequest.IPConfiguration.IPSubnet.IPAddress)
174209
}
175210

176-
if ncRequest.IPConfiguration.IPSubnet.PrefixLength != uint8(ipCIDRMaskLength) {
177-
t.Fatalf("Expected ncRequest's ipconfiguration prefix length to be %v but got %v", ipCIDRMaskLength, ncRequest.IPConfiguration.IPSubnet.PrefixLength)
211+
if ncRequest.IPConfiguration.IPSubnet.PrefixLength != uint8(subnetPrefixLen) {
212+
t.Fatalf("Expected ncRequest's ipconfiguration prefix length to be %v but got %v", subnetPrefixLen, ncRequest.IPConfiguration.IPSubnet.PrefixLength)
178213
}
179214

180215
if ncRequest.IPConfiguration.GatewayIPAddress != defaultGateway {
@@ -195,8 +230,8 @@ func TestStatusToNCRequestSuccess(t *testing.T) {
195230
t.Fatalf("Expected there to be a secondary ip with the key %v but found nothing", allocatedUUID)
196231
}
197232

198-
if secondaryIP.IPAddress != ipCIDRString {
199-
t.Fatalf("Expected %v as the secondary IP config but got %v", ipCIDRString, secondaryIP.IPAddress)
233+
if secondaryIP.IPAddress != testSecIp1 {
234+
t.Fatalf("Expected %v as the secondary IP config but got %v", testSecIp1, secondaryIP.IPAddress)
200235
}
201236
}
202237

@@ -229,7 +264,7 @@ func TestSecondaryIPsToCRDSpecSuccess(t *testing.T) {
229264

230265
secondaryIPs = map[string]cns.SecondaryIPConfig{
231266
allocatedUUID: {
232-
IPAddress: ipCIDRString,
267+
IPAddress: testSecIp1,
233268
},
234269
}
235270

cns/restserver/ipam.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func (service *HTTPRestService) releaseIPConfig(podInfo cns.KubernetesPodInfo) e
145145
if ipID != "" {
146146
if ipconfig, isExist := service.PodIPConfigState[ipID]; isExist {
147147
service.setIPConfigAsAvailable(ipconfig, podInfo)
148-
logger.Printf("Released IP %+v for pod %+v", ipconfig.IPSubnet, podInfo)
148+
logger.Printf("Released IP %+v for pod %+v", ipconfig.IPAddress, podInfo)
149149

150150
} else {
151151
logger.Errorf("Failed to get release ipconfig. Pod to IPID exists, but IPID to IPConfig doesn't exist, CNS State potentially corrupt")

cns/restserver/util.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -643,21 +643,23 @@ func (service *HTTPRestService) validateIpConfigRequest(ipConfigRequest cns.GetI
643643

644644
func (service *HTTPRestService) populateIpConfigInfoUntransacted(ipConfigStatus ipConfigurationStatus, ipConfiguration *cns.IPConfiguration) error {
645645
var (
646-
ncStatus containerstatus
647-
exists bool
646+
ncStatus containerstatus
647+
exists bool
648+
primaryIpConfiguration cns.IPConfiguration
648649
)
649650

650651
if ncStatus, exists = service.state.ContainerStatus[ipConfigStatus.NCID]; !exists {
651652
return fmt.Errorf("Failed to get NC Configuration for NcId: %s", ipConfigStatus.NCID)
652653
}
653654

654-
ipConfiguration.DNSServers = ncStatus.CreateNetworkContainerRequest.IPConfiguration.DNSServers
655-
ipConfiguration.GatewayIPAddress = ncStatus.CreateNetworkContainerRequest.IPConfiguration.GatewayIPAddress
655+
primaryIpConfiguration = ncStatus.CreateNetworkContainerRequest.IPConfiguration
656+
657+
ipConfiguration.DNSServers = primaryIpConfiguration.DNSServers
658+
ipConfiguration.GatewayIPAddress = primaryIpConfiguration.GatewayIPAddress
656659

657-
// TODO: This will be changed soon, and prefix length will be set as Subnetprefix length
658660
ipConfiguration.IPSubnet = cns.IPSubnet{
659661
IPAddress: ipConfigStatus.IPAddress,
660-
PrefixLength: 32,
662+
PrefixLength: primaryIpConfiguration.IPSubnet.PrefixLength,
661663
}
662664
return nil
663665
}

0 commit comments

Comments
 (0)