From fcd0c6706fe4930919e43734d2da8db49b7a7142 Mon Sep 17 00:00:00 2001 From: t-qini Date: Tue, 6 Aug 2019 22:59:04 +0800 Subject: [PATCH 1/2] Support cross resource group load balancer. --- .../azure/azure_backoff.go | 14 ++-- .../azure/azure_loadbalancer.go | 64 +++++++++++++-- .../azure/azure_loadbalancer_test.go | 77 +++++++++++++++++++ 3 files changed, 144 insertions(+), 11 deletions(-) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go index 6a584b723b6ca..5ab39c42eae35 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go @@ -25,7 +25,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network" "github.com/Azure/go-autorest/autorest/to" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" @@ -232,7 +232,8 @@ func (az *Cloud) CreateOrUpdateLB(service *v1.Service, lb network.LoadBalancer) ctx, cancel := getContextWithCancel() defer cancel() - resp, err := az.LoadBalancerClient.CreateOrUpdate(ctx, az.ResourceGroup, *lb.Name, lb, to.String(lb.Etag)) + rgName := az.getLoadBalancerResourceGroup(service) + resp, err := az.LoadBalancerClient.CreateOrUpdate(ctx, rgName, *lb.Name, lb, to.String(lb.Etag)) klog.V(10).Infof("LoadBalancerClient.CreateOrUpdate(%s): end", *lb.Name) if err == nil { if isSuccessHTTPResponse(resp) { @@ -259,7 +260,8 @@ func (az *Cloud) createOrUpdateLBWithRetry(service *v1.Service, lb network.LoadB ctx, cancel := getContextWithCancel() defer cancel() - resp, err := az.LoadBalancerClient.CreateOrUpdate(ctx, az.ResourceGroup, *lb.Name, lb, to.String(lb.Etag)) + rgName := az.getLoadBalancerResourceGroup(service) + resp, err := az.LoadBalancerClient.CreateOrUpdate(ctx, rgName, *lb.Name, lb, to.String(lb.Etag)) klog.V(10).Infof("LoadBalancerClient.CreateOrUpdate(%s): end", *lb.Name) done, retryError := az.processHTTPRetryResponse(service, "CreateOrUpdateLoadBalancer", resp, err) if done && err == nil { @@ -282,7 +284,8 @@ func (az *Cloud) ListLB(service *v1.Service) ([]network.LoadBalancer, error) { ctx, cancel := getContextWithCancel() defer cancel() - allLBs, err := az.LoadBalancerClient.List(ctx, az.ResourceGroup) + rgName := az.getLoadBalancerResourceGroup(service) + allLBs, err := az.LoadBalancerClient.List(ctx, rgName) if err != nil { az.Event(service, v1.EventTypeWarning, "ListLoadBalancers", err.Error()) klog.Errorf("LoadBalancerClient.List(%v) failure with err=%v", az.ResourceGroup, err) @@ -304,7 +307,8 @@ func (az *Cloud) listLBWithRetry(service *v1.Service) ([]network.LoadBalancer, e ctx, cancel := getContextWithCancel() defer cancel() - allLBs, retryErr = az.LoadBalancerClient.List(ctx, az.ResourceGroup) + rgName := az.getLoadBalancerResourceGroup(service) + allLBs, retryErr = az.LoadBalancerClient.List(ctx, rgName) if retryErr != nil { az.Event(service, v1.EventTypeWarning, "ListLoadBalancers", retryErr.Error()) klog.Errorf("LoadBalancerClient.List(%v) - backoff: failure, will retry,err=%v", diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go index b91d4ffc28c24..536ffebce6f8f 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go @@ -24,7 +24,7 @@ import ( "strconv" "strings" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/sets" cloudprovider "k8s.io/cloud-provider" servicehelpers "k8s.io/cloud-provider/service/helpers" @@ -93,6 +93,8 @@ const ( serviceTagKey = "service" // clusterNameKey is the cluster name key applied for public IP tags. clusterNameKey = "kubernetes-cluster-name" + // LoadBalancerName + LoadBalancerName = "load-balancer-name" ) // GetLoadBalancer returns whether the specified load balancer exists, and @@ -219,10 +221,42 @@ func (az *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName stri // GetLoadBalancerName returns the LoadBalancer name. func (az *Cloud) GetLoadBalancerName(ctx context.Context, clusterName string, service *v1.Service) string { - // TODO: replace DefaultLoadBalancerName to generate more meaningful loadbalancer names. + if lbName, found := service.Annotations[LoadBalancerName]; found && strings.TrimSpace(lbName) != "" { + return lbName + } return cloudprovider.DefaultLoadBalancerName(service) } +func (az *Cloud) getLoadBalancerResourceGroup(service *v1.Service) string { + if rgName, found := service.Annotations[ServiceAnnotationLoadBalancerResourceGroup]; found && strings.TrimSpace(rgName) != "" { + return rgName + } + return az.ResourceGroup +} + +// getLoadBalancerByName get the specific lb with given load balancer name and resource group. +// It works with annotation "service.beta.kubernetes.io/azure-load-balancer-resource-group" +func (az *Cloud) getLoadBalancerByName(service *v1.Service, lbName string) (lb *network.LoadBalancer, status *v1.LoadBalancerStatus, exists bool, err error) { + existingLBs, err := az.ListLB(service) + if err != nil { + return nil, nil, false, err + } + if existingLBs == nil { + return nil, nil, false, fmt.Errorf("cannot obtain the load balancer %q", lbName) + } + for _, existingLB := range existingLBs { + if strings.EqualFold(*existingLB.Name, lbName) { + status, err = az.getServiceLoadBalancerStatus(service, &existingLB) + if err != nil { + return nil, nil, false, err + } + return &existingLB, status, true, err + } + } + + return nil, nil, false, fmt.Errorf("cannot obtain the load balancer %q", lbName) +} + // getServiceLoadBalancer gets the loadbalancer for the service if it already exists. // If wantLb is TRUE then -it selects a new load balancer. // In case the selected load balancer does not exist it returns network.LoadBalancer struct @@ -595,14 +629,32 @@ func (az *Cloud) isFrontendIPChanged(clusterName string, config network.Frontend // This entails adding rules/probes for expected Ports and removing stale rules/ports. // nodes only used if wantLb is true func (az *Cloud) reconcileLoadBalancer(clusterName string, service *v1.Service, nodes []*v1.Node, wantLb bool) (*network.LoadBalancer, error) { + var ( + lb *network.LoadBalancer + exists bool + err error + rgName string + ) + isInternal := requiresInternalLoadBalancer(service) serviceName := getServiceName(service) klog.V(2).Infof("reconcileLoadBalancer for service(%s) - wantLb(%t): started", serviceName, wantLb) - lb, _, _, err := az.getServiceLoadBalancer(service, clusterName, nodes, wantLb) - if err != nil { - klog.Errorf("reconcileLoadBalancer: failed to get load balancer for service %q, error: %v", serviceName, err) - return nil, err + + if rgName = az.getLoadBalancerResourceGroup(service); rgName != az.ResourceGroup { + lbName := az.GetLoadBalancerName(context.Background(), clusterName, service) + lb, _, exists, err = az.getLoadBalancerByName(service, lbName) + if err != nil { + klog.Errorf("reconcileLoadBalancer: failed to get load balancer for service %q in resource group %q, error: %v, will continue in the default resource group", serviceName, rgName, err) + } } + if !exists || strings.TrimSpace(rgName) == "" || strings.TrimSpace(rgName) == az.ResourceGroup { + lb, _, _, err = az.getServiceLoadBalancer(service, clusterName, nodes, wantLb) + if err != nil { + klog.Errorf("reconcileLoadBalancer: failed to get load balancer for service %q, error: %v", serviceName, err) + return nil, err + } + } + lbName := *lb.Name klog.V(2).Infof("reconcileLoadBalancer for service(%s): lb(%s) wantLb(%t) resolved load balancer name", serviceName, lbName, wantLb) lbFrontendIPConfigName := az.getFrontendIPConfigName(service, subnet(service)) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go index fcd81719e02cd..479bfd2e37831 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go @@ -676,6 +676,83 @@ func TestGetServiceLoadBalancer(t *testing.T) { } } +func TestGetLoadBalancerByName(t *testing.T) { + testCases := []struct { + desc string + existingLBs []network.LoadBalancer + expectedLB *network.LoadBalancer + expectedStatus *v1.LoadBalancerStatus + expectedExists bool + expectedError bool + }{ + { + desc: "getLoadBalancerByName shall return the corresponding load balancer in the resource group different from the cluster's", + existingLBs: []network.LoadBalancer{ + { + Name: to.StringPtr("me"), + LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{ + FrontendIPConfigurations: &[]network.FrontendIPConfiguration{ + { + Name: to.StringPtr("atest1"), + FrontendIPConfigurationPropertiesFormat: &network.FrontendIPConfigurationPropertiesFormat{ + PublicIPAddress: &network.PublicIPAddress{ID: to.StringPtr("id1")}, + }, + }, + }, + }, + }, + { + Name: to.StringPtr("notMe"), + LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{ + FrontendIPConfigurations: &[]network.FrontendIPConfiguration{ + { + Name: to.StringPtr("atest1"), + FrontendIPConfigurationPropertiesFormat: &network.FrontendIPConfigurationPropertiesFormat{ + PublicIPAddress: &network.PublicIPAddress{ID: to.StringPtr("id1")}, + }, + }, + }, + }, + }, + }, + expectedLB: &network.LoadBalancer{ + Name: to.StringPtr("me"), + LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{ + FrontendIPConfigurations: &[]network.FrontendIPConfiguration{ + { + Name: to.StringPtr("atest1"), + FrontendIPConfigurationPropertiesFormat: &network.FrontendIPConfigurationPropertiesFormat{ + PublicIPAddress: &network.PublicIPAddress{ID: to.StringPtr("id1")}, + }, + }, + }, + }, + }, + expectedStatus: &v1.LoadBalancerStatus{Ingress: []v1.LoadBalancerIngress{{IP: "", Hostname: ""}}}, + expectedExists: true, + expectedError: false, + }, + } + + for i, test := range testCases { + az := getTestCloud() + service := getTestService("test1", v1.ProtocolTCP, nil, 80) + service.Annotations[ServiceAnnotationLoadBalancerResourceGroup] = "rg1" + for _, existingLB := range test.existingLBs { + _, err := az.LoadBalancerClient.CreateOrUpdate(context.TODO(), "rg1", *existingLB.Name, existingLB, "") + if err != nil { + t.Fatalf("TestCase[%d] meets unexpected error: %v", i, err) + } + } + + lb, status, exists, err := az.getLoadBalancerByName(&service, "me") + assert.Equal(t, test.expectedLB, lb, "TestCase[%d]: %s", i, test.desc) + assert.Equal(t, test.expectedStatus, status, "TestCase[%d]: %s", i, test.desc) + assert.Equal(t, test.expectedExists, exists, "TestCase[%d]: %s", i, test.desc) + assert.Equal(t, test.expectedError, err != nil, "TestCase[%d]: %s", i, test.desc) + } +} + func TestIsFrontendIPChanged(t *testing.T) { testCases := []struct { desc string From 659e857a1de60596ba163af581885bd357ba770d Mon Sep 17 00:00:00 2001 From: t-qini Date: Thu, 8 Aug 2019 13:54:23 +0800 Subject: [PATCH 2/2] Add load balancer cross resource group settings to config. --- .../legacy-cloud-providers/azure/azure.go | 7 ++ .../azure/azure_backoff.go | 18 +++-- .../azure/azure_loadbalancer.go | 61 ++------------- .../azure/azure_loadbalancer_test.go | 77 ------------------- .../azure/azure_standard.go | 3 + .../azure/azure_wrap.go | 2 +- 6 files changed, 29 insertions(+), 139 deletions(-) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure.go index 932b501c20aa4..0ef4e6d8367f3 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure.go @@ -163,6 +163,13 @@ type Config struct { // The cloud configure type for Azure cloud provider. Supported values are file, secret and merge. CloudConfigType cloudConfigType `json:"cloudConfigType,omitempty" yaml:"cloudConfigType,omitempty"` + + // LoadBalancerName determines the specific name of the load balancer user want to use, working with + // LoadBalancerResourceGroup + LoadBalancerName string `json:"loadBalancerName,omitempty" yaml:"loadBalancerName,omitempty"` + // LoadBalancerResourceGroup determines the specific resource group of the load balancer user want to use, working + // with LoadBalancerName + LoadBalancerResourceGroup string `json:"loadBalancerResourceGroup,omitempty" yaml:"loadBalancerResourceGroup,omitempty"` } var _ cloudprovider.Interface = (*Cloud)(nil) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go index 5ab39c42eae35..ec4e49c78105a 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_backoff.go @@ -232,7 +232,7 @@ func (az *Cloud) CreateOrUpdateLB(service *v1.Service, lb network.LoadBalancer) ctx, cancel := getContextWithCancel() defer cancel() - rgName := az.getLoadBalancerResourceGroup(service) + rgName := az.getLoadBalancerResourceGroup() resp, err := az.LoadBalancerClient.CreateOrUpdate(ctx, rgName, *lb.Name, lb, to.String(lb.Etag)) klog.V(10).Infof("LoadBalancerClient.CreateOrUpdate(%s): end", *lb.Name) if err == nil { @@ -260,7 +260,7 @@ func (az *Cloud) createOrUpdateLBWithRetry(service *v1.Service, lb network.LoadB ctx, cancel := getContextWithCancel() defer cancel() - rgName := az.getLoadBalancerResourceGroup(service) + rgName := az.getLoadBalancerResourceGroup() resp, err := az.LoadBalancerClient.CreateOrUpdate(ctx, rgName, *lb.Name, lb, to.String(lb.Etag)) klog.V(10).Infof("LoadBalancerClient.CreateOrUpdate(%s): end", *lb.Name) done, retryError := az.processHTTPRetryResponse(service, "CreateOrUpdateLoadBalancer", resp, err) @@ -284,11 +284,11 @@ func (az *Cloud) ListLB(service *v1.Service) ([]network.LoadBalancer, error) { ctx, cancel := getContextWithCancel() defer cancel() - rgName := az.getLoadBalancerResourceGroup(service) + rgName := az.getLoadBalancerResourceGroup() allLBs, err := az.LoadBalancerClient.List(ctx, rgName) if err != nil { az.Event(service, v1.EventTypeWarning, "ListLoadBalancers", err.Error()) - klog.Errorf("LoadBalancerClient.List(%v) failure with err=%v", az.ResourceGroup, err) + klog.Errorf("LoadBalancerClient.List(%v) failure with err=%v", rgName, err) return nil, err } klog.V(2).Infof("LoadBalancerClient.List(%v) success", az.ResourceGroup) @@ -307,12 +307,12 @@ func (az *Cloud) listLBWithRetry(service *v1.Service) ([]network.LoadBalancer, e ctx, cancel := getContextWithCancel() defer cancel() - rgName := az.getLoadBalancerResourceGroup(service) + rgName := az.getLoadBalancerResourceGroup() allLBs, retryErr = az.LoadBalancerClient.List(ctx, rgName) if retryErr != nil { az.Event(service, v1.EventTypeWarning, "ListLoadBalancers", retryErr.Error()) klog.Errorf("LoadBalancerClient.List(%v) - backoff: failure, will retry,err=%v", - az.ResourceGroup, + rgName, retryErr) return false, retryErr } @@ -454,7 +454,8 @@ func (az *Cloud) DeleteLB(service *v1.Service, lbName string) error { ctx, cancel := getContextWithCancel() defer cancel() - resp, err := az.LoadBalancerClient.Delete(ctx, az.ResourceGroup, lbName) + rgName := az.getLoadBalancerResourceGroup() + resp, err := az.LoadBalancerClient.Delete(ctx, rgName, lbName) if err == nil { if isSuccessHTTPResponse(resp) { // Invalidate the cache right after updating @@ -475,7 +476,8 @@ func (az *Cloud) deleteLBWithRetry(service *v1.Service, lbName string) error { ctx, cancel := getContextWithCancel() defer cancel() - resp, err := az.LoadBalancerClient.Delete(ctx, az.ResourceGroup, lbName) + rgName := az.getLoadBalancerResourceGroup() + resp, err := az.LoadBalancerClient.Delete(ctx, rgName, lbName) done, err := az.processHTTPRetryResponse(service, "DeleteLoadBalancer", resp, err) if done && err == nil { // Invalidate the cache right after deleting diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go index 536ffebce6f8f..c5481326fb3c3 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer.go @@ -93,8 +93,6 @@ const ( serviceTagKey = "service" // clusterNameKey is the cluster name key applied for public IP tags. clusterNameKey = "kubernetes-cluster-name" - // LoadBalancerName - LoadBalancerName = "load-balancer-name" ) // GetLoadBalancer returns whether the specified load balancer exists, and @@ -221,40 +219,15 @@ func (az *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName stri // GetLoadBalancerName returns the LoadBalancer name. func (az *Cloud) GetLoadBalancerName(ctx context.Context, clusterName string, service *v1.Service) string { - if lbName, found := service.Annotations[LoadBalancerName]; found && strings.TrimSpace(lbName) != "" { - return lbName - } return cloudprovider.DefaultLoadBalancerName(service) } -func (az *Cloud) getLoadBalancerResourceGroup(service *v1.Service) string { - if rgName, found := service.Annotations[ServiceAnnotationLoadBalancerResourceGroup]; found && strings.TrimSpace(rgName) != "" { - return rgName - } - return az.ResourceGroup -} - -// getLoadBalancerByName get the specific lb with given load balancer name and resource group. -// It works with annotation "service.beta.kubernetes.io/azure-load-balancer-resource-group" -func (az *Cloud) getLoadBalancerByName(service *v1.Service, lbName string) (lb *network.LoadBalancer, status *v1.LoadBalancerStatus, exists bool, err error) { - existingLBs, err := az.ListLB(service) - if err != nil { - return nil, nil, false, err - } - if existingLBs == nil { - return nil, nil, false, fmt.Errorf("cannot obtain the load balancer %q", lbName) - } - for _, existingLB := range existingLBs { - if strings.EqualFold(*existingLB.Name, lbName) { - status, err = az.getServiceLoadBalancerStatus(service, &existingLB) - if err != nil { - return nil, nil, false, err - } - return &existingLB, status, true, err - } +func (az *Cloud) getLoadBalancerResourceGroup() string { + if az.LoadBalancerResourceGroup != "" { + return az.LoadBalancerResourceGroup } - return nil, nil, false, fmt.Errorf("cannot obtain the load balancer %q", lbName) + return az.ResourceGroup } // getServiceLoadBalancer gets the loadbalancer for the service if it already exists. @@ -629,32 +602,14 @@ func (az *Cloud) isFrontendIPChanged(clusterName string, config network.Frontend // This entails adding rules/probes for expected Ports and removing stale rules/ports. // nodes only used if wantLb is true func (az *Cloud) reconcileLoadBalancer(clusterName string, service *v1.Service, nodes []*v1.Node, wantLb bool) (*network.LoadBalancer, error) { - var ( - lb *network.LoadBalancer - exists bool - err error - rgName string - ) - isInternal := requiresInternalLoadBalancer(service) serviceName := getServiceName(service) klog.V(2).Infof("reconcileLoadBalancer for service(%s) - wantLb(%t): started", serviceName, wantLb) - - if rgName = az.getLoadBalancerResourceGroup(service); rgName != az.ResourceGroup { - lbName := az.GetLoadBalancerName(context.Background(), clusterName, service) - lb, _, exists, err = az.getLoadBalancerByName(service, lbName) - if err != nil { - klog.Errorf("reconcileLoadBalancer: failed to get load balancer for service %q in resource group %q, error: %v, will continue in the default resource group", serviceName, rgName, err) - } - } - if !exists || strings.TrimSpace(rgName) == "" || strings.TrimSpace(rgName) == az.ResourceGroup { - lb, _, _, err = az.getServiceLoadBalancer(service, clusterName, nodes, wantLb) - if err != nil { - klog.Errorf("reconcileLoadBalancer: failed to get load balancer for service %q, error: %v", serviceName, err) - return nil, err - } + lb, _, _, err := az.getServiceLoadBalancer(service, clusterName, nodes, wantLb) + if err != nil { + klog.Errorf("reconcileLoadBalancer: failed to get load balancer for service %q, error: %v", serviceName, err) + return nil, err } - lbName := *lb.Name klog.V(2).Infof("reconcileLoadBalancer for service(%s): lb(%s) wantLb(%t) resolved load balancer name", serviceName, lbName, wantLb) lbFrontendIPConfigName := az.getFrontendIPConfigName(service, subnet(service)) diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go index 479bfd2e37831..fcd81719e02cd 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_loadbalancer_test.go @@ -676,83 +676,6 @@ func TestGetServiceLoadBalancer(t *testing.T) { } } -func TestGetLoadBalancerByName(t *testing.T) { - testCases := []struct { - desc string - existingLBs []network.LoadBalancer - expectedLB *network.LoadBalancer - expectedStatus *v1.LoadBalancerStatus - expectedExists bool - expectedError bool - }{ - { - desc: "getLoadBalancerByName shall return the corresponding load balancer in the resource group different from the cluster's", - existingLBs: []network.LoadBalancer{ - { - Name: to.StringPtr("me"), - LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{ - FrontendIPConfigurations: &[]network.FrontendIPConfiguration{ - { - Name: to.StringPtr("atest1"), - FrontendIPConfigurationPropertiesFormat: &network.FrontendIPConfigurationPropertiesFormat{ - PublicIPAddress: &network.PublicIPAddress{ID: to.StringPtr("id1")}, - }, - }, - }, - }, - }, - { - Name: to.StringPtr("notMe"), - LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{ - FrontendIPConfigurations: &[]network.FrontendIPConfiguration{ - { - Name: to.StringPtr("atest1"), - FrontendIPConfigurationPropertiesFormat: &network.FrontendIPConfigurationPropertiesFormat{ - PublicIPAddress: &network.PublicIPAddress{ID: to.StringPtr("id1")}, - }, - }, - }, - }, - }, - }, - expectedLB: &network.LoadBalancer{ - Name: to.StringPtr("me"), - LoadBalancerPropertiesFormat: &network.LoadBalancerPropertiesFormat{ - FrontendIPConfigurations: &[]network.FrontendIPConfiguration{ - { - Name: to.StringPtr("atest1"), - FrontendIPConfigurationPropertiesFormat: &network.FrontendIPConfigurationPropertiesFormat{ - PublicIPAddress: &network.PublicIPAddress{ID: to.StringPtr("id1")}, - }, - }, - }, - }, - }, - expectedStatus: &v1.LoadBalancerStatus{Ingress: []v1.LoadBalancerIngress{{IP: "", Hostname: ""}}}, - expectedExists: true, - expectedError: false, - }, - } - - for i, test := range testCases { - az := getTestCloud() - service := getTestService("test1", v1.ProtocolTCP, nil, 80) - service.Annotations[ServiceAnnotationLoadBalancerResourceGroup] = "rg1" - for _, existingLB := range test.existingLBs { - _, err := az.LoadBalancerClient.CreateOrUpdate(context.TODO(), "rg1", *existingLB.Name, existingLB, "") - if err != nil { - t.Fatalf("TestCase[%d] meets unexpected error: %v", i, err) - } - } - - lb, status, exists, err := az.getLoadBalancerByName(&service, "me") - assert.Equal(t, test.expectedLB, lb, "TestCase[%d]: %s", i, test.desc) - assert.Equal(t, test.expectedStatus, status, "TestCase[%d]: %s", i, test.desc) - assert.Equal(t, test.expectedExists, exists, "TestCase[%d]: %s", i, test.desc) - assert.Equal(t, test.expectedError, err != nil, "TestCase[%d]: %s", i, test.desc) - } -} - func TestIsFrontendIPChanged(t *testing.T) { testCases := []struct { desc string diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard.go index 4feeca2207709..0170074ea0f38 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_standard.go @@ -127,6 +127,9 @@ func (az *Cloud) mapLoadBalancerNameToVMSet(lbName string, clusterName string) ( // So we'd have a separate name for internal load balancer. // This would be the name for Azure LoadBalancer resource. func (az *Cloud) getAzureLoadBalancerName(clusterName string, vmSetName string, isInternal bool) string { + if az.LoadBalancerName != "" { + clusterName = az.LoadBalancerName + } lbNamePrefix := vmSetName if strings.EqualFold(vmSetName, az.vmSet.GetPrimaryVMSetName()) || az.useStandardLoadBalancer() { lbNamePrefix = clusterName diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_wrap.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_wrap.go index 37f6477c43010..33974a44afdcb 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_wrap.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_wrap.go @@ -233,7 +233,7 @@ func (az *Cloud) newLBCache() (*timedCache, error) { ctx, cancel := getContextWithCancel() defer cancel() - lb, err := az.LoadBalancerClient.Get(ctx, az.ResourceGroup, key, "") + lb, err := az.LoadBalancerClient.Get(ctx, az.getLoadBalancerResourceGroup(), key, "") exists, message, realErr := checkResourceExistsFromError(err) if realErr != nil { return nil, realErr