@@ -51,6 +51,7 @@ type Options struct {
51
51
TagSet map [string ]* string
52
52
Policies []policy.Policy
53
53
Creds azcore.TokenCredential
54
+ CredentialCache * sync.Map // Optional external credential cache to share across instances
54
55
}
55
56
56
57
type PrivateEndpointCreateOptions struct {
@@ -97,6 +98,13 @@ func New(opts *Options) (*Client, error) {
97
98
}, nil
98
99
}
99
100
101
+ func (c * Client ) getCredentialCache () * sync.Map {
102
+ if c .opts .CredentialCache != nil {
103
+ return c .opts .CredentialCache
104
+ }
105
+ return & c .azureCredentials
106
+ }
107
+
100
108
func (c * Client ) getCreds (ctx context.Context ) (azcore.TokenCredential , error ) {
101
109
if c .creds != nil {
102
110
return c .creds , nil
@@ -110,9 +118,14 @@ func (c *Client) getCreds(ctx context.Context) (azcore.TokenCredential, error) {
110
118
if userAssignedIdentityCredentialsFilePath != "" {
111
119
var ok bool
112
120
121
+ // Use shared credential cache if available
122
+ credCache := c .getCredentialCache ()
123
+ klog .V (2 ).Infof ("Using credential cache: %p" , credCache )
124
+
113
125
// We need to only store the Azure credentials once and reuse them after that.
114
- storedCreds , found := c . azureCredentials .Load (azureCredentialsKey )
126
+ storedCreds , found := credCache .Load (azureCredentialsKey )
115
127
if ! found {
128
+ klog .V (2 ).Infof ("Cache miss - creating new credentials" )
116
129
klog .V (2 ).Info ("Using UserAssignedIdentityCredentials for Azure authentication for managed Azure HCP" )
117
130
clientOptions := azcore.ClientOptions {
118
131
Cloud : c .clientOpts .Cloud ,
@@ -121,8 +134,10 @@ func (c *Client) getCreds(ctx context.Context) (azcore.TokenCredential, error) {
121
134
if err != nil {
122
135
return nil , err
123
136
}
124
- c .azureCredentials .Store (azureCredentialsKey , creds )
137
+ credCache .Store (azureCredentialsKey , creds )
138
+ klog .V (2 ).Infof ("Stored credentials in cache: %p" , creds )
125
139
} else {
140
+ klog .V (2 ).Infof ("Cache hit - reusing existing credentials" )
126
141
creds , ok = storedCreds .(azcore.TokenCredential )
127
142
if ! ok {
128
143
return nil , fmt .Errorf ("expected %T to be a TokenCredential" , storedCreds )
0 commit comments