Repository: kubernetes-sigs/cloud-provider-azure
Pull request #3854 (2023-05-05 07:09:17)

[Refactor] Drop RG param in getXXXID()
<!--  Thanks for sending a pull request!  Here are some tips for you:

1. If this is your first time, please read our contributor guidelines: https://git.k8s.io/community/contributors/guide/first-contribution.md#your-first-contribution and developer guide https://git.k8s.io/community/contributors/devel/development.md#development-guide
2. Please label this pull request according to what type of issue you are addressing, especially if this is a release targeted pull request. For reference on required PR/issue labels, read here:
https://git.k8s.io/community/contributors/devel/sig-release/release.md#issuepr-kind-label
3. Ensure you have added or ran the appropriate tests for your PR: https://git.k8s.io/community/contributors/devel/sig-testing/testing.md
4. If you want *faster* PR reviews, read how: https://git.k8s.io/community/contributors/guide/pull-requests.md#best-practices-for-faster-reviews
5. If the PR is unfinished, see how to mark it: https://git.k8s.io/community/contributors/guide/pull-requests.md#marking-unfinished-pull-requests
-->

#### What type of PR is this?
/kind cleanup
<!--
Add one of the following kinds:
/kind bug
/kind cleanup
/kind documentation
/kind feature
/kind design

Optionally add one or more of the following kinds if applicable:
/kind api-change
/kind deprecation
/kind failing-test
/kind flake
/kind regression
-->

#### What this PR does / why we need it:
[Refactor] Drop RG param in getXXXID(). Resource group name can be got from az.getLoadBalancerResourceGroup().
#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
_If PR is about `failing-tests or flakes`, please post the related issues/tests in a comment and do not use `Fixes`_*
-->
Fixes #

#### Special notes for your reviewer:

#### Does this PR introduce a user-facing change?
<!--
If no, just write "NONE" in the release-note block below.
If yes, a release note is required:
Enter your extended release note in the block below. If the PR requires additional action from users switching to the new release, include the string "action required".

For more information on release notes see: https://git.k8s.io/community/contributors/guide/release-notes.md
-->
```release-note
NONE
```

#### Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.:

<!--
This section can be blank if this pull request does not require a release note.

When adding links which point to resources within git repositories, like
KEPs or supporting documentation, please reference a specific commit and avoid
linking directly to the master branch. This ensures that links reference a
specific point in time, rather than a document that may change over time.

See here for guidance on getting permanent links to files: https://help.github.com/en/articles/getting-permanent-links-to-files

Please use the following format for linking documentation:
- [KEP]: <link>
- [Usage]: <link>
- [Other doc]: <link>
-->
```docs

```


netlify[bot]: ### <span aria-hidden="true">👷</span> Deploy Preview for *kubernetes-sigs-cloud-provide-azure* processing.


|  Name | Link |
|:-:|------------------------|
|<span aria-hidden="true">🔨</span> Latest commit | 92af615098199940249f5cdf0c60a61a4324a585 |
|<span aria-hidden="true">🔍</span> Latest deploy log | https://app.netlify.com/sites/kubernetes-sigs-cloud-provide-azure/deploys/6454ab9ff418080008429ff1 |
k8s-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/cloud-provider-azure/pull/3854#" title="Author self-approved">lzhecheng</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fcloud-provider-azure).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubernetes-sigs/cloud-provider-azure/blob/master/OWNERS)~~ [lzhecheng]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/provider/azure_loadbalancer.go b/pkg/provider/azure_loadbalancer.go
index 9386bb4b9..6ae6c70c4 100644
--- a/pkg/provider/azure_loadbalancer.go
+++ b/pkg/provider/azure_loadbalancer.go
@@ -429,10 +429,9 @@ func (az *Cloud) cleanOrphanedLoadBalancer(lb *network.LoadBalancer, existingLBs
 	lbName := pointer.StringDeref(lb.Name, "")
 	serviceName := getServiceName(service)
 	isBackendPoolPreConfigured := az.isBackendPoolPreConfigured(service)
-	lbResourceGroup := az.getLoadBalancerResourceGroup()
 	isIPv6 := utilnet.IsIPv6String(service.Spec.ClusterIP)
 	lbBackendPoolName := getBackendPoolName(clusterName, isIPv6)
-	lbBackendPoolID := az.getBackendPoolID(lbName, lbResourceGroup, lbBackendPoolName)
+	lbBackendPoolID := az.getBackendPoolID(lbName, lbBackendPoolName)
 	if isBackendPoolPreConfigured {
 		klog.V(2).Infof("cleanOrphanedLoadBalancer(%s, %s, %s): ignore cleanup of dirty lb because the lb is pre-configured", lbName, serviceName, clusterName)
 	} else {
@@ -504,7 +503,7 @@ func (az *Cloud) cleanOrphanedLoadBalancer(lb *network.LoadBalancer, existingLBs
 // safeDeleteLoadBalancer deletes the load balancer after decoupling it from the vmSet
 func (az *Cloud) safeDeleteLoadBalancer(lb network.LoadBalancer, clusterName, vmSetName string, service *v1.Service) *retry.Error {
 	isIPv6 := utilnet.IsIPv6String(service.Spec.ClusterIP)
-	lbBackendPoolID := az.getBackendPoolID(pointer.StringDeref(lb.Name, ""), az.getLoadBalancerResourceGroup(), getBackendPoolName(clusterName, isIPv6))
+	lbBackendPoolID := az.getBackendPoolID(pointer.StringDeref(lb.Name, ""), getBackendPoolName(clusterName, isIPv6))
 	_, err := az.VMSet.EnsureBackendPoolDeleted(service, []string{lbBackendPoolID}, vmSetName, lb.BackendAddressPools, true)
 	if err != nil {
 		return retry.NewError(false, fmt.Errorf("safeDeleteLoadBalancer: failed to EnsureBackendPoolDeleted: %w", err))
@@ -1524,12 +1523,12 @@ func (az *Cloud) reconcileLoadBalancer(clusterName string, service *v1.Service,
 	}
 
 	lbName := *lb.Name
-	lbResourceGroup := az.getLoadBalancerResourceGroup()
 	isIPv6 := utilnet.IsIPv6String(service.Spec.ClusterIP)
-	lbBackendPoolID := az.getBackendPoolID(lbName, az.getLoadBalancerResourceGroup(), getBackendPoolName(clusterName, isIPv6))
-	klog.V(2).Infof("reconcileLoadBalancer for service(%s): lb(%s/%s) wantLb(%t) resolved load balancer name", serviceName, lbResourceGroup, lbName, wantLb)
+	lbBackendPoolID := az.getBackendPoolID(lbName, getBackendPoolName(clusterName, isIPv6))
+	klog.V(2).Infof("reconcileLoadBalancer for service(%s): lb(%s/%s) wantLb(%t) resolved load balancer name",
+		serviceName, az.getLoadBalancerResourceGroup(), lbName, wantLb)
 	defaultLBFrontendIPConfigName := az.getDefaultFrontendIPConfigName(service)
-	defaultLBFrontendIPConfigID := az.getFrontendIPConfigID(lbName, lbResourceGroup, defaultLBFrontendIPConfigName)
+	defaultLBFrontendIPConfigID := az.getFrontendIPConfigID(lbName, defaultLBFrontendIPConfigName)
 	dirtyLb := false
 
 	// reconcile the load balancer's backend pool configuration.
@@ -2305,7 +2304,7 @@ func (az *Cloud) getExpectedLBRules(
 				}
 				if portprobe != nil {
 					props.Probe = &network.SubResource{
-						ID: pointer.String(az.getLoadBalancerProbeID(lbName, az.getLoadBalancerResourceGroup(), *portprobe.Name)),
+						ID: pointer.String(az.getLoadBalancerProbeID(lbName, *portprobe.Name)),
 					}
 					expectedProbes = append(expectedProbes, *portprobe)
 					break
@@ -2313,7 +2312,7 @@ func (az *Cloud) getExpectedLBRules(
 			}
 		} else {
 			props.Probe = &network.SubResource{
-				ID: pointer.String(az.getLoadBalancerProbeID(lbName, az.getLoadBalancerResourceGroup(), *nodeEndpointHealthprobe.Name)),
+				ID: pointer.String(az.getLoadBalancerProbeID(lbName, *nodeEndpointHealthprobe.Name)),
 			}
 		}
 
@@ -2367,13 +2366,13 @@ func (az *Cloud) getExpectedLBRules(
 					}
 					if portprobe != nil {
 						props.Probe = &network.SubResource{
-							ID: pointer.String(az.getLoadBalancerProbeID(lbName, az.getLoadBalancerResourceGroup(), *portprobe.Name)),
+							ID: pointer.String(az.getLoadBalancerProbeID(lbName, *portprobe.Name)),
 						}
 						expectedProbes = append(expectedProbes, *portprobe)
 					}
 				} else {
 					props.Probe = &network.SubResource{
-						ID: pointer.String(az.getLoadBalancerProbeID(lbName, az.getLoadBalancerResourceGroup(), *nodeEndpointHealthprobe.Name)),
+						ID: pointer.String(az.getLoadBalancerProbeID(lbName, *nodeEndpointHealthprobe.Name)),
 					}
 				}
 			}
diff --git a/pkg/provider/azure_loadbalancer_backendpool.go b/pkg/provider/azure_loadbalancer_backendpool.go
index e7f75d29e..19d283a90 100644
--- a/pkg/provider/azure_loadbalancer_backendpool.go
+++ b/pkg/provider/azure_loadbalancer_backendpool.go
@@ -89,10 +89,9 @@ func (bc *backendPoolTypeNodeIPConfig) CleanupVMSetFromBackendPoolByCondition(sl
 		false: getBackendPoolName(clusterName, false),
 		true:  getBackendPoolName(clusterName, true),
 	}
-	lbResourceGroup := bc.getLoadBalancerResourceGroup()
 	lbBackendPoolIDs := map[bool]string{
-		false: bc.getBackendPoolID(pointer.StringDeref(slb.Name, ""), lbResourceGroup, lbBackendPoolNames[false]),
-		true:  bc.getBackendPoolID(pointer.StringDeref(slb.Name, ""), lbResourceGroup, lbBackendPoolNames[true]),
+		false: bc.getBackendPoolID(pointer.StringDeref(slb.Name, ""), lbBackendPoolNames[false]),
+		true:  bc.getBackendPoolID(pointer.StringDeref(slb.Name, ""), lbBackendPoolNames[true]),
 	}
 	newBackendPools := make([]network.BackendAddressPool, 0)
 	if slb.LoadBalancerPropertiesFormat != nil && slb.BackendAddressPools != nil {
@@ -181,8 +180,8 @@ func (bc *backendPoolTypeNodeIPConfig) ReconcileBackendPools(clusterName string,
 		true:  getBackendPoolName(clusterName, true),
 	}
 	lbBackendPoolIDs := map[bool]string{
-		false: bc.getBackendPoolID(lbName, bc.getLoadBalancerResourceGroup(), lbBackendPoolNames[false]),
-		true:  bc.getBackendPoolID(lbName, bc.getLoadBalancerResourceGroup(), lbBackendPoolNames[true]),
+		false: bc.getBackendPoolID(lbName, lbBackendPoolNames[false]),
+		true:  bc.getBackendPoolID(lbName, lbBackendPoolNames[true]),
 	}
 	vmSetName := bc.mapLoadBalancerNameToVMSet(lbName, clusterName)
 	isBackendPoolPreConfigured := bc.isBackendPoolPreConfigured(service)
@@ -571,8 +570,8 @@ func (bi *backendPoolTypeNodeIP) ReconcileBackendPools(clusterName string, servi
 	}
 	vmSetName := bi.mapLoadBalancerNameToVMSet(lbName, clusterName)
 	lbBackendPoolIDs := map[bool]string{
-		false: bi.getBackendPoolID(pointer.StringDeref(lb.Name, ""), bi.getLoadBalancerResourceGroup(), lbBackendPoolNames[false]),
-		true:  bi.getBackendPoolID(pointer.StringDeref(lb.Name, ""), bi.getLoadBalancerResourceGroup(), lbBackendPoolNames[true]),
+		false: bi.getBackendPoolID(pointer.StringDeref(lb.Name, ""), lbBackendPoolNames[false]),
+		true:  bi.getBackendPoolID(pointer.StringDeref(lb.Name, ""), lbBackendPoolNames[true]),
 	}
 	isBackendPoolPreConfigured := bi.isBackendPoolPreConfigured(service)
 
diff --git a/pkg/provider/azure_standard.go b/pkg/provider/azure_standard.go
index d8c9d43c7..90674a1a7 100644
--- a/pkg/provider/azure_standard.go
+++ b/pkg/provider/azure_standard.go
@@ -69,7 +69,11 @@ func (az *Cloud) getAvailabilitySetID(resourceGroup, availabilitySetName string)
 }
 
 // returns the full identifier of a loadbalancer frontendipconfiguration.
-func (az *Cloud) getFrontendIPConfigID(lbName, rgName, fipConfigName string) string {
+func (az *Cloud) getFrontendIPConfigID(lbName, fipConfigName string) string {
+	return az.getFrontendIPConfigIDWithRG(lbName, az.getLoadBalancerResourceGroup(), fipConfigName)
+}
+
+func (az *Cloud) getFrontendIPConfigIDWithRG(lbName, rgName, fipConfigName string) string {
 	return fmt.Sprintf(
 		consts.FrontendIPConfigIDTemplate,
 		az.getNetworkResourceSubscriptionID(),
@@ -79,7 +83,11 @@ func (az *Cloud) getFrontendIPConfigID(lbName, rgName, fipConfigName string) str
 }
 
 // returns the full identifier of a loadbalancer backendpool.
-func (az *Cloud) getBackendPoolID(lbName, rgName, backendPoolName string) string {
+func (az *Cloud) getBackendPoolID(lbName, backendPoolName string) string {
+	return az.getBackendPoolIDWithRG(lbName, az.getLoadBalancerResourceGroup(), backendPoolName)
+}
+
+func (az *Cloud) getBackendPoolIDWithRG(lbName, rgName, backendPoolName string) string {
 	return fmt.Sprintf(
 		consts.BackendPoolIDTemplate,
 		az.getNetworkResourceSubscriptionID(),
@@ -89,7 +97,11 @@ func (az *Cloud) getBackendPoolID(lbName, rgName, backendPoolName string) string
 }
 
 // returns the full identifier of a loadbalancer probe.
-func (az *Cloud) getLoadBalancerProbeID(lbName, rgName, lbRuleName string) string {
+func (az *Cloud) getLoadBalancerProbeID(lbName, lbRuleName string) string {
+	return az.getLoadBalancerProbeIDWithRG(lbName, az.getLoadBalancerResourceGroup(), lbRuleName)
+}
+
+func (az *Cloud) getLoadBalancerProbeIDWithRG(lbName, rgName, lbRuleName string) string {
 	return fmt.Sprintf(
 		consts.LoadBalancerProbeIDTemplate,
 		az.getNetworkResourceSubscriptionID(),


Test patch:
diff --git a/pkg/provider/azure_standard_test.go b/pkg/provider/azure_standard_test.go
index 97a00032c..50a9517c5 100644
--- a/pkg/provider/azure_standard_test.go
+++ b/pkg/provider/azure_standard_test.go
@@ -518,7 +518,7 @@ func TestGetFrontendIPConfigID(t *testing.T) {
 	defer ctrl.Finish()
 	az := GetTestCloud(ctrl)
 
-	testGetLoadBalancerSubResourceID(t, az, az.getFrontendIPConfigID, consts.FrontendIPConfigIDTemplate)
+	testGetLoadBalancerSubResourceID(t, az, az.getFrontendIPConfigID, az.getFrontendIPConfigIDWithRG, consts.FrontendIPConfigIDTemplate)
 }
 
 func TestGetBackendPoolID(t *testing.T) {
@@ -526,7 +526,7 @@ func TestGetBackendPoolID(t *testing.T) {
 	defer ctrl.Finish()
 	az := GetTestCloud(ctrl)
 
-	testGetLoadBalancerSubResourceID(t, az, az.getBackendPoolID, consts.BackendPoolIDTemplate)
+	testGetLoadBalancerSubResourceID(t, az, az.getBackendPoolID, az.getBackendPoolIDWithRG, consts.BackendPoolIDTemplate)
 }
 
 func TestGetLoadBalancerProbeID(t *testing.T) {
@@ -534,70 +534,66 @@ func TestGetLoadBalancerProbeID(t *testing.T) {
 	defer ctrl.Finish()
 	az := GetTestCloud(ctrl)
 
-	testGetLoadBalancerSubResourceID(t, az, az.getLoadBalancerProbeID, consts.LoadBalancerProbeIDTemplate)
+	testGetLoadBalancerSubResourceID(t, az, az.getLoadBalancerProbeID, az.getLoadBalancerProbeIDWithRG, consts.LoadBalancerProbeIDTemplate)
 }
 
 func testGetLoadBalancerSubResourceID(
 	t *testing.T,
 	az *Cloud,
-	getLoadBalancerSubResourceID func(string, string, string) string,
+	getLoadBalancerSubResourceID func(string, string) string,
+	getLoadBalancerSubResourceIDWithRG func(string, string, string) string,
 	expectedResourceIDTemplate string) {
 	cases := []struct {
 		description                         string
-		loadBalancerName                    string
-		resourceGroupName                   string
-		subResourceName                     string
 		useNetworkResourceInDifferentTenant bool
 		useNetworkResourceInDifferentSub    bool
-		expected                            string
 	}{
 		{
 			description:                         "resource id should contain NetworkResourceSubscriptionID when using network resources in different tenant and subscription",
-			loadBalancerName:                    "lbName",
-			resourceGroupName:                   "rgName",
-			subResourceName:                     "subResourceName",
 			useNetworkResourceInDifferentTenant: true,
 			useNetworkResourceInDifferentSub:    true,
 		},
 		{
 			description:                         "resource id should contain NetworkResourceSubscriptionID when using network resources in different subscription",
-			loadBalancerName:                    "lbName",
-			resourceGroupName:                   "rgName",
-			subResourceName:                     "subResourceName",
 			useNetworkResourceInDifferentTenant: false,
 			useNetworkResourceInDifferentSub:    true,
 		},
 		{
 			description:                         "resource id should contain SubscriptionID when not using network resources in different subscription",
-			loadBalancerName:                    "lbName",
-			resourceGroupName:                   "rgName",
-			subResourceName:                     "subResourceName",
 			useNetworkResourceInDifferentTenant: false,
 			useNetworkResourceInDifferentSub:    false,
 		},
 	}
 
 	for _, c := range cases {
-		subscriptionID := az.SubscriptionID
-		if c.useNetworkResourceInDifferentTenant {
-			az.NetworkResourceTenantID = networkResourceTenantID
-		} else {
-			az.NetworkResourceTenantID = ""
-		}
-		if c.useNetworkResourceInDifferentSub {
-			az.NetworkResourceSubscriptionID = networkResourceSubscriptionID
-			subscriptionID = networkResourceSubscriptionID
-		} else {
-			az.NetworkResourceSubscriptionID = ""
-		}
-		c.expected = fmt.Sprintf(
-			expectedResourceIDTemplate,
-			subscriptionID,
-			c.resourceGroupName,
-			c.loadBalancerName,
-			c.subResourceName)
-		subResourceID := getLoadBalancerSubResourceID(c.loadBalancerName, c.resourceGroupName, c.subResourceName)
-		assert.Equal(t, c.expected, subResourceID, c.description)
+		t.Run(c.description, func(t *testing.T) {
+			subscriptionID := az.SubscriptionID
+			rgName := "rgName"
+			lbName := "lbName"
+			subResourceName := "subResourceName"
+			az.ResourceGroup = rgName
+			if c.useNetworkResourceInDifferentTenant {
+				az.NetworkResourceTenantID = networkResourceTenantID
+			} else {
+				az.NetworkResourceTenantID = ""
+			}
+			if c.useNetworkResourceInDifferentSub {
+				az.NetworkResourceSubscriptionID = networkResourceSubscriptionID
+				subscriptionID = networkResourceSubscriptionID
+			} else {
+				az.NetworkResourceSubscriptionID = ""
+			}
+			expected := fmt.Sprintf(
+				expectedResourceIDTemplate,
+				subscriptionID,
+				rgName,
+				lbName,
+				subResourceName)
+			subResourceID := getLoadBalancerSubResourceID(lbName, subResourceName)
+			assert.Equal(t, expected, subResourceID)
+			subResourceIDWithRG := getLoadBalancerSubResourceIDWithRG(lbName, rgName, subResourceName)
+			assert.Equal(t, expected, subResourceIDWithRG)
+		})
 	}
 }