Repository: kubernetes-sigs/cloud-provider-azure
Pull request #4575 (2023-09-07 03:13:09)

fix: match account if PrivateEndpoint is not set in storage class
<!--  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:
chore: match account if PrivateEndpoint is not set in storage class
This PR change CreatePrivateEndpoint var as `*bool`, if it's not set, then it's nil

#### 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
none
```



Patch:
diff --git a/pkg/provider/azure_storageaccount.go b/pkg/provider/azure_storageaccount.go
index ee0fe7db9..7e50bb2dd 100644
--- a/pkg/provider/azure_storageaccount.go
+++ b/pkg/provider/azure_storageaccount.go
@@ -56,7 +56,7 @@ type AccountOptions struct {
 	EnableHTTPSTrafficOnly                    bool
 	// indicate whether create new account when Name is empty or when account does not exists
 	CreateAccount                           bool
-	CreatePrivateEndpoint                   bool
+	CreatePrivateEndpoint                   *bool
 	StorageType                             StorageType
 	StorageEndpointSuffix                   string
 	DisableFileServiceDeleteRetentionPolicy *bool
@@ -216,7 +216,7 @@ func (az *Cloud) EnsureStorageAccount(ctx context.Context, accountOptions *Accou
 	}
 
 	var privateDNSZoneName string
-	if accountOptions.CreatePrivateEndpoint {
+	if pointer.BoolDeref(accountOptions.CreatePrivateEndpoint, false) {
 		if accountOptions.StorageType == "" {
 			klog.V(2).Info("set StorageType as file when not specified")
 			accountOptions.StorageType = StorageTypeFile
@@ -282,7 +282,7 @@ func (az *Cloud) EnsureStorageAccount(ctx context.Context, accountOptions *Accou
 		}
 	}
 
-	if accountOptions.CreatePrivateEndpoint {
+	if pointer.BoolDeref(accountOptions.CreatePrivateEndpoint, false) {
 		if _, err := az.privatednsclient.Get(ctx, vnetResourceGroup, privateDNSZoneName); err != nil {
 			klog.V(2).Infof("get private dns zone %s returned with %v", privateDNSZoneName, err.Error())
 			// Create DNS zone first, this could make sure driver has write permission on vnetResourceGroup
@@ -320,7 +320,7 @@ func (az *Cloud) EnsureStorageAccount(ctx context.Context, accountOptions *Accou
 			}
 		}
 
-		if accountOptions.CreatePrivateEndpoint {
+		if pointer.BoolDeref(accountOptions.CreatePrivateEndpoint, false) {
 			networkRuleSet = &storage.NetworkRuleSet{
 				DefaultAction: storage.DefaultActionDeny,
 			}
@@ -471,7 +471,7 @@ func (az *Cloud) EnsureStorageAccount(ctx context.Context, accountOptions *Accou
 		}
 	}
 
-	if accountOptions.CreatePrivateEndpoint {
+	if pointer.BoolDeref(accountOptions.CreatePrivateEndpoint, false) {
 		// Get properties of the storageAccount
 		storageAccount, err := az.StorageAccountClient.GetProperties(ctx, subsID, resourceGroup, accountName)
 		if err != nil {
@@ -770,10 +770,15 @@ func isEnableNfsV3PropertyEqual(account storage.Account, accountOptions *Account
 }
 
 func isPrivateEndpointAsExpected(account storage.Account, accountOptions *AccountOptions) bool {
-	if accountOptions.CreatePrivateEndpoint && account.PrivateEndpointConnections != nil && len(*account.PrivateEndpointConnections) > 0 {
+	if accountOptions.CreatePrivateEndpoint == nil {
+		// CreatePrivateEndpoint is not set, match current account
 		return true
 	}
-	if !accountOptions.CreatePrivateEndpoint && (account.PrivateEndpointConnections == nil || len(*account.PrivateEndpointConnections) == 0) {
+
+	if pointer.BoolDeref(accountOptions.CreatePrivateEndpoint, false) && account.PrivateEndpointConnections != nil && len(*account.PrivateEndpointConnections) > 0 {
+		return true
+	}
+	if !pointer.BoolDeref(accountOptions.CreatePrivateEndpoint, false) && (account.PrivateEndpointConnections == nil || len(*account.PrivateEndpointConnections) == 0) {
 		return true
 	}
 	return false


Test patch:
diff --git a/pkg/provider/azure_storageaccount_test.go b/pkg/provider/azure_storageaccount_test.go
index 0c7a3adb2..9ad2cd007 100644
--- a/pkg/provider/azure_storageaccount_test.go
+++ b/pkg/provider/azure_storageaccount_test.go
@@ -343,7 +343,7 @@ func TestGetStorageAccountEdgeCases(t *testing.T) {
 			testCase: "account options CreatePrivateEndpoint is true and no private endpoint exists",
 			testAccountOptions: &AccountOptions{
 				ResourceGroup:         "rg",
-				CreatePrivateEndpoint: true,
+				CreatePrivateEndpoint: pointer.BoolPtr(true),
 			},
 			testResourceGroups: []storage.Account{{Name: &name, Kind: "kind", Location: &location, Sku: sku, AccountProperties: &storage.AccountProperties{}}},
 			expectedResult:     []accountWithLocation{},
@@ -409,7 +409,7 @@ func TestEnsureStorageAccount(t *testing.T) {
 	tests := []struct {
 		name                            string
 		createAccount                   bool
-		createPrivateEndpoint           bool
+		createPrivateEndpoint           *bool
 		SubnetPropertiesFormatNil       bool
 		mockStorageAccountsClient       bool
 		setAccountOptions               bool
@@ -426,7 +426,7 @@ func TestEnsureStorageAccount(t *testing.T) {
 		{
 			name:                            "[Success] EnsureStorageAccount with createPrivateEndpoint and storagetype blob",
 			createAccount:                   true,
-			createPrivateEndpoint:           true,
+			createPrivateEndpoint:           pointer.BoolPtr(true),
 			mockStorageAccountsClient:       true,
 			setAccountOptions:               true,
 			pickRandomMatchingAccount:       true,
@@ -441,7 +441,7 @@ func TestEnsureStorageAccount(t *testing.T) {
 		{
 			name:                            "[Success] EnsureStorageAccount with createPrivateEndpoint",
 			createAccount:                   true,
-			createPrivateEndpoint:           true,
+			createPrivateEndpoint:           pointer.BoolPtr(true),
 			mockStorageAccountsClient:       true,
 			setAccountOptions:               true,
 			requireInfrastructureEncryption: pointer.Bool(true),
@@ -454,7 +454,7 @@ func TestEnsureStorageAccount(t *testing.T) {
 		{
 			name:                      "[Failed] EnsureStorageAccount with createPrivateEndpoint: get storage key failed",
 			createAccount:             true,
-			createPrivateEndpoint:     true,
+			createPrivateEndpoint:     pointer.BoolPtr(true),
 			SubnetPropertiesFormatNil: true,
 			mockStorageAccountsClient: true,
 			setAccountOptions:         true,
@@ -496,7 +496,7 @@ func TestEnsureStorageAccount(t *testing.T) {
 			cloud.StorageAccountClient = mockStorageAccountsClient
 		}
 
-		if test.createPrivateEndpoint {
+		if pointer.BoolDeref(test.createPrivateEndpoint, false) {
 			mockStorageAccountsClient.EXPECT().ListByResourceGroup(gomock.Any(), gomock.Any(), gomock.Any()).Return(testStorageAccounts, nil).AnyTimes()
 			mockStorageAccountsClient.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
 			mockStorageAccountsClient.EXPECT().GetProperties(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(testStorageAccounts[0], nil).AnyTimes()
@@ -629,7 +629,7 @@ func TestIsPrivateEndpointAsExpected(t *testing.T) {
 				},
 			},
 			accountOptions: &AccountOptions{
-				CreatePrivateEndpoint: true,
+				CreatePrivateEndpoint: pointer.BoolPtr(true),
 			},
 			expectedResult: true,
 		},
@@ -640,7 +640,7 @@ func TestIsPrivateEndpointAsExpected(t *testing.T) {
 				},
 			},
 			accountOptions: &AccountOptions{
-				CreatePrivateEndpoint: false,
+				CreatePrivateEndpoint: pointer.BoolPtr(false),
 			},
 			expectedResult: true,
 		},
@@ -651,10 +651,21 @@ func TestIsPrivateEndpointAsExpected(t *testing.T) {
 				},
 			},
 			accountOptions: &AccountOptions{
-				CreatePrivateEndpoint: false,
+				CreatePrivateEndpoint: pointer.BoolPtr(false),
 			},
 			expectedResult: false,
 		},
+		{
+			account: storage.Account{
+				AccountProperties: &storage.AccountProperties{
+					PrivateEndpointConnections: &[]storage.PrivateEndpointConnection{{}},
+				},
+			},
+			accountOptions: &AccountOptions{
+				CreatePrivateEndpoint: nil,
+			},
+			expectedResult: true,
+		},
 		{
 			account: storage.Account{
 				AccountProperties: &storage.AccountProperties{
@@ -662,7 +673,7 @@ func TestIsPrivateEndpointAsExpected(t *testing.T) {
 				},
 			},
 			accountOptions: &AccountOptions{
-				CreatePrivateEndpoint: true,
+				CreatePrivateEndpoint: pointer.BoolPtr(true),
 			},
 			expectedResult: false,
 		},