2525 ErrStoreEmpty = errors .New ("empty endpoint state store" )
2626 ErrParsePodIPFailed = errors .New ("failed to parse pod's ip" )
2727 ErrNoNCs = errors .New ("no NCs found in the CNS internal state" )
28+ ErrNoIPFamilies = errors .New ("No IP Families found on NCs" )
2829 ErrOptManageEndpointState = errors .New ("CNS is not set to manage the endpoint state" )
2930 ErrEndpointStateNotFound = errors .New ("endpoint state could not be found in the statefile" )
3031 ErrGetAllNCResponseEmpty = errors .New ("failed to get NC responses from statefile" )
@@ -990,13 +991,25 @@ func (service *HTTPRestService) AssignDesiredIPConfigs(podInfo cns.PodInfo, desi
990991// Assigns an available IP from each NC on the NNC. If there is one NC then we expect to only have one IP return
991992// In the case of dualstack we would expect to have one IPv6 from one NC and one IPv4 from a second NC
992993func (service * HTTPRestService ) AssignAvailableIPConfigs (podInfo cns.PodInfo ) ([]cns.PodIpInfo , error ) {
993- // Gets the number of IPFamilies expected to be returned.
994- numOfIPFamilies := len ( service . IPFamilies )
994+ // Gets the number of IPFamilies expected to be returned across all NCs
995+ ipFamilies := map [ IPFamily ] struct {}{}
995996
996- if numOfIPFamilies == 0 {
997- // TODO: replace with it's own error
997+ // checks to make sure we have at least one NC
998+ if len ( service . state . ContainerStatus ) == 0 {
998999 return nil , ErrNoNCs
9991000 }
1001+
1002+ for ncID := range service .state .ContainerStatus {
1003+ for ipFamily := range service .state .ContainerStatus [ncID ].CreateNetworkContainerRequest .IPFamilies {
1004+ ipFamilies [ipFamily ] = struct {}{}
1005+ }
1006+ }
1007+
1008+ numOfIPFamilies := len (ipFamilies )
1009+ if numOfIPFamilies == 0 {
1010+ return nil , ErrNoIPFamilies
1011+ }
1012+
10001013 service .Lock ()
10011014 defer service .Unlock ()
10021015 // Creates a slice of PodIpInfo with the size as number of NCs to hold the result for assigned IP configs
@@ -1006,29 +1019,37 @@ func (service *HTTPRestService) AssignAvailableIPConfigs(podInfo cns.PodInfo) ([
10061019
10071020 // Searches for available IPs in the pool
10081021 for _ , ipState := range service .PodIPConfigState {
1022+ // get the IPFamily of the current ipState
1023+ var ipStateFamily IPFamily
1024+ if net .ParseIP (ipState .IPAddress ).To4 () != nil {
1025+ ipStateFamily = IPv4Family
1026+ } else {
1027+ ipStateFamily = IPv6Family
1028+ }
1029+
10091030 // check if the IP with the same family type exists already
1010- if _ , IPFamilyAlreadyMarkedForAssignment := ipsToAssign [net . ParseIP ( ipState . IPAddress ). To4 () == nil ]; IPFamilyAlreadyMarkedForAssignment {
1031+ if _ , IPFamilyAlreadyMarkedForAssignment := ipsToAssign [ipStateFamily ]; IPFamilyAlreadyMarkedForAssignment {
10111032 continue
10121033 }
10131034 // Checks if the current IP is available
10141035 if ipState .GetState () != types .Available {
10151036 continue
10161037 }
1017- ipsToAssign [net . ParseIP ( ipState . IPAddress ). To4 () == nil ] = ipState
1018- // Once one IP per container is found break out of the loop and stop searching
1038+ ipsToAssign [ipStateFamily ] = ipState
1039+ // Once one IP per family is found break out of the loop and stop searching
10191040 if len (ipsToAssign ) == numOfIPFamilies {
10201041 break
10211042 }
10221043 }
10231044
1024- // Checks to make sure we found one IP for each NC
1045+ // Checks to make sure we found one IP for each IPFamily
10251046 if len (ipsToAssign ) != numOfIPFamilies {
1026- for _ , ipFamily := range service .IPFamilies {
1027- if _ , found := ipsToAssign [ipFamily ]; found {
1047+ for ncID := range service .state . ContainerStatus {
1048+ if _ , found := ipsToAssign [ncID ]; found {
10281049 continue
10291050 }
1030- return podIPInfo , errors .Errorf ("not enough IPs available of type %s, waiting on Azure CNS to allocate more with NC Status: %s" ,
1031- ipFamily , string (service .state .ContainerStatus [ncID ].CreateNetworkContainerRequest .NCStatus ))
1051+ return podIPInfo , errors .Errorf ("not enough IPs available for %s, waiting on Azure CNS to allocate more with NC Status: %s" ,
1052+ ncID , string (service .state .ContainerStatus [ncID ].CreateNetworkContainerRequest .NCStatus ))
10321053 }
10331054 }
10341055
0 commit comments