@@ -86,7 +86,7 @@ func (p *contextProviderK8sSecrets) Run(ctx context.Context, comm corecomp.Conte
86
86
p .clientMx .Unlock ()
87
87
88
88
if ! p .config .DisableCache {
89
- go p .updateSecrets (ctx )
89
+ go p .updateSecrets (ctx , comm )
90
90
}
91
91
92
92
<- comm .Done ()
@@ -102,28 +102,39 @@ func getK8sClient(kubeconfig string, opt kubernetes.KubeClientOptions) (k8sclien
102
102
}
103
103
104
104
// Update the secrets in the cache every RefreshInterval
105
- func (p * contextProviderK8sSecrets ) updateSecrets (ctx context.Context ) {
105
+ func (p * contextProviderK8sSecrets ) updateSecrets (ctx context.Context , comm corecomp. ContextProviderComm ) {
106
106
timer := time .NewTimer (p .config .RefreshInterval )
107
107
for {
108
108
select {
109
109
case <- ctx .Done ():
110
110
return
111
111
case <- timer .C :
112
- p .updateCache ()
112
+ updatedCache := p .updateCache ()
113
+ if updatedCache {
114
+ p .logger .Info ("Secrets cache was updated, the agent will be notified." )
115
+ comm .Signal ()
116
+ }
113
117
timer .Reset (p .config .RefreshInterval )
114
118
}
115
119
}
116
120
}
117
121
118
122
// mergeWithCurrent merges the updated map with the cache map.
119
123
// This function needs to be called between the mutex lock for the map.
120
- func (p * contextProviderK8sSecrets ) mergeWithCurrent (updatedMap map [string ]* secretsData ) map [string ]* secretsData {
124
+ func (p * contextProviderK8sSecrets ) mergeWithCurrent (updatedMap map [string ]* secretsData ) ( map [string ]* secretsData , bool ) {
121
125
merged := make (map [string ]* secretsData )
126
+ updatedCache := false
122
127
123
128
for name , data := range p .secretsCache {
124
129
diff := time .Since (data .lastAccess )
125
130
if diff < p .config .TTLDelete {
126
131
merged [name ] = data
132
+ // Check if this key is part of the updatedMap. If it is not, we know the secrets cache was updated,
133
+ // and we need to signal that.
134
+ _ , ok := updatedMap [name ]
135
+ if ! ok {
136
+ updatedCache = true
137
+ }
127
138
}
128
139
}
129
140
@@ -132,14 +143,20 @@ func (p *contextProviderK8sSecrets) mergeWithCurrent(updatedMap map[string]*secr
132
143
// it could have been updated when trying to fetch the secret at the same time we are running update cache.
133
144
// In that case, we only update the value.
134
145
if _ , ok := merged [name ]; ok {
135
- merged [name ].value = data .value
146
+ if merged [name ].value != data .value {
147
+ merged [name ].value = data .value
148
+ updatedCache = true
149
+ }
136
150
}
137
151
}
138
152
139
- return merged
153
+ return merged , updatedCache
140
154
}
141
155
142
- func (p * contextProviderK8sSecrets ) updateCache () {
156
+ func (p * contextProviderK8sSecrets ) updateCache () bool {
157
+ // Keep track whether the cache had values changing, so we can notify the agent
158
+ updatedCache := false
159
+
143
160
// deleting entries does not free the memory, so we need to create a new map
144
161
// to place the secrets we want to keep
145
162
cacheTmp := make (map [string ]* secretsData )
@@ -152,6 +169,8 @@ func (p *contextProviderK8sSecrets) updateCache() {
152
169
}
153
170
p .secretsCacheMx .RUnlock ()
154
171
172
+ // The only way to update an entry in the cache is through the last access time (to delete the key)
173
+ // or if the value gets updated.
155
174
for name , data := range copyMap {
156
175
diff := time .Since (data .lastAccess )
157
176
if diff < p .config .TTLDelete {
@@ -162,17 +181,24 @@ func (p *contextProviderK8sSecrets) updateCache() {
162
181
lastAccess : data .lastAccess ,
163
182
}
164
183
cacheTmp [name ] = newData
184
+ if value != data .value {
185
+ updatedCache = true
186
+ }
165
187
}
166
-
188
+ } else {
189
+ updatedCache = true
167
190
}
168
191
}
169
192
170
193
// While the cache was updated, it is possible that some secret was added through another go routine.
171
194
// We need to merge the updated map with the current cache map to catch the new entries and avoid
172
195
// loss of data.
196
+ var updated bool
173
197
p .secretsCacheMx .Lock ()
174
- p .secretsCache = p .mergeWithCurrent (cacheTmp )
198
+ p .secretsCache , updated = p .mergeWithCurrent (cacheTmp )
175
199
p .secretsCacheMx .Unlock ()
200
+
201
+ return updatedCache || updated
176
202
}
177
203
178
204
func (p * contextProviderK8sSecrets ) getFromCache (key string ) (string , bool ) {
0 commit comments