@@ -18,6 +18,7 @@ import (
1818
1919 "github.com/Azure/azure-container-networking/cns"
2020 "github.com/Azure/azure-container-networking/cns/logger"
21+ "github.com/Azure/azure-container-networking/cns/nodesubnet"
2122 "github.com/Azure/azure-container-networking/cns/types"
2223 "github.com/Azure/azure-container-networking/common"
2324 "github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
@@ -275,22 +276,7 @@ func (service *HTTPRestService) syncHostNCVersion(ctx context.Context, channelMo
275276 return len (programmedNCs ), nil
276277}
277278
278- func (service * HTTPRestService ) ReconcileIPAMState (ncReqs []* cns.CreateNetworkContainerRequest , podInfoByIP map [string ]cns.PodInfo , nnc * v1alpha.NodeNetworkConfig ) types.ResponseCode {
279- logger .Printf ("Reconciling CNS IPAM state with nc requests: [%+v], PodInfo [%+v], NNC: [%+v]" , ncReqs , podInfoByIP , nnc )
280- // if no nc reqs, there is no CRD state yet
281- if len (ncReqs ) == 0 {
282- logger .Printf ("CNS starting with no NC state, podInfoMap count %d" , len (podInfoByIP ))
283- return types .Success
284- }
285-
286- // first step in reconciliation is to create all the NCs in CNS, no IP assignment yet.
287- for _ , ncReq := range ncReqs {
288- returnCode := service .CreateOrUpdateNetworkContainerInternal (ncReq )
289- if returnCode != types .Success {
290- return returnCode
291- }
292- }
293-
279+ func (service * HTTPRestService ) ReconcileIPAssignment (podInfoByIP map [string ]cns.PodInfo , ncReqs []* cns.CreateNetworkContainerRequest ) types.ResponseCode {
294280 // index all the secondary IP configs for all the nc reqs, for easier lookup later on.
295281 allSecIPsIdx := make (map [string ]* cns.CreateNetworkContainerRequest )
296282 for i := range ncReqs {
@@ -321,6 +307,7 @@ func (service *HTTPRestService) ReconcileIPAMState(ncReqs []*cns.CreateNetworkCo
321307 // }
322308 //
323309 // such that we can iterate over pod interfaces, and assign all IPs for it at once.
310+
324311 podKeyToPodIPs , err := newPodKeyToPodIPsMap (podInfoByIP )
325312 if err != nil {
326313 logger .Errorf ("could not transform pods indexed by IP address to pod IPs indexed by interface: %v" , err )
@@ -378,12 +365,69 @@ func (service *HTTPRestService) ReconcileIPAMState(ncReqs []*cns.CreateNetworkCo
378365 }
379366 }
380367
368+ return types .Success
369+ }
370+
371+ func (service * HTTPRestService ) CreateNCs (ncReqs []* cns.CreateNetworkContainerRequest ) types.ResponseCode {
372+ for _ , ncReq := range ncReqs {
373+ returnCode := service .CreateOrUpdateNetworkContainerInternal (ncReq )
374+ if returnCode != types .Success {
375+ return returnCode
376+ }
377+ }
378+
379+ return types .Success
380+ }
381+
382+ func (service * HTTPRestService ) ReconcileIPAMStateForSwift (ncReqs []* cns.CreateNetworkContainerRequest , podInfoByIP map [string ]cns.PodInfo , nnc * v1alpha.NodeNetworkConfig ) types.ResponseCode {
383+ logger .Printf ("Reconciling CNS IPAM state with nc requests: [%+v], PodInfo [%+v], NNC: [%+v]" , ncReqs , podInfoByIP , nnc )
384+ // if no nc reqs, there is no CRD state yet
385+ if len (ncReqs ) == 0 {
386+ logger .Printf ("CNS starting with no NC state, podInfoMap count %d" , len (podInfoByIP ))
387+ return types .Success
388+ }
389+
390+ // first step in reconciliation is to create all the NCs in CNS, no IP assignment yet.
391+ if returnCode := service .CreateNCs (ncReqs ); returnCode != types .Success {
392+ return returnCode
393+ }
394+
395+ logger .Debugf ("ncReqs created successfully, now save IPs" )
396+ // now reconcile IPAM state.
397+ if returnCode := service .ReconcileIPAssignment (podInfoByIP , ncReqs ); returnCode != types .Success {
398+ return returnCode
399+ }
400+
381401 if err := service .MarkExistingIPsAsPendingRelease (nnc .Spec .IPsNotInUse ); err != nil {
382402 logger .Errorf ("[Azure CNS] Error. Failed to mark IPs as pending %v" , nnc .Spec .IPsNotInUse )
383403 return types .UnexpectedError
384404 }
385405
386- return 0
406+ return types .Success
407+ }
408+
409+ // todo: there is some redundancy between this funcation and ReconcileIPAMStateForNodeSubnet. The difference is that this one
410+ // doesn't include the NNC parameter. We may want to unify the common parts.
411+ func (service * HTTPRestService ) ReconcileIPAMStateForNodeSubnet (ncReqs []* cns.CreateNetworkContainerRequest , podInfoByIP map [string ]cns.PodInfo ) types.ResponseCode {
412+ logger .Printf ("Reconciling CNS IPAM state with nc requests: [%+v], PodInfo [%+v]" , ncReqs , podInfoByIP )
413+
414+ if len (ncReqs ) != 1 {
415+ logger .Errorf ("Nodesubnet should always have 1 NC to hold secondary IPs" )
416+ return types .NetworkContainerNotSpecified
417+ }
418+
419+ // first step in reconciliation is to create all the NCs in CNS, no IP assignment yet.
420+ if returnCode := service .CreateNCs (ncReqs ); returnCode != types .Success {
421+ return returnCode
422+ }
423+
424+ logger .Debugf ("ncReqs created successfully, now save IPs" )
425+ // now reconcile IPAM state.
426+ if returnCode := service .ReconcileIPAssignment (podInfoByIP , ncReqs ); returnCode != types .Success {
427+ return returnCode
428+ }
429+
430+ return types .Success
387431}
388432
389433var (
@@ -526,11 +570,19 @@ func (service *HTTPRestService) CreateOrUpdateNetworkContainerInternal(req *cns.
526570 return types .UnsupportedOrchestratorType
527571 }
528572
529- // Validate PrimaryCA must never be empty
530- err := validateIPSubnet (req .IPConfiguration .IPSubnet )
531- if err != nil {
532- logger .Errorf ("[Azure CNS] Error. PrimaryCA is invalid, NC Req: %v" , req )
533- return types .InvalidPrimaryIPConfig
573+ if req .NetworkContainerid == nodesubnet .NodeSubnetNCID {
574+ // For NodeSubnet scenarios, Validate PrimaryCA must be empty
575+ if req .IPConfiguration .IPSubnet .IPAddress != "" {
576+ logger .Errorf ("[Azure CNS] Error. PrimaryCA is invalid, NC Req: %v" , req )
577+ return types .InvalidPrimaryIPConfig
578+ }
579+ } else {
580+ // For Swift scenarios, Validate PrimaryCA must never be empty
581+ err := validateIPSubnet (req .IPConfiguration .IPSubnet )
582+ if err != nil {
583+ logger .Errorf ("[Azure CNS] Error. PrimaryCA is invalid, NC Req: %v" , req )
584+ return types .InvalidPrimaryIPConfig
585+ }
534586 }
535587
536588 // Validate SecondaryIPConfig
0 commit comments