@@ -31,6 +31,8 @@ import (
31
31
"k8s.io/klog/v2"
32
32
"k8s.io/utils/pointer"
33
33
34
+ "sigs.k8s.io/cloud-provider-azure/pkg/cache"
35
+ azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache"
34
36
"sigs.k8s.io/cloud-provider-azure/pkg/consts"
35
37
"sigs.k8s.io/cloud-provider-azure/pkg/retry"
36
38
)
@@ -586,12 +588,44 @@ func (az *Cloud) createPrivateDNSZoneGroup(ctx context.Context, dnsZoneGroupName
586
588
return az .privatednszonegroupclient .CreateOrUpdate (ctx , vnetResourceGroup , privateEndpointName , dnsZoneGroupName , privateDNSZoneGroup , "" , false ).Error ()
587
589
}
588
590
589
- // AddStorageAccountTags add tags to storage account
590
- func (az * Cloud ) AddStorageAccountTags (ctx context.Context , subsID , resourceGroup , account string , tags map [string ]* string ) * retry.Error {
591
+ func (az * Cloud ) newStorageAccountCache () (azcache.Resource , error ) {
592
+ getter := func (key string ) (interface {}, error ) { return nil , nil }
593
+ return azcache .NewTimedCache (time .Minute , getter , az .Config .DisableAPICallCache )
594
+ }
595
+
596
+ func (az * Cloud ) getStorageAccountWithCache (ctx context.Context , subsID , resourceGroup , account string ) (storage.Account , * retry.Error ) {
591
597
if az .StorageAccountClient == nil {
592
- return retry .NewError (false , fmt .Errorf ("StorageAccountClient is nil" ))
598
+ return storage.Account {}, retry .NewError (false , fmt .Errorf ("StorageAccountClient is nil" ))
599
+ }
600
+
601
+ if az .storageAccountCache == nil {
602
+ return storage.Account {}, retry .NewError (false , fmt .Errorf ("storageAccountCache is nil" ))
603
+ }
604
+
605
+ // search in cache first
606
+ cache , err := az .storageAccountCache .Get (account , cache .CacheReadTypeDefault )
607
+ if err != nil {
608
+ return storage.Account {}, retry .NewError (false , err )
609
+ }
610
+ var result storage.Account
611
+ if cache != nil {
612
+ result = cache .(storage.Account )
613
+ klog .V (2 ).Infof ("Get storage account(%s) from cache" , account )
614
+ } else {
615
+ var rerr * retry.Error
616
+ result , rerr = az .StorageAccountClient .GetProperties (ctx , subsID , resourceGroup , account )
617
+ if rerr != nil {
618
+ return storage.Account {}, rerr
619
+ }
620
+ az .storageAccountCache .Set (account , result )
593
621
}
594
- result , rerr := az .StorageAccountClient .GetProperties (ctx , subsID , resourceGroup , account )
622
+
623
+ return result , nil
624
+ }
625
+
626
+ // AddStorageAccountTags add tags to storage account
627
+ func (az * Cloud ) AddStorageAccountTags (ctx context.Context , subsID , resourceGroup , account string , tags map [string ]* string ) * retry.Error {
628
+ result , rerr := az .getStorageAccountWithCache (ctx , subsID , resourceGroup , account )
595
629
if rerr != nil {
596
630
return rerr
597
631
}
@@ -606,16 +640,18 @@ func (az *Cloud) AddStorageAccountTags(ctx context.Context, subsID, resourceGrou
606
640
newTags [k ] = v
607
641
}
608
642
609
- updateParams := storage.AccountUpdateParameters {Tags : newTags }
610
- return az .StorageAccountClient .Update (ctx , subsID , resourceGroup , account , updateParams )
643
+ if len (newTags ) > len (result .Tags ) {
644
+ // only update when newTags is different from old tags
645
+ _ = az .storageAccountCache .Delete (account ) // clean cache
646
+ updateParams := storage.AccountUpdateParameters {Tags : newTags }
647
+ return az .StorageAccountClient .Update (ctx , subsID , resourceGroup , account , updateParams )
648
+ }
649
+ return nil
611
650
}
612
651
613
652
// RemoveStorageAccountTag remove tag from storage account
614
653
func (az * Cloud ) RemoveStorageAccountTag (ctx context.Context , subsID , resourceGroup , account , key string ) * retry.Error {
615
- if az .StorageAccountClient == nil {
616
- return retry .NewError (false , fmt .Errorf ("StorageAccountClient is nil" ))
617
- }
618
- result , rerr := az .StorageAccountClient .GetProperties (ctx , subsID , resourceGroup , account )
654
+ result , rerr := az .getStorageAccountWithCache (ctx , subsID , resourceGroup , account )
619
655
if rerr != nil {
620
656
return rerr
621
657
}
@@ -627,6 +663,8 @@ func (az *Cloud) RemoveStorageAccountTag(ctx context.Context, subsID, resourceGr
627
663
originalLen := len (result .Tags )
628
664
delete (result .Tags , key )
629
665
if originalLen != len (result .Tags ) {
666
+ // only update when newTags is different from old tags
667
+ _ = az .storageAccountCache .Delete (account ) // clean cache
630
668
updateParams := storage.AccountUpdateParameters {Tags : result .Tags }
631
669
return az .StorageAccountClient .Update (ctx , subsID , resourceGroup , account , updateParams )
632
670
}
0 commit comments