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" )
@@ -979,13 +980,25 @@ func (service *HTTPRestService) AssignDesiredIPConfigs(podInfo cns.PodInfo, desi
979980// Assigns an available IP from each NC on the NNC. If there is one NC then we expect to only have one IP return
980981// In the case of dualstack we would expect to have one IPv6 from one NC and one IPv4 from a second NC
981982func (service * HTTPRestService ) AssignAvailableIPConfigs (podInfo cns.PodInfo ) ([]cns.PodIpInfo , error ) {
982- // Gets the number of IPFamilies expected to be returned.
983- numOfIPFamilies := len ( service . IPFamilies )
983+ // Gets the number of IPFamilies expected to be returned across all NCs
984+ ipFamilies := map [ IPFamily ] struct {}{}
984985
985- if numOfIPFamilies == 0 {
986- // TODO: replace with it's own error
986+ // checks to make sure we have at least one NC
987+ if len ( service . state . ContainerStatus ) == 0 {
987988 return nil , ErrNoNCs
988989 }
990+
991+ for ncID := range service .state .ContainerStatus {
992+ for ipFamily := range service .state .ContainerStatus [ncID ].CreateNetworkContainerRequest .IPFamilies {
993+ ipFamilies [ipFamily ] = struct {}{}
994+ }
995+ }
996+
997+ numOfIPFamilies := len (ipFamilies )
998+ if numOfIPFamilies == 0 {
999+ return nil , ErrNoIPFamilies
1000+ }
1001+
9891002 service .Lock ()
9901003 defer service .Unlock ()
9911004 // Creates a slice of PodIpInfo with the size as number of NCs to hold the result for assigned IP configs
@@ -995,29 +1008,37 @@ func (service *HTTPRestService) AssignAvailableIPConfigs(podInfo cns.PodInfo) ([
9951008
9961009 // Searches for available IPs in the pool
9971010 for _ , ipState := range service .PodIPConfigState {
1011+ // get the IPFamily of the current ipState
1012+ var ipStateFamily IPFamily
1013+ if net .ParseIP (ipState .IPAddress ).To4 () != nil {
1014+ ipStateFamily = IPv4Family
1015+ } else {
1016+ ipStateFamily = IPv6Family
1017+ }
1018+
9981019 // check if the IP with the same family type exists already
999- if _ , IPFamilyAlreadyMarkedForAssignment := ipsToAssign [net . ParseIP ( ipState . IPAddress ). To4 () == nil ]; IPFamilyAlreadyMarkedForAssignment {
1020+ if _ , IPFamilyAlreadyMarkedForAssignment := ipsToAssign [ipStateFamily ]; IPFamilyAlreadyMarkedForAssignment {
10001021 continue
10011022 }
10021023 // Checks if the current IP is available
10031024 if ipState .GetState () != types .Available {
10041025 continue
10051026 }
1006- ipsToAssign [net . ParseIP ( ipState . IPAddress ). To4 () == nil ] = ipState
1007- // Once one IP per container is found break out of the loop and stop searching
1027+ ipsToAssign [ipStateFamily ] = ipState
1028+ // Once one IP per family is found break out of the loop and stop searching
10081029 if len (ipsToAssign ) == numOfIPFamilies {
10091030 break
10101031 }
10111032 }
10121033
1013- // Checks to make sure we found one IP for each NC
1034+ // Checks to make sure we found one IP for each IPFamily
10141035 if len (ipsToAssign ) != numOfIPFamilies {
1015- for _ , ipFamily := range service .IPFamilies {
1016- if _ , found := ipsToAssign [ipFamily ]; found {
1036+ for ncID := range service .state . ContainerStatus {
1037+ if _ , found := ipsToAssign [ncID ]; found {
10171038 continue
10181039 }
1019- return podIPInfo , errors .Errorf ("not enough IPs available of type %s, waiting on Azure CNS to allocate more with NC Status: %s" ,
1020- ipFamily , string (service .state .ContainerStatus [ncID ].CreateNetworkContainerRequest .NCStatus ))
1040+ return podIPInfo , errors .Errorf ("not enough IPs available for %s, waiting on Azure CNS to allocate more with NC Status: %s" ,
1041+ ncID , string (service .state .ContainerStatus [ncID ].CreateNetworkContainerRequest .NCStatus ))
10211042 }
10221043 }
10231044
0 commit comments