Skip to content

Commit 836af72

Browse files
committed
feat(azure): implement shared credential cache across azureclient instances
Instead of caching entire client instances, enable sharing the driver's existing azureCredentials sync.Map across all azureclient instances. Changes: - Add CredentialCache *sync.Map to azureclient.Options for external cache - Add getCredentialCache() method to use external cache when provided - Modify getCreds() to use shared cache instead of per-client cache - Pass driver's azureCredentials to all azureclient instances in newAzClient() This eliminates duplicate credential creation across multiple client instances while reusing the existing azureCredentials infrastructure. Much simpler than client instance caching and more efficient for credential reuse.
1 parent 7bea55c commit 836af72

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

pkg/storage/azure/azure.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ func (d *driver) newAzClient(cfg *Azure, environment autorestazure.Environment,
343343
SubscriptionID: cfg.SubscriptionID,
344344
TagSet: tagset,
345345
Policies: d.policies,
346+
CredentialCache: &d.azureCredentials, // Share the driver's credential cache
346347
})
347348
if err != nil {
348349
return nil, err

pkg/storage/azure/azureclient/azureclient.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type Options struct {
5151
TagSet map[string]*string
5252
Policies []policy.Policy
5353
Creds azcore.TokenCredential
54+
CredentialCache *sync.Map // Optional external credential cache to share across instances
5455
}
5556

5657
type PrivateEndpointCreateOptions struct {
@@ -97,6 +98,14 @@ func New(opts *Options) (*Client, error) {
9798
}, nil
9899
}
99100

101+
// getCredentialCache returns the appropriate credential cache to use - external if provided, otherwise internal
102+
func (c *Client) getCredentialCache() *sync.Map {
103+
if c.opts.CredentialCache != nil {
104+
return c.opts.CredentialCache
105+
}
106+
return &c.azureCredentials
107+
}
108+
100109
func (c *Client) getCreds(ctx context.Context) (azcore.TokenCredential, error) {
101110
if c.creds != nil {
102111
return c.creds, nil
@@ -109,9 +118,10 @@ func (c *Client) getCreds(ctx context.Context) (azcore.TokenCredential, error) {
109118
userAssignedIdentityCredentialsFilePath := os.Getenv("MANAGED_AZURE_HCP_CREDENTIALS_FILE_PATH")
110119
if userAssignedIdentityCredentialsFilePath != "" {
111120
var ok bool
121+
credCache := c.getCredentialCache()
112122

113123
// We need to only store the Azure credentials once and reuse them after that.
114-
storedCreds, found := c.azureCredentials.Load(azureCredentialsKey)
124+
storedCreds, found := credCache.Load(azureCredentialsKey)
115125
if !found {
116126
klog.V(2).Info("Using UserAssignedIdentityCredentials for Azure authentication for managed Azure HCP")
117127
clientOptions := azcore.ClientOptions{
@@ -121,7 +131,7 @@ func (c *Client) getCreds(ctx context.Context) (azcore.TokenCredential, error) {
121131
if err != nil {
122132
return nil, err
123133
}
124-
c.azureCredentials.Store(azureCredentialsKey, creds)
134+
credCache.Store(azureCredentialsKey, creds)
125135
} else {
126136
creds, ok = storedCreds.(azcore.TokenCredential)
127137
if !ok {

0 commit comments

Comments
 (0)