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