func (az *Cloud) selectLoadBalancer(clusterName string, service *v1.Service, existingLBs *[]network.LoadBalancer, nodes []*v1.Node) (selectedLB *network.LoadBalancer, existsLb bool, err error) {
	isInternal := requiresInternalLoadBalancer(service)
	serviceName := getServiceName(service)
	glog.V(3).Infof("selectLoadBalancer(%s): isInternal(%s) - start", serviceName, isInternal)
	vmSetNames, err := az.vmSet.GetVMSetNames(service, nodes)
	if err != nil {
		glog.Errorf("az.selectLoadBalancer: cluster(%s) service(%s) isInternal(%t) - az.GetVMSetNames failed, err=(%v)", clusterName, serviceName, isInternal, err)
		return nil, false, err
	}
	glog.Infof("selectLoadBalancer: cluster(%s) service(%s) isInternal(%t) - vmSetNames %v", clusterName, serviceName, isInternal, *vmSetNames)

	mapExistingLBs := map[string]network.LoadBalancer{}
	for _, lb := range *existingLBs {
		mapExistingLBs[*lb.Name] = lb
	}
	selectedLBRuleCount := math.MaxInt32
	for _, currASName := range *vmSetNames {
		currLBName := az.getLoadBalancerName(clusterName, currASName, isInternal)
		lb, exists := mapExistingLBs[currLBName]
		if !exists {
			// select this LB as this is a new LB and will have minimum rules
			// create tmp lb struct to hold metadata for the new load-balancer
			selectedLB = &network.LoadBalancer{
				Name:                         &currLBName,
				Location:                     &az.Location,
				LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{},
			}

			return selectedLB, false, nil
		}

		lbRules := *lb.LoadBalancingRules
		currLBRuleCount := 0
		if lbRules != nil {
			currLBRuleCount = len(lbRules)
		}
		if currLBRuleCount < selectedLBRuleCount {
			selectedLBRuleCount = currLBRuleCount
			selectedLB = &lb
		}
	}

	if selectedLB == nil {
		err = fmt.Errorf("selectLoadBalancer: cluster(%s) service(%s) isInternal(%t) - unable to find load balancer for selected VM sets %v", clusterName, serviceName, isInternal, *vmSetNames)
		glog.Error(err)
		return nil, false, err
	}
	// validate if the selected LB has not exceeded the MaximumLoadBalancerRuleCount
	if az.Config.MaximumLoadBalancerRuleCount != 0 && selectedLBRuleCount >= az.Config.MaximumLoadBalancerRuleCount {
		err = fmt.Errorf("selectLoadBalancer: cluster(%s) service(%s) isInternal(%t) -  all available load balancers have exceeded maximum rule limit %d, vmSetNames (%v)", clusterName, serviceName, isInternal, selectedLBRuleCount, *vmSetNames)
		glog.Error(err)
		return selectedLB, existsLb, err
	}

	return selectedLB, existsLb, nil
}
// selectLoadBalancer selects load balancer for the service in the cluster.
// The selection algorithm selects the load balancer which currently has
// the minimum lb rules. If there are multiple LBs with same number of rules,
// then selects the first one (sorted based on name).
selectLoadBalancer selects load balancer for the service in the cluster.
The selection algorithm selects the load balancer which currently has
the minimum lb rules. If there are multiple LBs with same number of rules,
then selects the first one (sorted based on name).
// selectLoadBalancer selects load balancer for the service in the cluster . // The selection algorithm selects the load balancer which currently has // the minimum lb rules . If there are multiple LBs with same number of rules // then selects the first one ( sorted based on name ) .
func (az *Cloud) selectLoadBalancer(clusterName string, service *v1.Service, existingLBs *[]network.LoadBalancer, nodes []*v1.Node) (selectedLB *network.LoadBalancer, existsLb bool, err error) {
	isInternal := requiresInternalLoadBalancer(service)
	serviceName := getServiceName(service)
	glog.V(3).Infof("selectLoadBalancer(%s): isInternal(%s) - start", serviceName, isInternal)
	vmSetNames, err := az.vmSet.GetVMSetNames(service, nodes)
	if err != nil {
		glog.Errorf("az.selectLoadBalancer: cluster(%s) service(%s) isInternal(%t) - az.GetVMSetNames failed, err=(%v)", clusterName, serviceName, isInternal, err)
		return nil, false, err
	}
	glog.Infof("selectLoadBalancer: cluster(%s) service(%s) isInternal(%t) - vmSetNames %v", clusterName, serviceName, isInternal, *vmSetNames)
	mapExistingLBs := map[string]network.LoadBalancer{}
	for _, lb := range *existingLBs {
		mapExistingLBs[*lb.Name] = lb
	}
	selectedLBRuleCount := math.MaxInt32
	for _, currASName := range *vmSetNames {
		currLBName := az.getLoadBalancerName(clusterName, currASName, isInternal)
		lb, exists := mapExistingLBs[currLBName]
		if !exists {
			selectedLB = &network.LoadBalancer{
				Name:                         &currLBName,
				Location:                     &az.Location,
				LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{},
			}
			return selectedLB, false, nil
		}
		lbRules := *lb.LoadBalancingRules
		currLBRuleCount := 0
		if lbRules != nil {
			currLBRuleCount = len(lbRules)
		}
		if currLBRuleCount < selectedLBRuleCount {
			selectedLBRuleCount = currLBRuleCount
			selectedLB = &lb
		}
	}
	if selectedLB == nil {
		err = fmt.Errorf("selectLoadBalancer: cluster(%s) service(%s) isInternal(%t) - unable to find load balancer for selected VM sets %v", clusterName, serviceName, isInternal, *vmSetNames)
		glog.Error(err)
		return nil, false, err
	}
	if az.Config.MaximumLoadBalancerRuleCount != 0 && selectedLBRuleCount >= az.Config.MaximumLoadBalancerRuleCount {
		err = fmt.Errorf("selectLoadBalancer: cluster(%s) service(%s) isInternal(%t) -  all available load balancers have exceeded maximum rule limit %d, vmSetNames (%v)", clusterName, serviceName, isInternal, selectedLBRuleCount, *vmSetNames)
		glog.Error(err)
		return selectedLB, existsLb, err
	}
	return selectedLB, existsLb, nil
}
func ( az * Cloud ) selectLoadBalancer ( clusterName string , service * v1 . Service , existingLBs * [ ] network . LoadBalancer , nodes [ ] * v1 . Node ) ( selectedLB * network . LoadBalancer , existsLb bool , err error ) { isInternal := requiresInternalLoadBalancer ( service ) serviceName := getServiceName ( service ) glog . V ( 3 ) . Infof ( " " , serviceName , isInternal ) vmSetNames , err := az . vmSet . GetVMSetNames ( service , nodes ) if err != nil { glog . Errorf ( " " , clusterName , serviceName , isInternal , err ) return nil , false , err } glog . Infof ( " " , clusterName , serviceName , isInternal , * vmSetNames ) mapExistingLBs := map [ string ] network . LoadBalancer { } for _ , lb := range * existingLBs { mapExistingLBs [ * lb . Name ] = lb } selectedLBRuleCount := math . MaxInt32 for _ , currASName := range * vmSetNames { currLBName := az . getLoadBalancerName ( clusterName , currASName , isInternal ) lb , exists := mapExistingLBs [ currLBName ] if ! exists { selectedLB = & network . LoadBalancer { Name : & currLBName , Location : & az . Location , LoadBalancerPropertiesFormat : & network . LoadBalancerPropertiesFormat { } , } return selectedLB , false , nil } lbRules := * lb . LoadBalancingRules currLBRuleCount := 0 if lbRules != nil { currLBRuleCount = len ( lbRules ) } if currLBRuleCount < selectedLBRuleCount { selectedLBRuleCount = currLBRuleCount selectedLB = & lb } } if selectedLB == nil { err = fmt . Errorf ( " " , clusterName , serviceName , isInternal , * vmSetNames ) glog . Error ( err ) return nil , false , err } if az . Config . MaximumLoadBalancerRuleCount != 0 && selectedLBRuleCount >= az . Config . MaximumLoadBalancerRuleCount { err = fmt . Errorf ( " " , clusterName , serviceName , isInternal , selectedLBRuleCount , * vmSetNames ) glog . Error ( err ) return selectedLB , existsLb , err } return selectedLB , existsLb , nil }
selectLoadBalancer selects load balancer for the service in the cluster.
selectLoadBalancer selects load balancer for the service in the cluster .
// select this LB as this is a new LB and will have minimum rules // create tmp lb struct to hold metadata for the new load-balancer // validate if the selected LB has not exceeded the MaximumLoadBalancerRuleCount