Skip to content

Commit e1282ab

Browse files
authored
cns_removePrefixLengthFromSecondaryIpconfig (#648)
* cns_removePrefixLengthFromSecondaryIpconfig Removed PrefixLength from SecondaryIPConfig struct. We dont need to populate /32 in this struct. When CNI request for SecondaryIPconfig, then add Subnet range prefix length it the IP CIDR format.
1 parent 81194c2 commit e1282ab

File tree

11 files changed

+65
-94
lines changed

11 files changed

+65
-94
lines changed

cns/NetworkContainerContract.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ type IPConfiguration struct {
127127

128128
// SecondaryIPConfig contains IP info of SecondaryIP
129129
type SecondaryIPConfig struct {
130-
IPSubnet IPSubnet
130+
IPAddress string
131131
}
132132

133133
// IPSubnet contains ip subnet.

cns/cnsclient/cnsclient_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@ func addTestStateToRestServer(t *testing.T, secondaryIps []string) {
4646

4747
for _, secIpAddress := range secondaryIps {
4848
secIpConfig := cns.SecondaryIPConfig{
49-
IPSubnet: cns.IPSubnet{
50-
IPAddress: secIpAddress,
51-
PrefixLength: 32,
52-
},
49+
IPAddress: secIpAddress,
5350
}
5451
ipId := uuid.New()
5552
secondaryIPConfigs[ipId.String()] = secIpConfig

cns/requestcontroller/kubecontroller/crdrequestcontroller_test.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,11 @@ func TestUpdateSpecOnNonExistingNodeNetConfig(t *testing.T) {
366366
logger.InitLogger("Azure CNS RequestController", 0, 0, "")
367367

368368
ipConfig1 := cns.SecondaryIPConfig{
369-
IPSubnet: cns.IPSubnet{
370-
IPAddress: "10.0.0.1",
371-
PrefixLength: 32,
372-
},
369+
IPAddress: "10.0.0.1",
373370
}
374371

375372
ipConfig2 := cns.SecondaryIPConfig{
376-
IPSubnet: cns.IPSubnet{
377-
IPAddress: "10.0.0.2",
378-
PrefixLength: 32,
379-
},
373+
IPAddress: "10.0.0.2",
380374
}
381375

382376
secondaryIPConfigs := map[string]cns.SecondaryIPConfig{
@@ -421,17 +415,11 @@ func TestUpdateSpecOnExistingNodeNetConfig(t *testing.T) {
421415
logger.InitLogger("Azure CNS RequestController", 0, 0, "")
422416

423417
ipConfig1 := cns.SecondaryIPConfig{
424-
IPSubnet: cns.IPSubnet{
425-
IPAddress: "10.0.0.1",
426-
PrefixLength: 32,
427-
},
418+
IPAddress: "10.0.0.1",
428419
}
429420

430421
ipConfig2 := cns.SecondaryIPConfig{
431-
IPSubnet: cns.IPSubnet{
432-
IPAddress: "10.0.0.2",
433-
PrefixLength: 32,
434-
},
422+
IPAddress: "10.0.0.2",
435423
}
436424

437425
secondaryIPConfigs := map[string]cns.SecondaryIPConfig{

cns/requestcontroller/kubecontroller/crdtranslator.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ 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 32
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
4142
if ip, ipNet, err = net.ParseCIDR(nc.PrimaryIP); err != nil {
4243
return ncRequest, err
4344
}
@@ -49,16 +50,12 @@ func CRDStatusToNCRequest(crdStatus nnc.NodeNetworkConfigStatus) (cns.CreateNetw
4950
ncRequest.IPConfiguration.GatewayIPAddress = nc.DefaultGateway
5051

5152
for _, ipAssignment = range nc.IPAssignments {
52-
if ip, ipNet, err = net.ParseCIDR(ipAssignment.IP); err != nil {
53+
if ip, _, err = net.ParseCIDR(ipAssignment.IP); err != nil {
5354
return ncRequest, err
5455
}
5556

56-
_, bits = ipNet.Mask.Size()
57-
58-
ipSubnet.IPAddress = ip.String()
59-
ipSubnet.PrefixLength = uint8(bits)
6057
secondaryIPConfig = cns.SecondaryIPConfig{
61-
IPSubnet: ipSubnet,
58+
IPAddress: ip.String(),
6259
}
6360
ncRequest.SecondaryIPConfigs[ipAssignment.Name] = secondaryIPConfig
6461
}

cns/requestcontroller/kubecontroller/crdtranslator_test.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,8 @@ func TestStatusToNCRequestSuccess(t *testing.T) {
195195
t.Fatalf("Expected there to be a secondary ip with the key %v but found nothing", allocatedUUID)
196196
}
197197

198-
if secondaryIP.IPSubnet.IPAddress != ipCIDRString {
199-
t.Fatalf("Expected %v as the secondary IP config but got %v", ipCIDRString, secondaryIP.IPSubnet.IPAddress)
200-
}
201-
202-
if secondaryIP.IPSubnet.PrefixLength != ipCIDRMaskLength {
203-
t.Fatalf("Expected %v as the prefix length for the secondary IP config but got %v", ipCIDRMaskLength, secondaryIP.IPSubnet.PrefixLength)
198+
if secondaryIP.IPAddress != ipCIDRString {
199+
t.Fatalf("Expected %v as the secondary IP config but got %v", ipCIDRString, secondaryIP.IPAddress)
204200
}
205201
}
206202

@@ -233,10 +229,7 @@ func TestSecondaryIPsToCRDSpecSuccess(t *testing.T) {
233229

234230
secondaryIPs = map[string]cns.SecondaryIPConfig{
235231
allocatedUUID: {
236-
IPSubnet: cns.IPSubnet{
237-
IPAddress: ipCIDRString,
238-
PrefixLength: ipCIDRMaskLength,
239-
},
232+
IPAddress: ipCIDRString,
240233
},
241234
}
242235

cns/restserver/internalapi.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,12 @@ func (service *HTTPRestService) ReconcileNCState(ncRequest *cns.CreateNetworkCon
167167

168168
// now parse the secondaryIP list, if it exists in PodInfo list, then allocate that ip
169169
for _, secIpConfig := range ncRequest.SecondaryIPConfigs {
170-
if podInfo, exists := podInfoByIp[secIpConfig.IPSubnet.IPAddress]; exists {
170+
if podInfo, exists := podInfoByIp[secIpConfig.IPAddress]; exists {
171171
log.Logf("SecondaryIP %+v is allocated to Pod. %+v, ncId: %s", secIpConfig, podInfo, ncRequest.NetworkContainerid)
172172

173173
desiredIPConfig := cns.IPSubnet{
174-
IPAddress: secIpConfig.IPSubnet.IPAddress,
175-
PrefixLength: secIpConfig.IPSubnet.PrefixLength,
174+
IPAddress: secIpConfig.IPAddress,
175+
PrefixLength: 32, //todo: remove PrefixLenght in
176176
}
177177

178178
kubernetesPodInfo := cns.KubernetesPodInfo{
@@ -220,11 +220,10 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req cns.C
220220
}
221221

222222
// Validate SecondaryIPConfig
223-
for ipId, secIpconfig := range req.SecondaryIPConfigs {
223+
for _, secIpconfig := range req.SecondaryIPConfigs {
224224
// Validate Ipconfig
225-
err := validateIPSubnet(secIpconfig.IPSubnet)
226-
if err != nil {
227-
logger.Errorf("[Azure CNS] Error. SecondaryIpConfig, Id:%s is invalid, SecondaryIPConfig: %v, ncId: %s", ipId, secIpconfig, req.NetworkContainerid)
225+
if secIpconfig.IPAddress == "" {
226+
logger.Errorf("Failed to add IPConfig to state: %+v, empty IPSubnet.IPAddress", secIpconfig)
228227
return InvalidSecondaryIPConfig
229228
}
230229
}

cns/restserver/internalapi_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func TestReconcileNCWithExistingState(t *testing.T) {
5858
var startingIndex = 6
5959
for i := 0; i < 4; i++ {
6060
ipaddress := "10.0.0." + strconv.Itoa(startingIndex)
61-
secIpConfig := newSecondaryIPConfig(ipaddress, 32)
61+
secIpConfig := newSecondaryIPConfig(ipaddress)
6262
ipId := uuid.New()
6363
secondaryIPConfigs[ipId.String()] = secIpConfig
6464
startingIndex++
@@ -95,7 +95,7 @@ func TestReconcileNCWithSystemPods(t *testing.T) {
9595
var startingIndex = 6
9696
for i := 0; i < 4; i++ {
9797
ipaddress := "10.0.0." + strconv.Itoa(startingIndex)
98-
secIpConfig := newSecondaryIPConfig(ipaddress, 32)
98+
secIpConfig := newSecondaryIPConfig(ipaddress)
9999
ipId := uuid.New()
100100
secondaryIPConfigs[ipId.String()] = secIpConfig
101101
startingIndex++
@@ -136,7 +136,7 @@ func validateCreateOrUpdateNCInternal(t *testing.T, secondaryIpCount int) {
136136
var startingIndex = 6
137137
for i := 0; i < secondaryIpCount; i++ {
138138
ipaddress := "10.0.0." + strconv.Itoa(startingIndex)
139-
secIpConfig := newSecondaryIPConfig(ipaddress, 32)
139+
secIpConfig := newSecondaryIPConfig(ipaddress)
140140
ipId := uuid.New()
141141
secondaryIPConfigs[ipId.String()] = secIpConfig
142142
startingIndex++
@@ -148,7 +148,7 @@ func validateCreateOrUpdateNCInternal(t *testing.T, secondaryIpCount int) {
148148
fmt.Println("Validate Scaleup")
149149
for i := 0; i < secondaryIpCount; i++ {
150150
ipaddress := "10.0.0." + strconv.Itoa(startingIndex)
151-
secIpConfig := newSecondaryIPConfig(ipaddress, 32)
151+
secIpConfig := newSecondaryIPConfig(ipaddress)
152152
ipId := uuid.New()
153153
secondaryIPConfigs[ipId.String()] = secIpConfig
154154
startingIndex++
@@ -216,8 +216,8 @@ func validateNetworkRequest(t *testing.T, req cns.CreateNetworkContainerRequest)
216216
if secondaryIpConfig, ok := req.SecondaryIPConfigs[ipid]; !ok {
217217
t.Fatalf("PodIpConfigState has stale ipId: %s, config: %+v", ipid, ipStatus)
218218
} else {
219-
if ipStatus.IPSubnet != secondaryIpConfig.IPSubnet {
220-
t.Fatalf("IPId: %s IPSubnet doesnt match: expected %+v, actual: %+v", ipid, secondaryIpConfig.IPSubnet, ipStatus.IPSubnet)
219+
if ipStatus.IPAddress != secondaryIpConfig.IPAddress {
220+
t.Fatalf("IPId: %s IPSubnet doesnt match: expected %+v, actual: %+v", ipid, secondaryIpConfig.IPAddress, ipStatus.IPAddress)
221221
}
222222

223223
// Validate IP state
@@ -239,12 +239,12 @@ func validateNetworkRequest(t *testing.T, req cns.CreateNetworkContainerRequest)
239239
t.Fatalf("IPId: %s State is not Available, ipStatus: %+v", ipid, ipStatus)
240240
}
241241

242-
alreadyValidated[ipid] = ipStatus.IPSubnet.IPAddress
242+
alreadyValidated[ipid] = ipStatus.IPAddress
243243
}
244244
} else {
245245
// if ipaddress is not same, then fail
246-
if ipaddress != ipStatus.IPSubnet.IPAddress {
247-
t.Fatalf("Added the same IP guid :%s with different ipaddress, expected:%s, actual %s", ipid, ipStatus.IPSubnet.IPAddress, ipaddress)
246+
if ipaddress != ipStatus.IPAddress {
247+
t.Fatalf("Added the same IP guid :%s with different ipaddress, expected:%s, actual %s", ipid, ipStatus.IPAddress, ipaddress)
248248
}
249249
}
250250
}
@@ -298,7 +298,7 @@ func validateNCStateAfterReconcile(t *testing.T, ncRequest *cns.CreateNetworkCon
298298
}
299299

300300
// Validate if IPAddress matches
301-
if ipConfigstate.IPSubnet.IPAddress != ipaddress {
301+
if ipConfigstate.IPAddress != ipaddress {
302302
t.Fatalf("IpAddress %s is not same, for Pod: %+v, actual ipState: %+v", ipaddress, podInfo, ipConfigstate)
303303
}
304304

@@ -319,7 +319,7 @@ func validateNCStateAfterReconcile(t *testing.T, ncRequest *cns.CreateNetworkCon
319319
// validate rest of Secondary IPs in Available state
320320
if ncRequest != nil {
321321
for secIpId, secIpConfig := range ncRequest.SecondaryIPConfigs {
322-
if _, exists := expectedAllocatedPods[secIpConfig.IPSubnet.IPAddress]; exists {
322+
if _, exists := expectedAllocatedPods[secIpConfig.IPAddress]; exists {
323323
continue
324324
}
325325

cns/restserver/ipam.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ func (service *HTTPRestService) GetExistingIPConfig(podInfo cns.KubernetesPodInf
169169
ipID := service.PodIPIDByOrchestratorContext[podInfo.GetOrchestratorContextKey()]
170170
if ipID != "" {
171171
if ipState, isExist := service.PodIPConfigState[ipID]; isExist {
172-
ipConfiguration.IPSubnet = ipState.IPSubnet
173-
err := service.populateIpConfigInfoFromNCUntransacted(ipState.NCID, &ipConfiguration)
172+
err := service.populateIpConfigInfoUntransacted(ipState, &ipConfiguration)
174173
return ipConfiguration, isExist, err
175174
}
176175

@@ -188,7 +187,7 @@ func (service *HTTPRestService) AllocateDesiredIPConfig(podInfo cns.KubernetesPo
188187

189188
found := false
190189
for _, ipState := range service.PodIPConfigState {
191-
if ipState.IPSubnet.IPAddress == desiredIPAddress {
190+
if ipState.IPAddress == desiredIPAddress {
192191
if ipState.State == cns.Allocated {
193192
// This IP has already been allocated, if it is allocated to same pod, then return the same
194193
// IPconfiguration
@@ -207,13 +206,8 @@ func (service *HTTPRestService) AllocateDesiredIPConfig(podInfo cns.KubernetesPo
207206
}
208207

209208
if found {
210-
err := service.populateIpConfigInfoFromNCUntransacted(ipState.NCID, &ipConfiguration)
211-
if err != nil {
212-
return ipConfiguration, err
213-
}
214-
215-
ipConfiguration.IPSubnet = ipState.IPSubnet
216-
return ipConfiguration, nil
209+
err := service.populateIpConfigInfoUntransacted(ipState, &ipConfiguration)
210+
return ipConfiguration, err
217211
}
218212
}
219213
}
@@ -228,10 +222,10 @@ func (service *HTTPRestService) AllocateAnyAvailableIPConfig(podInfo cns.Kuberne
228222

229223
for _, ipState := range service.PodIPConfigState {
230224
if ipState.State == cns.Available {
231-
service.setIPConfigAsAllocated(ipState, podInfo, orchestratorContext)
232-
233-
ipConfiguration.IPSubnet = ipState.IPSubnet
234-
err := service.populateIpConfigInfoFromNCUntransacted(ipState.NCID, &ipConfiguration)
225+
err := service.populateIpConfigInfoUntransacted(ipState, &ipConfiguration)
226+
if err == nil {
227+
service.setIPConfigAsAllocated(ipState, podInfo, orchestratorContext)
228+
}
235229
return ipConfiguration, err
236230
}
237231
}

cns/restserver/ipam_test.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,20 @@ func getTestService() *HTTPRestService {
4747
return svc
4848
}
4949

50-
func newSecondaryIPConfig(ipAddress string, prefixLength uint8) cns.SecondaryIPConfig {
50+
func newSecondaryIPConfig(ipAddress string) cns.SecondaryIPConfig {
5151
return cns.SecondaryIPConfig{
52-
IPSubnet: cns.IPSubnet{
53-
IPAddress: ipAddress,
54-
PrefixLength: prefixLength,
55-
},
52+
IPAddress: ipAddress,
5653
}
5754
}
5855

5956
func NewPodState(ipaddress string, prefixLength uint8, id, ncid, state string) ipConfigurationStatus {
60-
ipconfig := newSecondaryIPConfig(ipaddress, prefixLength)
57+
ipconfig := newSecondaryIPConfig(ipaddress)
6158

6259
return ipConfigurationStatus{
63-
IPSubnet: ipconfig.IPSubnet,
64-
ID: id,
65-
NCID: ncid,
66-
State: state,
60+
IPAddress: ipconfig.IPAddress,
61+
ID: id,
62+
NCID: ncid,
63+
State: state,
6764
}
6865
}
6966

@@ -88,6 +85,7 @@ func requestIpAddressAndGetState(t *testing.T, req cns.GetIPConfigRequest) (ipCo
8885
if reflect.DeepEqual(ipConfig.GatewayIPAddress, gatewayIp) != true {
8986
t.Fatalf("Gateway is not added as expected ipConfig %+v, expected GatewayIp: %+v", ipConfig, gatewayIp)
9087
}
88+
9189
// retrieve podinfo from orchestrator context
9290
if err := json.Unmarshal(req.OrchestratorContext, &podInfo); err != nil {
9391
return ipState, err
@@ -100,10 +98,10 @@ func requestIpAddressAndGetState(t *testing.T, req cns.GetIPConfigRequest) (ipCo
10098
}
10199

102100
func NewPodStateWithOrchestratorContext(ipaddress string, prefixLength uint8, id, ncid, state string, orchestratorContext cns.KubernetesPodInfo) (ipConfigurationStatus, error) {
103-
ipconfig := newSecondaryIPConfig(ipaddress, prefixLength)
101+
ipconfig := newSecondaryIPConfig(ipaddress)
104102
b, err := json.Marshal(orchestratorContext)
105103
return ipConfigurationStatus{
106-
IPSubnet: ipconfig.IPSubnet,
104+
IPAddress: ipconfig.IPAddress,
107105
ID: id,
108106
NCID: ncid,
109107
State: state,
@@ -117,10 +115,7 @@ func UpdatePodIpConfigState(t *testing.T, svc *HTTPRestService, ipconfigs map[st
117115
secondaryIPConfigs := make(map[string]cns.SecondaryIPConfig)
118116
for _, ipconfig := range ipconfigs {
119117
secIpConfig := cns.SecondaryIPConfig{
120-
IPSubnet: cns.IPSubnet{
121-
IPAddress: ipconfig.IPSubnet.IPAddress,
122-
PrefixLength: ipconfig.IPSubnet.PrefixLength,
123-
},
118+
IPAddress: ipconfig.IPAddress,
124119
}
125120

126121
ipId := ipconfig.ID
@@ -406,7 +401,7 @@ func TestIPAMRequestThenReleaseThenRequestAgain(t *testing.T) {
406401

407402
desiredState, _ := NewPodStateWithOrchestratorContext(testIP1, 24, testPod1GUID, testNCID, cns.Allocated, testPod1Info)
408403
// want first available Pod IP State
409-
desiredState.IPSubnet = desiredIPConfig
404+
desiredState.IPAddress = desiredIPConfig.IPAddress
410405
desiredState.OrchestratorContext = b
411406

412407
if reflect.DeepEqual(desiredState, actualstate) != true {
@@ -489,7 +484,10 @@ func TestAvailableIPConfigs(t *testing.T) {
489484
req := cns.GetIPConfigRequest{}
490485
b, _ := json.Marshal(testPod1Info)
491486
req.OrchestratorContext = b
492-
req.DesiredIPConfig = state1.IPSubnet
487+
req.DesiredIPConfig = cns.IPSubnet{
488+
IPAddress: state1.IPAddress,
489+
PrefixLength: 32,
490+
}
493491

494492
_, err := requestIpAddressAndGetState(t, req)
495493
if err != nil {

cns/restserver/restserver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type allocatedIPCount struct {
5959
type ipConfigurationStatus struct {
6060
NCID string
6161
ID string //uuid
62-
IPSubnet cns.IPSubnet
62+
IPAddress string
6363
State string
6464
OrchestratorContext json.RawMessage
6565
}

0 commit comments

Comments
 (0)