Skip to content

Commit

Permalink
move azure config struct to config package (#7696)
Browse files Browse the repository at this point in the history
Co-authored-by: MartinForReal <[email protected]>
  • Loading branch information
1 parent b67c63f commit 1d378c4
Show file tree
Hide file tree
Showing 24 changed files with 587 additions and 506 deletions.
3 changes: 2 additions & 1 deletion pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/klog/v2"

azureprovider "sigs.k8s.io/cloud-provider-azure/pkg/provider"
"sigs.k8s.io/cloud-provider-azure/pkg/provider/config"
)

// IMDSNodeProvider implements nodemanager.NodeProvider.
Expand All @@ -34,7 +35,7 @@ type IMDSNodeProvider struct {

// NewIMDSNodeProvider creates a new IMDSNodeProvider.
func NewIMDSNodeProvider(ctx context.Context) *IMDSNodeProvider {
az, err := azureprovider.NewCloud(ctx, nil, &azureprovider.Config{
az, err := azureprovider.NewCloud(ctx, nil, &config.Config{
UseInstanceMetadata: true,
VMType: "vmss",
}, false)
Expand Down
319 changes: 28 additions & 291 deletions pkg/provider/azure.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/provider/azure_fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewTestFlexScaleSet(ctrl *gomock.Controller) (*FlexScaleSet, error) {
// GetTestCloud returns a fake azure cloud for unit tests in Azure related CSI drivers
func GetTestCloud(ctrl *gomock.Controller) (az *Cloud) {
az = &Cloud{
Config: Config{
Config: config.Config{
AzureClientConfig: config.AzureClientConfig{
ARMClientConfig: azclient.ARMClientConfig{
TenantID: "TenantID",
Expand Down
4 changes: 3 additions & 1 deletion pkg/provider/azure_instance_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"sigs.k8s.io/cloud-provider-azure/pkg/provider/config"
)

// TestFillNetInterfacePublicIPs tests if IPv6 IPs from imds load balancer are
Expand Down Expand Up @@ -110,7 +112,7 @@ func TestGetPlatformSubFaultDomain(t *testing.T) {
} {
t.Run(testCase.description, func(t *testing.T) {
cloud := &Cloud{
Config: Config{
Config: config.Config{
Location: "eastus",
UseInstanceMetadata: true,
},
Expand Down
47 changes: 24 additions & 23 deletions pkg/provider/azure_loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"sigs.k8s.io/cloud-provider-azure/pkg/consts"
"sigs.k8s.io/cloud-provider-azure/pkg/log"
"sigs.k8s.io/cloud-provider-azure/pkg/metrics"
"sigs.k8s.io/cloud-provider-azure/pkg/provider/config"
"sigs.k8s.io/cloud-provider-azure/pkg/provider/loadbalancer"
"sigs.k8s.io/cloud-provider-azure/pkg/retry"
"sigs.k8s.io/cloud-provider-azure/pkg/trace"
Expand Down Expand Up @@ -177,7 +178,7 @@ func (az *Cloud) reconcileService(ctx context.Context, clusterName string, servi

lbName := strings.ToLower(ptr.Deref(lb.Name, ""))
key := strings.ToLower(getServiceName(service))
if az.useMultipleStandardLoadBalancers() && isLocalService(service) {
if az.UseMultipleStandardLoadBalancers() && isLocalService(service) {
az.localServiceNameToServiceInfoMap.Store(key, newServiceInfo(getServiceIPFamily(service), lbName))
// There are chances that the endpointslice changes after EnsureHostsInPool, so
// need to check endpointslice for a second time.
Expand Down Expand Up @@ -488,7 +489,7 @@ func (az *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName stri
return err
}

if az.useMultipleStandardLoadBalancers() && isLocalService(service) {
if az.UseMultipleStandardLoadBalancers() && isLocalService(service) {
key := strings.ToLower(svcName)
az.localServiceNameToServiceInfoMap.Delete(key)
}
Expand Down Expand Up @@ -519,7 +520,7 @@ func (az *Cloud) shouldChangeLoadBalancer(service *v1.Service, currLBName, clust
// The load balancer can be changed in two cases:
// 1. Using multiple standard load balancers.
// 2. Migrate from multiple standard load balancers to single standard load balancer.
if az.useStandardLoadBalancer() {
if az.UseStandardLoadBalancer() {
if currLBName != expectedLBName {
klog.V(2).Infof("shouldChangeLoadBalancer(%s, %s, %s): change the LB to another one %s", service.Name, currLBName, clusterName, expectedLBName)
return true
Expand Down Expand Up @@ -826,7 +827,7 @@ func (az *Cloud) getServiceLoadBalancer(
ptr.Deref(existingLB.Name, ""),
)

if isLocalService(service) && az.useMultipleStandardLoadBalancers() {
if isLocalService(service) && az.UseMultipleStandardLoadBalancers() {
// No need for the endpoint slice informer to update the backend pool
// for the service because the main loop will delete the old backend pool
// and create a new one in the new load balancer.
Expand Down Expand Up @@ -854,7 +855,7 @@ func (az *Cloud) getServiceLoadBalancer(
// Service does not have a load balancer, select one.
// Single standard load balancer doesn't need this because
// all backends nodes should be added to same LB.
if wantLb && !az.useStandardLoadBalancer() {
if wantLb && !az.UseStandardLoadBalancer() {
// select new load balancer for service
selectedLB, exists, err := az.selectLoadBalancer(ctx, clusterName, service, existingLBs, nodes)
if err != nil {
Expand All @@ -881,7 +882,7 @@ func (az *Cloud) getServiceLoadBalancer(
Location: &az.Location,
LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{},
}
if az.useStandardLoadBalancer() {
if az.UseStandardLoadBalancer() {
defaultLB.Sku = &network.LoadBalancerSku{
Name: network.LoadBalancerSkuNameStandard,
}
Expand Down Expand Up @@ -929,7 +930,7 @@ func (az *Cloud) selectLoadBalancer(ctx context.Context, clusterName string, ser
// 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
var loadBalancerSKU network.LoadBalancerSkuName
if az.useStandardLoadBalancer() {
if az.UseStandardLoadBalancer() {
loadBalancerSKU = network.LoadBalancerSkuNameStandard
} else {
loadBalancerSKU = network.LoadBalancerSkuNameBasic
Expand Down Expand Up @@ -1203,7 +1204,7 @@ func (az *Cloud) ensurePublicIPExists(ctx context.Context, service *v1.Service,
return nil, err
}

if az.useStandardLoadBalancer() {
if az.UseStandardLoadBalancer() {
pip.Sku = &network.PublicIPAddressSku{
Name: network.PublicIPAddressSkuNameStandard,
}
Expand Down Expand Up @@ -1277,7 +1278,7 @@ func (az *Cloud) reconcileIPSettings(pip *network.PublicIPAddress, service *v1.S
changed = true
}

if az.useStandardLoadBalancer() {
if az.UseStandardLoadBalancer() {
// standard sku must have static allocation method for ipv6
if !strings.EqualFold(string(pip.PublicIPAddressPropertiesFormat.PublicIPAllocationMethod), string(network.Static)) {
pip.PublicIPAddressPropertiesFormat.PublicIPAllocationMethod = network.Static
Expand Down Expand Up @@ -1688,7 +1689,7 @@ func (az *Cloud) reconcileMultipleStandardLoadBalancerConfigurations(
existingLBs *[]network.LoadBalancer,
nodes []*v1.Node,
) (err error) {
if !az.useMultipleStandardLoadBalancers() {
if !az.UseMultipleStandardLoadBalancers() {
return nil
}

Expand Down Expand Up @@ -1777,7 +1778,7 @@ func (az *Cloud) reconcileLoadBalancer(ctx context.Context, clusterName string,
// Delete backend pools for local service if:
// 1. the cluster is migrating from multi-slb to single-slb,
// 2. the service is changed from local to cluster.
if !az.useMultipleStandardLoadBalancers() || !isLocalService(service) {
if !az.UseMultipleStandardLoadBalancers() || !isLocalService(service) {
existingLBs, err = az.cleanupLocalServiceBackendPool(ctx, service, nodes, existingLBs, clusterName)
if err != nil {
klog.Errorf("reconcileLoadBalancer: failed to cleanup local service backend pool for service %q, error: %s", serviceName, err.Error())
Expand Down Expand Up @@ -1957,7 +1958,7 @@ func (az *Cloud) reconcileLoadBalancer(ctx context.Context, clusterName string,
_ = az.lbCache.Delete(lbName)
}()

if az.useMultipleStandardLoadBalancers() {
if az.UseMultipleStandardLoadBalancers() {
err := az.reconcileMultipleStandardLoadBalancerBackendNodes(ctx, clusterName, lbName, existingLBs, service, nodes, false)
if err != nil {
return nil, err
Expand All @@ -1970,7 +1971,7 @@ func (az *Cloud) reconcileLoadBalancer(ctx context.Context, clusterName string,
// change in one reconciliation loop.
var lbToReconcile []network.LoadBalancer
lbToReconcile = append(lbToReconcile, *lb)
if az.useMultipleStandardLoadBalancers() {
if az.UseMultipleStandardLoadBalancers() {
lbToReconcile = *existingLBs
}
lb, err = az.reconcileBackendPoolHosts(ctx, lb, lbToReconcile, service, nodes, clusterName, vmSetName, lbBackendPoolIDs)
Expand Down Expand Up @@ -2662,7 +2663,7 @@ func (az *Cloud) getFrontendZones(
if err != nil {
return err
}
if az.useStandardLoadBalancer() && len(zones) > 0 && !az.HasExtendedLocation() {
if az.UseStandardLoadBalancer() && len(zones) > 0 && !az.HasExtendedLocation() {
fipConfig.Zones = &zones
}
} else {
Expand Down Expand Up @@ -2826,7 +2827,7 @@ func (az *Cloud) getExpectedLBRules(
// In HA mode, lb forward traffic of all port to backend
// HA mode is only supported on standard loadbalancer SKU in internal mode
if consts.IsK8sServiceUsingInternalLoadBalancer(service) &&
az.useStandardLoadBalancer() &&
az.UseStandardLoadBalancer() &&
consts.IsK8sServiceHasHAModeEnabled(service) {

lbRuleName := az.getloadbalancerHAmodeRuleName(service, isIPv6)
Expand Down Expand Up @@ -2882,7 +2883,7 @@ func (az *Cloud) getExpectedLBRules(
klog.V(2).Infof("getExpectedLBRules lb name (%s) rule name (%s) no lb rule required", lbName, lbRuleName)
continue
}
if port.Protocol == v1.ProtocolSCTP && !(az.useStandardLoadBalancer() && consts.IsK8sServiceUsingInternalLoadBalancer(service)) {
if port.Protocol == v1.ProtocolSCTP && !(az.UseStandardLoadBalancer() && consts.IsK8sServiceUsingInternalLoadBalancer(service)) {
return expectedProbes, expectedRules, fmt.Errorf("SCTP is only supported on standard loadbalancer in internal mode")
}

Expand Down Expand Up @@ -2970,7 +2971,7 @@ func (az *Cloud) getExpectedLoadBalancingRulePropertiesForPort(
Protocol: transportProto,
FrontendPort: ptr.To(servicePort.Port),
BackendPort: ptr.To(servicePort.Port),
DisableOutboundSnat: ptr.To(az.disableLoadBalancerOutboundSNAT()),
DisableOutboundSnat: ptr.To(az.DisableLoadBalancerOutboundSNAT()),
EnableFloatingIP: ptr.To(true),
LoadDistribution: loadDistribution,
FrontendIPConfiguration: &network.SubResource{
Expand All @@ -2981,7 +2982,7 @@ func (az *Cloud) getExpectedLoadBalancingRulePropertiesForPort(
},
IdleTimeoutInMinutes: lbIdleTimeout,
}
if strings.EqualFold(string(transportProto), string(network.TransportProtocolTCP)) && az.useStandardLoadBalancer() {
if strings.EqualFold(string(transportProto), string(network.TransportProtocolTCP)) && az.UseStandardLoadBalancer() {
props.EnableTCPReset = ptr.To(!consts.IsTCPResetDisabled(service.Annotations))
}

Expand Down Expand Up @@ -3663,7 +3664,7 @@ func ipInSubnet(ip string, subnet *network.Subnet) bool {
// if anything else it returns the unique VM set names after trimming spaces.
func (az *Cloud) getServiceLoadBalancerMode(service *v1.Service) (bool, bool, string) {
mode, hasMode := service.Annotations[consts.ServiceAnnotationLoadBalancerMode]
if az.useStandardLoadBalancer() && hasMode {
if az.UseStandardLoadBalancer() && hasMode {
klog.Warningf("single standard load balancer doesn't work with annotation %q, would ignore it", consts.ServiceAnnotationLoadBalancerMode)
}
mode = strings.TrimSpace(mode)
Expand Down Expand Up @@ -3909,14 +3910,14 @@ func (az *Cloud) getAzureLoadBalancerName(
// The LB name prefix is set to the name of the cluster when:
// 1. the LB belongs to the primary agent pool.
// 2. using the single SLB.
if strings.EqualFold(vmSetName, az.VMSet.GetPrimaryVMSetName()) || az.useSingleStandardLoadBalancer() {
if strings.EqualFold(vmSetName, az.VMSet.GetPrimaryVMSetName()) || az.UseSingleStandardLoadBalancer() {
lbNamePrefix = clusterName
}

// For multiple standard load balancers scenario:
// 1. Filter out the eligible load balancers.
// 2. Choose the most eligible load balancer.
if az.useMultipleStandardLoadBalancers() {
if az.UseMultipleStandardLoadBalancers() {
eligibleLBs, err := az.getEligibleLoadBalancersForService(ctx, service)
if err != nil {
return "", err
Expand Down Expand Up @@ -4008,7 +4009,7 @@ func (az *Cloud) getServiceCurrentLoadBalancerName(service *v1.Service) string {
// 5. If there is no label/namespace selector on the LB, it can be a valid placement target if and only if the service has no other choice.
func (az *Cloud) getEligibleLoadBalancersForService(ctx context.Context, service *v1.Service) ([]string, error) {
var (
eligibleLBs []MultipleStandardLoadBalancerConfiguration
eligibleLBs []config.MultipleStandardLoadBalancerConfiguration
eligibleLBNames []string
lbSelectedByAnnotation []string
lbFailedLabelSelector []string
Expand Down Expand Up @@ -4159,7 +4160,7 @@ func (az *Cloud) getEligibleLoadBalancersForService(ctx context.Context, service
return eligibleLBNames, nil
}

func (az *Cloud) isLoadBalancerInUseByService(service *v1.Service, lbConfig MultipleStandardLoadBalancerConfiguration) bool {
func (az *Cloud) isLoadBalancerInUseByService(service *v1.Service, lbConfig config.MultipleStandardLoadBalancerConfiguration) bool {
az.multipleStandardLoadBalancersActiveServicesLock.Lock()
defer az.multipleStandardLoadBalancersActiveServicesLock.Unlock()

Expand Down
12 changes: 6 additions & 6 deletions pkg/provider/azure_loadbalancer_backendpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (bi *backendPoolTypeNodeIP) EnsureHostsInPool(ctx context.Context, service
numOfAdd, numOfDelete int
activeNodes *utilsets.IgnoreCaseSet
)
if bi.useMultipleStandardLoadBalancers() {
if bi.UseMultipleStandardLoadBalancers() {
if !isLocalService(service) {
activeNodes = bi.getActiveNodesByLoadBalancerName(lbName)
} else {
Expand Down Expand Up @@ -463,7 +463,7 @@ func (bi *backendPoolTypeNodeIP) EnsureHostsInPool(ctx context.Context, service
nodePrivateIPsSet.Insert(privateIP)
}

if bi.useMultipleStandardLoadBalancers() {
if bi.UseMultipleStandardLoadBalancers() {
if activeNodes != nil && !activeNodes.Has(node.Name) {
klog.V(4).Infof("bi.EnsureHostsInPool: node %s should not be in load balancer %q", node.Name, lbName)
continue
Expand All @@ -487,7 +487,7 @@ func (bi *backendPoolTypeNodeIP) EnsureHostsInPool(ctx context.Context, service
nodeIPsToBeDeleted = append(nodeIPsToBeDeleted, ip)
changed = true
numOfDelete++
} else if bi.useMultipleStandardLoadBalancers() && activeNodes != nil {
} else if bi.UseMultipleStandardLoadBalancers() && activeNodes != nil {
nodeName, ok := bi.nodePrivateIPToNodeNameMap[ip]
if !ok {
klog.Warningf("bi.EnsureHostsInPool: cannot find node name for private IP %s", ip)
Expand All @@ -501,7 +501,7 @@ func (bi *backendPoolTypeNodeIP) EnsureHostsInPool(ctx context.Context, service
}
}
}
removeNodeIPAddressesFromBackendPool(backendPool, nodeIPsToBeDeleted, false, bi.useMultipleStandardLoadBalancers(), true)
removeNodeIPAddressesFromBackendPool(backendPool, nodeIPsToBeDeleted, false, bi.UseMultipleStandardLoadBalancers(), true)
}
if changed {
klog.V(2).Infof("bi.EnsureHostsInPool: updating backend pool %s of load balancer %s to add %d nodes and remove %d nodes", lbBackendPoolName, lbName, numOfAdd, numOfDelete)
Expand Down Expand Up @@ -872,7 +872,7 @@ func hasIPAddressInBackendPool(backendPool *network.BackendAddressPool, ipAddres
func removeNodeIPAddressesFromBackendPool(
backendPool network.BackendAddressPool,
nodeIPAddresses []string,
removeAll, useMultipleStandardLoadBalancers, isNodeIP bool,
removeAll, UseMultipleStandardLoadBalancers, isNodeIP bool,
) bool {
changed := false
nodeIPsSet := utilsets.NewString(nodeIPAddresses...)
Expand Down Expand Up @@ -913,7 +913,7 @@ func removeNodeIPAddressesFromBackendPool(

// Allow the pool to be empty when EnsureHostsInPool for multiple standard load balancers clusters,
// or one node could occur in multiple backend pools.
if len(addresses) == 0 && !useMultipleStandardLoadBalancers {
if len(addresses) == 0 && !UseMultipleStandardLoadBalancers {
klog.V(2).Info("removeNodeIPAddressFromBackendPool: the pool is empty or will be empty after removing the unwanted IP addresses, skipping the removal")
changed = false
} else if changed {
Expand Down
21 changes: 11 additions & 10 deletions pkg/provider/azure_loadbalancer_backendpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (

"sigs.k8s.io/cloud-provider-azure/pkg/azureclients/loadbalancerclient/mockloadbalancerclient"
"sigs.k8s.io/cloud-provider-azure/pkg/consts"
"sigs.k8s.io/cloud-provider-azure/pkg/provider/config"
"sigs.k8s.io/cloud-provider-azure/pkg/retry"
utilsets "sigs.k8s.io/cloud-provider-azure/pkg/util/sets"
)
Expand Down Expand Up @@ -107,7 +108,7 @@ func TestEnsureHostsInPoolNodeIP(t *testing.T) {
testcases := []struct {
desc string
backendPool network.BackendAddressPool
multiSLBConfigs []MultipleStandardLoadBalancerConfiguration
multiSLBConfigs []config.MultipleStandardLoadBalancerConfiguration
local bool
notFound bool
skip bool
Expand Down Expand Up @@ -214,10 +215,10 @@ func TestEnsureHostsInPoolNodeIP(t *testing.T) {
},
},
},
multiSLBConfigs: []MultipleStandardLoadBalancerConfiguration{
multiSLBConfigs: []config.MultipleStandardLoadBalancerConfiguration{
{
Name: "kubernetes",
MultipleStandardLoadBalancerConfigurationStatus: MultipleStandardLoadBalancerConfigurationStatus{
MultipleStandardLoadBalancerConfigurationStatus: config.MultipleStandardLoadBalancerConfigurationStatus{
ActiveNodes: utilsets.NewString("vmss-2"),
},
},
Expand Down Expand Up @@ -255,10 +256,10 @@ func TestEnsureHostsInPoolNodeIP(t *testing.T) {
},
},
},
multiSLBConfigs: []MultipleStandardLoadBalancerConfiguration{
multiSLBConfigs: []config.MultipleStandardLoadBalancerConfiguration{
{
Name: "kubernetes",
MultipleStandardLoadBalancerConfigurationStatus: MultipleStandardLoadBalancerConfigurationStatus{
MultipleStandardLoadBalancerConfigurationStatus: config.MultipleStandardLoadBalancerConfigurationStatus{
ActiveNodes: utilsets.NewString("vmss-2"),
},
},
Expand All @@ -282,7 +283,7 @@ func TestEnsureHostsInPoolNodeIP(t *testing.T) {
desc: "local service without service info",
local: true,
notFound: true,
multiSLBConfigs: []MultipleStandardLoadBalancerConfiguration{
multiSLBConfigs: []config.MultipleStandardLoadBalancerConfiguration{
{
Name: "kubernetes",
},
Expand All @@ -292,7 +293,7 @@ func TestEnsureHostsInPoolNodeIP(t *testing.T) {
desc: "local service with another load balancer",
local: true,
skip: true,
multiSLBConfigs: []MultipleStandardLoadBalancerConfiguration{
multiSLBConfigs: []config.MultipleStandardLoadBalancerConfiguration{
{
Name: "kubernetes",
},
Expand All @@ -307,7 +308,7 @@ func TestEnsureHostsInPoolNodeIP(t *testing.T) {
LoadBalancerBackendAddresses: &[]network.LoadBalancerBackendAddress{},
},
},
multiSLBConfigs: []MultipleStandardLoadBalancerConfiguration{
multiSLBConfigs: []config.MultipleStandardLoadBalancerConfiguration{
{
Name: "kubernetes",
},
Expand Down Expand Up @@ -343,7 +344,7 @@ func TestEnsureHostsInPoolNodeIP(t *testing.T) {
LoadBalancerBackendAddresses: &[]network.LoadBalancerBackendAddress{},
},
},
multiSLBConfigs: []MultipleStandardLoadBalancerConfiguration{
multiSLBConfigs: []config.MultipleStandardLoadBalancerConfiguration{
{
Name: "kubernetes",
},
Expand Down Expand Up @@ -942,7 +943,7 @@ func TestReconcileBackendPoolsNodeIPConfigToIPWithMigrationAPI(t *testing.T) {
az.LoadBalancerClient = mockLBClient
az.EnableMigrateToIPBasedBackendPoolAPI = true
az.LoadBalancerSku = "standard"
az.MultipleStandardLoadBalancerConfigurations = []MultipleStandardLoadBalancerConfiguration{{Name: "kubernetes"}}
az.MultipleStandardLoadBalancerConfigurations = []config.MultipleStandardLoadBalancerConfiguration{{Name: "kubernetes"}}

bi := newBackendPoolTypeNodeIP(az)
svc := getTestService("test", v1.ProtocolTCP, nil, false, 80)
Expand Down
Loading

0 comments on commit 1d378c4

Please sign in to comment.