Repository: kubernetes-sigs/cloud-provider-azure
Pull request #2993 (2022-12-22 13:45:33)

feat: add DisableUpdateCache flag
<!--  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 feature

#### What this PR does / why we need it:
feat: add DisableUpdateCache flag
this flag could control update cache behavior in csi driver disk attach/detach

#### 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
feat: add DisableUpdateCache flag
```

#### 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
feat: add DisableUpdateCache flag
```



Patch:
diff --git a/pkg/provider/azure_controller_common.go b/pkg/provider/azure_controller_common.go
index a8feb0893..cab363a62 100644
--- a/pkg/provider/azure_controller_common.go
+++ b/pkg/provider/azure_controller_common.go
@@ -100,6 +100,8 @@ type controllerCommon struct {
 	detachDiskMap sync.Map
 	// attach/detach disk rate limiter
 	diskOpRateLimiter flowcontrol.RateLimiter
+	// DisableUpdateCache whether disable update cache in disk attach/detach
+	DisableUpdateCache bool
 }
 
 // AttachDiskOptions attach disk options
@@ -432,16 +434,11 @@ func (c *controllerCommon) UpdateVM(ctx context.Context, nodeName types.NodeName
 	defer c.lockMap.UnlockEntry(node)
 
 	defer func() {
-		// invalidate the cache if there is an error with UpdateVM operation
-		if err != nil {
-			_ = vmset.DeleteCacheForNode(string(nodeName))
-		}
+		_ = vmset.DeleteCacheForNode(string(nodeName))
 	}()
 
 	klog.V(2).Infof("azureDisk - update: vm(%s)", nodeName)
-	err = vmset.UpdateVM(ctx, nodeName)
-	klog.V(2).Infof("azureDisk - update: vm(%s) returned with %v", err)
-	return err
+	return vmset.UpdateVM(ctx, nodeName)
 }
 
 func (c *controllerCommon) insertDetachDiskRequest(diskName, diskURI, nodeName string) error {
diff --git a/pkg/provider/azure_controller_standard.go b/pkg/provider/azure_controller_standard.go
index e6e2b054e..9d6f42915 100644
--- a/pkg/provider/azure_controller_standard.go
+++ b/pkg/provider/azure_controller_standard.go
@@ -265,6 +265,9 @@ func (as *availabilitySet) UpdateVMAsync(ctx context.Context, nodeName types.Nod
 }
 
 func (as *availabilitySet) updateCache(nodeName string, vm *compute.VirtualMachine) {
+	if as.common.DisableUpdateCache {
+		return
+	}
 	as.cloud.vmCache.Update(nodeName, vm)
 	klog.V(2).Infof("updateCache(%s) successfully", nodeName)
 }
diff --git a/pkg/provider/azure_controller_vmssflex.go b/pkg/provider/azure_controller_vmssflex.go
index 1c0eb1e51..7f34ad971 100644
--- a/pkg/provider/azure_controller_vmssflex.go
+++ b/pkg/provider/azure_controller_vmssflex.go
@@ -264,6 +264,9 @@ func (fs *FlexScaleSet) UpdateVMAsync(ctx context.Context, nodeName types.NodeNa
 }
 
 func (fs *FlexScaleSet) updateCache(nodeName string, vm *compute.VirtualMachine) error {
+	if fs.common.DisableUpdateCache {
+		return nil
+	}
 	if vm == nil {
 		return fmt.Errorf("vm is nil")
 	}
@@ -290,7 +293,6 @@ func (fs *FlexScaleSet) updateCache(nodeName string, vm *compute.VirtualMachine)
 	}
 	vmMap := cached.(*sync.Map)
 	vmMap.Store(nodeName, vm)
-	fs.vmssFlexVMCache.Update(vmssFlexID, vmMap)
 
 	fs.vmssFlexVMNameToVmssID.Store(strings.ToLower(*vm.OsProfile.ComputerName), vmssFlexID)
 	fs.vmssFlexVMNameToNodeName.Store(*vm.Name, strings.ToLower(*vm.OsProfile.ComputerName))
diff --git a/pkg/provider/azure_utils.go b/pkg/provider/azure_utils.go
index 4fa3fd722..0b000774a 100644
--- a/pkg/provider/azure_utils.go
+++ b/pkg/provider/azure_utils.go
@@ -328,3 +328,17 @@ func isNodeInVMSSVMCache(nodeName string, vmssVMCache *azcache.TimedCache) bool
 
 	return isInCache
 }
+
+func extractVmssVMName(name string) (string, string, error) {
+	split := strings.SplitAfter(name, consts.VMSSNameSeparator)
+	if len(split) < 2 {
+		klog.V(3).Infof("Failed to extract vmssVMName %q", name)
+		return "", "", ErrorNotVmssInstance
+	}
+
+	ssName := strings.Join(split[0:len(split)-1], "")
+	// removing the trailing `vmssNameSeparator` since we used SplitAfter
+	ssName = ssName[:len(ssName)-1]
+	instanceID := split[len(split)-1]
+	return ssName, instanceID, nil
+}
diff --git a/pkg/provider/azure_vmss_cache.go b/pkg/provider/azure_vmss_cache.go
index 231cd94a6..7b71b641d 100644
--- a/pkg/provider/azure_vmss_cache.go
+++ b/pkg/provider/azure_vmss_cache.go
@@ -122,20 +122,6 @@ func (ss *ScaleSet) newVMSSCache(ctx context.Context) (*azcache.TimedCache, erro
 	return azcache.NewTimedcache(time.Duration(ss.Config.VmssCacheTTLInSeconds)*time.Second, getter)
 }
 
-func extractVmssVMName(name string) (string, string, error) {
-	split := strings.SplitAfter(name, consts.VMSSNameSeparator)
-	if len(split) < 2 {
-		klog.V(3).Infof("Failed to extract vmssVMName %q", name)
-		return "", "", ErrorNotVmssInstance
-	}
-
-	ssName := strings.Join(split[0:len(split)-1], "")
-	// removing the trailing `vmssNameSeparator` since we used SplitAfter
-	ssName = ssName[:len(ssName)-1]
-	instanceID := split[len(split)-1]
-	return ssName, instanceID, nil
-}
-
 func (ss *ScaleSet) getVMSSVMsFromCache(resourceGroup, vmssName string, crt azcache.AzureCacheReadType) (*sync.Map, error) {
 	cacheKey := getVMSSVMCacheKey(resourceGroup, vmssName)
 	entry, err := ss.vmssVMCache.Get(cacheKey, crt)
@@ -158,7 +144,6 @@ func (ss *ScaleSet) newVMSSVirtualMachinesCache() (*azcache.TimedCache, error) {
 
 	getter := func(cacheKey string) (interface{}, error) {
 		localCache := &sync.Map{} // [nodeName]*VMSSVirtualMachineEntry
-
 		oldCache := make(map[string]*VMSSVirtualMachineEntry)
 
 		entry, exists, err := ss.vmssVMCache.Store.GetByKey(cacheKey)
@@ -292,6 +277,9 @@ func (ss *ScaleSet) DeleteCacheForNode(nodeName string) error {
 }
 
 func (ss *ScaleSet) updateCache(nodeName, resourceGroupName, vmssName, instanceID string, updatedVM *compute.VirtualMachineScaleSetVM) error {
+	if ss.common.DisableUpdateCache {
+		return nil
+	}
 	// lock the VMSS entry to ensure a consistent view of the VM map when there are concurrent updates.
 	cacheKey := getVMSSVMCacheKey(resourceGroupName, vmssName)
 	ss.lockMap.LockEntry(cacheKey)


Test patch:
diff --git a/pkg/provider/azure_controller_vmssflex_test.go b/pkg/provider/azure_controller_vmssflex_test.go
index 864429c39..af5f34708 100644
--- a/pkg/provider/azure_controller_vmssflex_test.go
+++ b/pkg/provider/azure_controller_vmssflex_test.go
@@ -344,7 +344,7 @@ func TestGetDataDisksWithVmssFlex(t *testing.T) {
 	}
 }
 
-func TestUpdateCache(t *testing.T) {
+func TestVMSSFlexUpdateCache(t *testing.T) {
 	ctrl := gomock.NewController(t)
 	defer ctrl.Finish()
 
@@ -352,10 +352,11 @@ func TestUpdateCache(t *testing.T) {
 	assert.NoError(t, err, "unexpected error when creating test FlexScaleSet")
 
 	testCases := []struct {
-		description string
-		nodeName    string
-		vm          *compute.VirtualMachine
-		expectedErr error
+		description        string
+		nodeName           string
+		vm                 *compute.VirtualMachine
+		disableUpdateCache bool
+		expectedErr        error
 	}{
 		{
 			description: "vm is nil",
@@ -388,11 +389,16 @@ func TestUpdateCache(t *testing.T) {
 			},
 			expectedErr: fmt.Errorf("vm.OsProfile.ComputerName is nil"),
 		},
+		{
+			description:        "disableUpdateCache is set",
+			disableUpdateCache: true,
+			expectedErr:        nil,
+		},
 	}
 
 	for _, test := range testCases {
+		fs.DisableUpdateCache = test.disableUpdateCache
 		err = fs.updateCache(test.nodeName, test.vm)
 		assert.Equal(t, test.expectedErr, err, test.description)
 	}
-
 }
diff --git a/pkg/provider/azure_utils_test.go b/pkg/provider/azure_utils_test.go
index 7308269cf..16031e45b 100644
--- a/pkg/provider/azure_utils_test.go
+++ b/pkg/provider/azure_utils_test.go
@@ -404,3 +404,47 @@ func TestIsNodeInVMSSVMCache(t *testing.T) {
 		assert.Equal(t, test.expectedResult, result, test.description)
 	}
 }
+
+func TestExtractVmssVMName(t *testing.T) {
+	cases := []struct {
+		description        string
+		vmName             string
+		expectError        bool
+		expectedScaleSet   string
+		expectedInstanceID string
+	}{
+		{
+			description: "wrong vmss VM name should report error",
+			vmName:      "vm1234",
+			expectError: true,
+		},
+		{
+			description: "wrong VM name separator should report error",
+			vmName:      "vm-1234",
+			expectError: true,
+		},
+		{
+			description:        "correct vmss VM name should return correct ScaleSet and instanceID",
+			vmName:             "vm_1234",
+			expectedScaleSet:   "vm",
+			expectedInstanceID: "1234",
+		},
+		{
+			description:        "correct vmss VM name with Extra Separator should return correct ScaleSet and instanceID",
+			vmName:             "vm_test_1234",
+			expectedScaleSet:   "vm_test",
+			expectedInstanceID: "1234",
+		},
+	}
+
+	for _, c := range cases {
+		ssName, instanceID, err := extractVmssVMName(c.vmName)
+		if c.expectError {
+			assert.Error(t, err, c.description)
+			continue
+		}
+
+		assert.Equal(t, c.expectedScaleSet, ssName, c.description)
+		assert.Equal(t, c.expectedInstanceID, instanceID, c.description)
+	}
+}
diff --git a/pkg/provider/azure_vmss_cache_test.go b/pkg/provider/azure_vmss_cache_test.go
index aa721c291..73b9e5e08 100644
--- a/pkg/provider/azure_vmss_cache_test.go
+++ b/pkg/provider/azure_vmss_cache_test.go
@@ -36,50 +36,6 @@ import (
 	"sigs.k8s.io/cloud-provider-azure/pkg/retry"
 )
 
-func TestExtractVmssVMName(t *testing.T) {
-	cases := []struct {
-		description        string
-		vmName             string
-		expectError        bool
-		expectedScaleSet   string
-		expectedInstanceID string
-	}{
-		{
-			description: "wrong vmss VM name should report error",
-			vmName:      "vm1234",
-			expectError: true,
-		},
-		{
-			description: "wrong VM name separator should report error",
-			vmName:      "vm-1234",
-			expectError: true,
-		},
-		{
-			description:        "correct vmss VM name should return correct ScaleSet and instanceID",
-			vmName:             "vm_1234",
-			expectedScaleSet:   "vm",
-			expectedInstanceID: "1234",
-		},
-		{
-			description:        "correct vmss VM name with Extra Separator should return correct ScaleSet and instanceID",
-			vmName:             "vm_test_1234",
-			expectedScaleSet:   "vm_test",
-			expectedInstanceID: "1234",
-		},
-	}
-
-	for _, c := range cases {
-		ssName, instanceID, err := extractVmssVMName(c.vmName)
-		if c.expectError {
-			assert.Error(t, err, c.description)
-			continue
-		}
-
-		assert.Equal(t, c.expectedScaleSet, ssName, c.description)
-		assert.Equal(t, c.expectedInstanceID, instanceID, c.description)
-	}
-}
-
 func TestVMSSVMCache(t *testing.T) {
 	ctrl := gomock.NewController(t)
 	defer ctrl.Finish()
@@ -446,3 +402,32 @@ func TestGetVMManagementTypeByIPConfigurationID(t *testing.T) {
 
 	}
 }
+
+func TestVMSSUpdateCache(t *testing.T) {
+	ctrl := gomock.NewController(t)
+	defer ctrl.Finish()
+
+	ss, err := NewTestScaleSet(ctrl)
+	assert.NoError(t, err, "unexpected error when creating test ScaleSet")
+
+	testCases := []struct {
+		description                 string
+		nodeName, resourceGroupName string
+		vmssName, instanceID        string
+		vm                          *compute.VirtualMachineScaleSetVM
+		disableUpdateCache          bool
+		expectedErr                 error
+	}{
+		{
+			description:        "disableUpdateCache is set",
+			disableUpdateCache: true,
+			expectedErr:        nil,
+		},
+	}
+
+	for _, test := range testCases {
+		ss.DisableUpdateCache = test.disableUpdateCache
+		err = ss.updateCache(test.nodeName, test.nodeName, test.resourceGroupName, test.instanceID, test.vm)
+		assert.Equal(t, test.expectedErr, err, test.description)
+	}
+}