Repository: kubernetes-sigs/cloud-provider-azure
Pull request #1228 (2022-03-07 09:27:58)

fix: add tags matching in storage account search
<!--  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 bug

#### What this PR does / why we need it:
fix: add tags matching in storage account search for azure file and blob csi drivers.
This PR adds the tags matching in storage account search for azure file and blob csi drivers.

#### 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
fix: add tags matching in storage account search for azure file and blob csi drivers.
```

#### 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
fix: add tags matching in storage account search for azure file and blob csi drivers.
```


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/1228#" title="Author self-approved">andyzhangx</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)~~ [andyzhangx]

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":[]} -->
coveralls: 
[![Coverage Status](https://coveralls.io/builds/47118998/badge)](https://coveralls.io/builds/47118998)

Coverage increased (+0.02%) to 80.195% when pulling **81654855691b7865dedef7b9cc3a7d49ce6f72b8 on andyzhangx:tags-equal** into **c036e6f3b335abb423e3e86191fbc1ae7f9e39c6 on kubernetes-sigs:master**.


Patch:
diff --git a/pkg/provider/azure_storageaccount.go b/pkg/provider/azure_storageaccount.go
index 49e70d633..184934c3f 100644
--- a/pkg/provider/azure_storageaccount.go
+++ b/pkg/provider/azure_storageaccount.go
@@ -80,6 +80,7 @@ func (az *Cloud) getStorageAccounts(ctx context.Context, accountOptions *Account
 				isLocationEqual(acct, accountOptions) &&
 				AreVNetRulesEqual(acct, accountOptions) &&
 				isLargeFileSharesPropertyEqual(acct, accountOptions) &&
+				isTagsEqual(acct, accountOptions) &&
 				isTaggedWithSkip(acct) &&
 				isHnsPropertyEqual(acct, accountOptions) &&
 				isEnableNfsV3PropertyEqual(acct, accountOptions) &&
@@ -496,6 +497,26 @@ func isTaggedWithSkip(account storage.Account) bool {
 	return true
 }
 
+func isTagsEqual(account storage.Account, accountOptions *AccountOptions) bool {
+	// nil and empty map should be regarded as equal
+	if len(account.Tags) == 0 && len(accountOptions.Tags) == 0 {
+		return true
+	}
+
+	for k, v := range account.Tags {
+		var value string
+		// nil and empty value should be regarded as equal
+		if v != nil {
+			value = *v
+		}
+		if accountOptions.Tags[k] != value {
+			return false
+		}
+	}
+
+	return true
+}
+
 func isHnsPropertyEqual(account storage.Account, accountOptions *AccountOptions) bool {
 	return to.Bool(account.IsHnsEnabled) == to.Bool(accountOptions.IsHnsEnabled)
 }


Test patch:
diff --git a/pkg/provider/azure_storageaccount_test.go b/pkg/provider/azure_storageaccount_test.go
index 8f80e2bf1..66891d5d6 100644
--- a/pkg/provider/azure_storageaccount_test.go
+++ b/pkg/provider/azure_storageaccount_test.go
@@ -23,6 +23,7 @@ import (
 
 	"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network"
 	"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2021-02-01/storage"
+	"github.com/Azure/go-autorest/autorest/to"
 	"github.com/golang/mock/gomock"
 	"github.com/stretchr/testify/assert"
 
@@ -483,3 +484,72 @@ func TestIsPrivateEndpointAsExpected(t *testing.T) {
 		assert.Equal(t, result, test.expectedResult)
 	}
 }
+
+func TestIsTagsEqual(t *testing.T) {
+	tests := []struct {
+		desc           string
+		account        storage.Account
+		accountOptions *AccountOptions
+		expectedResult bool
+	}{
+		{
+			desc: "empty tags",
+			account: storage.Account{
+				Tags: nil,
+			},
+			accountOptions: &AccountOptions{},
+			expectedResult: true,
+		},
+		{
+			desc: "identitical tags",
+			account: storage.Account{
+				Tags: map[string]*string{
+					"key":  to.StringPtr("value"),
+					"key2": nil,
+				},
+			},
+			accountOptions: &AccountOptions{
+				Tags: map[string]string{
+					"key":  "value",
+					"key2": "",
+				},
+			},
+			expectedResult: true,
+		},
+		{
+			desc: "identitical tags",
+			account: storage.Account{
+				Tags: map[string]*string{
+					"key":  to.StringPtr("value"),
+					"key2": to.StringPtr("value2"),
+				},
+			},
+			accountOptions: &AccountOptions{
+				Tags: map[string]string{
+					"key2": "value2",
+					"key":  "value",
+				},
+			},
+			expectedResult: true,
+		},
+		{
+			desc: "non-identitical tags",
+			account: storage.Account{
+				Tags: map[string]*string{
+					"key": to.StringPtr("value2"),
+				},
+			},
+			accountOptions: &AccountOptions{
+				Tags: map[string]string{
+					"key": "value",
+				},
+			},
+			expectedResult: false,
+		},
+	}
+
+	for _, test := range tests {
+		result := isTagsEqual(test.account, test.accountOptions)
+		assert.Equal(t, result, test.expectedResult)
+	}
+}