@@ -136,7 +136,7 @@ func (p *contextProviderK8SSecrets) Fetch(key string) (string, bool) {
136
136
return "" , false
137
137
}
138
138
if len (tokens ) != 4 {
139
- p .logger .Warn ( " Invalid secret key format: " , key , " . Secrets should be of the format kubernetes_secrets.namespace.secret_name.value" )
139
+ p .logger .Warnf ( ` Invalid secret key format: %q . Secrets should be of the format kubernetes_secrets.namespace.secret_name.value` , key )
140
140
return "" , false
141
141
}
142
142
@@ -151,7 +151,8 @@ func (p *contextProviderK8SSecrets) Fetch(key string) (string, bool) {
151
151
152
152
if p .config .DisableCache {
153
153
// cache disabled - fetch secret from the API
154
- return p .fetchFromAPI (ctx , secretName , secretNamespace , secretKey )
154
+ val , _ , ok := p .fetchFromAPI (ctx , secretName , secretNamespace , secretKey )
155
+ return val , ok
155
156
}
156
157
157
158
// cache enabled
@@ -162,7 +163,7 @@ func (p *contextProviderK8SSecrets) Fetch(key string) (string, bool) {
162
163
}
163
164
164
165
// cache miss - fetch secret from the API
165
- apiSecretValue , apiExists := p .fetchFromAPI (ctx , secretName , secretNamespace , secretKey )
166
+ apiSecretValue , apiSecretResourceVersion , apiExists := p .fetchFromAPI (ctx , secretName , secretNamespace , secretKey )
166
167
now := time .Now ()
167
168
sd = secret {
168
169
name : secretName ,
@@ -175,11 +176,13 @@ func (p *contextProviderK8SSecrets) Fetch(key string) (string, bool) {
175
176
p .store .AddConditionally (key , sd , true , func (existing secret , exists bool ) bool {
176
177
if ! exists {
177
178
// no existing secret in the cache thus add it
179
+ p .logger .Infof (`Fetch: %q inserted. Resource Version of secret: %q` , key , apiSecretResourceVersion )
178
180
return true
179
181
}
180
182
if existing .value != apiSecretValue && ! existing .apiFetchTime .After (now ) {
181
183
// there is an existing secret in the cache but its value has changed since the last time
182
184
// it was fetched from the API thus update it
185
+ p .logger .Infof (`Fetch: %q updated. Resource Version of secret: %q` , key , apiSecretResourceVersion )
183
186
return true
184
187
}
185
188
// there is an existing secret in the cache, and it points already to the latest value
@@ -199,10 +202,13 @@ func (p *contextProviderK8SSecrets) refreshCache(ctx context.Context, comm corec
199
202
case <- ctx .Done ():
200
203
return
201
204
case <- timer .C :
205
+ p .logger .Info ("Cache: refresh started" )
202
206
hasUpdate := p .updateSecrets (ctx )
203
207
if hasUpdate {
204
- p .logger .Info ("Secrets cache was updated, the agent will be notified. " )
208
+ p .logger .Info ("Cache: refresh ended with updates, agent will be notified" )
205
209
comm .Signal ()
210
+ } else {
211
+ p .logger .Info ("Cache: refresh ended without updates" )
206
212
}
207
213
timer .Reset (p .config .RefreshInterval )
208
214
}
@@ -220,11 +226,12 @@ func (p *contextProviderK8SSecrets) updateSecrets(ctx context.Context) bool {
220
226
sd , exists := p .store .Get (key , false )
221
227
if ! exists {
222
228
// this item has expired thus mark that the cache has updates and continue
229
+ p .logger .Infof (`Cache: %q expired` , key )
223
230
hasUpdates = true
224
231
continue
225
232
}
226
233
227
- apiSecretValue , apiExists := p .fetchFromAPI (ctx , sd .name , sd .namespace , sd .key )
234
+ apiSecretValue , apiResourceVersion , apiExists := p .fetchFromAPI (ctx , sd .name , sd .namespace , sd .key )
228
235
now := time .Now ()
229
236
sd = secret {
230
237
name : sd .name ,
@@ -247,6 +254,7 @@ func (p *contextProviderK8SSecrets) updateSecrets(ctx context.Context) bool {
247
254
// the secret value has changed and the above fetchFromAPI is more recent thus
248
255
// add it and mark that the cache has updates
249
256
hasUpdates = true
257
+ p .logger .Infof (`Cache: %q updated. Resource Version of secret: %q` , key , apiResourceVersion )
250
258
return true
251
259
}
252
260
// the secret value has not changed
@@ -258,30 +266,32 @@ func (p *contextProviderK8SSecrets) updateSecrets(ctx context.Context) bool {
258
266
}
259
267
260
268
// fetchFromAPI fetches the secret value from the API
261
- func (p * contextProviderK8SSecrets ) fetchFromAPI (ctx context.Context , secretName string , secretNamespace string , secretKey string ) (string , bool ) {
269
+ func (p * contextProviderK8SSecrets ) fetchFromAPI (ctx context.Context , secretName string , secretNamespace string , secretKey string ) (string , string , bool ) {
262
270
ctx , cancel := context .WithTimeout (ctx , p .config .RequestTimeout )
263
271
defer cancel ()
264
272
265
273
p .clientMtx .RLock ()
266
274
if p .client == nil {
267
275
// k8s client is nil most probably due to an error at p.Run
268
276
p .clientMtx .RUnlock ()
269
- return "" , false
277
+ p .logger .Warnf (`Could not retrieve secret %q at namespace %q because k8s client is nil` , secretName , secretNamespace )
278
+ return "" , "" , false
270
279
}
271
280
c := p .client
272
281
p .clientMtx .RUnlock ()
273
282
274
283
si := c .CoreV1 ().Secrets (secretNamespace )
275
284
secret , err := si .Get (ctx , secretName , metav1.GetOptions {})
276
285
if err != nil {
277
- p .logger .Warn ( " Could not retrieve secret " , secretName , " at namespace " , secretNamespace , ": " , err .Error ())
278
- return "" , false
286
+ p .logger .Warnf ( ` Could not retrieve secret %q at namespace %q: %s` , secretName , secretNamespace , err .Error ())
287
+ return "" , "" , false
279
288
}
280
289
281
- if _ , ok := secret .Data [secretKey ]; ! ok {
282
- p .logger .Warn ("Could not retrieve value of key " , secretKey , " for secret " , secretName , " at namespace " , secretNamespace )
283
- return "" , false
290
+ secretData , ok := secret .Data [secretKey ]
291
+ if ! ok {
292
+ p .logger .Warnf (`Could not retrieve value of key %q for secret %q at namespace %q because it does not exist` , secretKey , secretName , secretNamespace )
293
+ return "" , "" , false
284
294
}
285
295
286
- return string (secret .Data [ secretKey ] ), true
296
+ return string (secretData ), secret .GetResourceVersion ( ), true
287
297
}
0 commit comments