From 23a7bd5c99c945bf0cf9028950f650d87952680b Mon Sep 17 00:00:00 2001 From: Pengfei Ni Date: Fri, 28 Jun 2019 16:11:05 +0800 Subject: [PATCH] Default resourceGroup should be used when value of annotation azure-load-balancer-resource-group is empty string --- .../providers/azure/azure_loadbalancer.go | 5 ++- .../azure/azure_loadbalancer_test.go | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkg/cloudprovider/providers/azure/azure_loadbalancer.go b/pkg/cloudprovider/providers/azure/azure_loadbalancer.go index ccb9dedd13..e632775400 100644 --- a/pkg/cloudprovider/providers/azure/azure_loadbalancer.go +++ b/pkg/cloudprovider/providers/azure/azure_loadbalancer.go @@ -1542,7 +1542,10 @@ func findSecurityRule(rules []network.SecurityRule, rule network.SecurityRule) b func (az *Cloud) getPublicIPAddressResourceGroup(service *v1.Service) string { if resourceGroup, found := service.Annotations[ServiceAnnotationLoadBalancerResourceGroup]; found { - return resourceGroup + resourceGroupName := strings.TrimSpace(resourceGroup) + if len(resourceGroupName) > 0 { + return resourceGroupName + } } return az.ResourceGroup diff --git a/pkg/cloudprovider/providers/azure/azure_loadbalancer_test.go b/pkg/cloudprovider/providers/azure/azure_loadbalancer_test.go index d7fe868ce3..d949092cc3 100644 --- a/pkg/cloudprovider/providers/azure/azure_loadbalancer_test.go +++ b/pkg/cloudprovider/providers/azure/azure_loadbalancer_test.go @@ -368,3 +368,35 @@ func TestEnsureLoadBalancerDeleted(t *testing.T) { assert.Equal(t, len(result), 0, "TestCase[%d]: %s", i, c.desc) } } + +func TestGetPublicIPAddressResourceGroup(t *testing.T) { + az := getTestCloud() + + for i, c := range []struct { + desc string + annotations map[string]string + expected string + }{ + { + desc: "no annotation", + expected: "rg", + }, + { + desc: "annoation with empty string resource group", + annotations: map[string]string{ServiceAnnotationLoadBalancerResourceGroup: ""}, + expected: "rg", + }, + { + desc: "annoation with non-empty resource group ", + annotations: map[string]string{ServiceAnnotationLoadBalancerResourceGroup: "rg2"}, + expected: "rg2", + }, + } { + t.Run(c.desc, func(t *testing.T) { + s := &v1.Service{} + s.Annotations = c.annotations + real := az.getPublicIPAddressResourceGroup(s) + assert.Equal(t, c.expected, real, "TestCase[%d]: %s", i, c.desc) + }) + } +}