Skip to content

Commit dccceff

Browse files
author
Jan Roehrich
committed
fix modulitos' findings (part 1)
1 parent e278706 commit dccceff

File tree

5 files changed

+57
-58
lines changed

5 files changed

+57
-58
lines changed

pkg/cache/cache.go

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type Response struct {
5252
type ServiceAccountCache interface {
5353
Start(stop chan struct{})
5454
Get(name, namespace string) Response
55-
GetOrNotify(name, namespace string, handler chan any) Response
55+
GetOrNotify(name, namespace string, handler chan struct{}) Response
5656
GetCommonConfigurations(name, namespace string) (useRegionalSTS bool, tokenExpiration int64)
5757
// ToJSON returns cache contents as JSON string
5858
ToJSON() string
@@ -70,7 +70,7 @@ type serviceAccountCache struct {
7070
composeRoleArn ComposeRoleArn
7171
defaultTokenExpiration int64
7272
webhookUsage prometheus.Gauge
73-
notificationHandlers map[string]chan any // type of channel doesn't matter. It's just for being notified
73+
notificationHandlers map[string]chan struct{}
7474
}
7575

7676
type ComposeRoleArn struct {
@@ -106,32 +106,32 @@ func (c *serviceAccountCache) Get(name, namespace string) Response {
106106
// It will first look at the set of ServiceAccounts configured using annotations. If none is found, it will register
107107
// handler to be notified as soon as a ServiceAccount with given key is populated to the cache. Afterwards it will check
108108
// for a ServiceAccount configured through the pod-identity-webhook ConfigMap.
109-
func (c *serviceAccountCache) GetOrNotify(name, namespace string, handler chan any) Response {
109+
func (c *serviceAccountCache) GetOrNotify(name, namespace string, handler chan struct{}) Response {
110110
result := Response{
111111
TokenExpiration: pkg.DefaultTokenExpiration,
112112
}
113113
klog.V(5).Infof("Fetching sa %s/%s from cache", namespace, name)
114114
{
115-
resp := c.getSAorNotify(name, namespace, handler)
116-
if resp != nil {
115+
entry := c.getSAorNotify(name, namespace, handler)
116+
if entry != nil {
117117
result.FoundInSACache = true
118118
}
119-
if resp != nil && resp.RoleARN != "" {
120-
result.RoleARN = resp.RoleARN
121-
result.Audience = resp.Audience
122-
result.UseRegionalSTS = resp.UseRegionalSTS
123-
result.TokenExpiration = resp.TokenExpiration
119+
if entry != nil && entry.RoleARN != "" {
120+
result.RoleARN = entry.RoleARN
121+
result.Audience = entry.Audience
122+
result.UseRegionalSTS = entry.UseRegionalSTS
123+
result.TokenExpiration = entry.TokenExpiration
124124
return result
125125
}
126126
}
127127
{
128-
resp := c.getCM(name, namespace)
129-
if resp != nil {
128+
entry := c.getCM(name, namespace)
129+
if entry != nil {
130130
result.FoundInCMCache = true
131-
result.RoleARN = resp.RoleARN
132-
result.Audience = resp.Audience
133-
result.UseRegionalSTS = resp.UseRegionalSTS
134-
result.TokenExpiration = resp.TokenExpiration
131+
result.RoleARN = entry.RoleARN
132+
result.Audience = entry.Audience
133+
result.UseRegionalSTS = entry.UseRegionalSTS
134+
result.TokenExpiration = entry.TokenExpiration
135135
return result
136136
}
137137
}
@@ -143,34 +143,34 @@ func (c *serviceAccountCache) GetOrNotify(name, namespace string, handler chan a
143143
// The config file for the container credentials does not contain "TokenExpiration" or "UseRegionalSTS". For backward compatibility,
144144
// Use these fields if they are set in the sa annotations or config map.
145145
func (c *serviceAccountCache) GetCommonConfigurations(name, namespace string) (useRegionalSTS bool, tokenExpiration int64) {
146-
if resp := c.getSAorNotify(name, namespace, nil); resp != nil {
147-
return resp.UseRegionalSTS, resp.TokenExpiration
148-
} else if resp := c.getCM(name, namespace); resp != nil {
149-
return resp.UseRegionalSTS, resp.TokenExpiration
146+
if entry := c.getSAorNotify(name, namespace, nil); entry != nil {
147+
return entry.UseRegionalSTS, entry.TokenExpiration
148+
} else if entry := c.getCM(name, namespace); entry != nil {
149+
return entry.UseRegionalSTS, entry.TokenExpiration
150150
}
151151
return false, pkg.DefaultTokenExpiration
152152
}
153153

154-
func (c *serviceAccountCache) getSAorNotify(name, namespace string, handler chan any) *Entry {
154+
func (c *serviceAccountCache) getSAorNotify(name, namespace string, handler chan struct{}) *Entry {
155155
c.mu.RLock()
156156
defer c.mu.RUnlock()
157-
resp, ok := c.saCache[namespace+"/"+name]
157+
entry, ok := c.saCache[namespace+"/"+name]
158158
if !ok && handler != nil {
159159
klog.V(5).Infof("Service Account %s/%s not found in cache, adding notification handler", namespace, name)
160160
c.notificationHandlers[namespace+"/"+name] = handler
161161
return nil
162162
}
163-
return resp
163+
return entry
164164
}
165165

166166
func (c *serviceAccountCache) getCM(name, namespace string) *Entry {
167167
c.mu.RLock()
168168
defer c.mu.RUnlock()
169-
resp, ok := c.cmCache[namespace+"/"+name]
169+
entry, ok := c.cmCache[namespace+"/"+name]
170170
if !ok {
171171
return nil
172172
}
173-
return resp
173+
return entry
174174
}
175175

176176
func (c *serviceAccountCache) popSA(name, namespace string) {
@@ -200,7 +200,7 @@ func (c *serviceAccountCache) ToJSON() string {
200200
}
201201

202202
func (c *serviceAccountCache) addSA(sa *v1.ServiceAccount) {
203-
resp := &Entry{}
203+
entry := &Entry{}
204204

205205
arn, ok := sa.Annotations[c.annotationPrefix+"/"+pkg.RoleARNAnnotation]
206206
if ok {
@@ -214,57 +214,57 @@ func (c *serviceAccountCache) addSA(sa *v1.ServiceAccount) {
214214
} else if !matched {
215215
klog.Warningf("arn is invalid: %s", arn)
216216
}
217-
resp.RoleARN = arn
217+
entry.RoleARN = arn
218218
}
219219

220-
resp.Audience = c.defaultAudience
220+
entry.Audience = c.defaultAudience
221221
if audience, ok := sa.Annotations[c.annotationPrefix+"/"+pkg.AudienceAnnotation]; ok {
222-
resp.Audience = audience
222+
entry.Audience = audience
223223
}
224224

225-
resp.UseRegionalSTS = c.defaultRegionalSTS
225+
entry.UseRegionalSTS = c.defaultRegionalSTS
226226
if useRegionalStr, ok := sa.Annotations[c.annotationPrefix+"/"+pkg.UseRegionalSTSAnnotation]; ok {
227227
useRegional, err := strconv.ParseBool(useRegionalStr)
228228
if err != nil {
229229
klog.V(4).Infof("Ignoring service account %s/%s invalid value for disable-regional-sts annotation", sa.Namespace, sa.Name)
230230
} else {
231-
resp.UseRegionalSTS = useRegional
231+
entry.UseRegionalSTS = useRegional
232232
}
233233
}
234234

235-
resp.TokenExpiration = c.defaultTokenExpiration
235+
entry.TokenExpiration = c.defaultTokenExpiration
236236
if tokenExpirationStr, ok := sa.Annotations[c.annotationPrefix+"/"+pkg.TokenExpirationAnnotation]; ok {
237237
if tokenExpiration, err := strconv.ParseInt(tokenExpirationStr, 10, 64); err != nil {
238-
klog.V(4).Infof("Found invalid value for token expiration, using %d seconds as default: %v", resp.TokenExpiration, err)
238+
klog.V(4).Infof("Found invalid value for token expiration, using %d seconds as default: %v", entry.TokenExpiration, err)
239239
} else {
240-
resp.TokenExpiration = pkg.ValidateMinTokenExpiration(tokenExpiration)
240+
entry.TokenExpiration = pkg.ValidateMinTokenExpiration(tokenExpiration)
241241
}
242242
}
243243
c.webhookUsage.Set(1)
244244

245-
c.setSA(sa.Name, sa.Namespace, resp)
245+
c.setSA(sa.Name, sa.Namespace, entry)
246246
}
247247

248-
func (c *serviceAccountCache) setSA(name, namespace string, resp *Entry) {
248+
func (c *serviceAccountCache) setSA(name, namespace string, entry *Entry) {
249249
c.mu.Lock()
250250
defer c.mu.Unlock()
251251

252252
key := namespace + "/" + name
253-
klog.V(5).Infof("Adding SA %q to SA cache: %+v", key, resp)
254-
c.saCache[namespace+"/"+name] = resp
253+
klog.V(5).Infof("Adding SA %q to SA cache: %+v", key, entry)
254+
c.saCache[key] = entry
255255

256256
if handler, found := c.notificationHandlers[key]; found {
257257
klog.V(5).Infof("Notifying handler for %q", key)
258-
handler <- 1
258+
handler <- struct{}{}
259259
delete(c.notificationHandlers, key)
260260
}
261261
}
262262

263-
func (c *serviceAccountCache) setCM(name, namespace string, resp *Entry) {
263+
func (c *serviceAccountCache) setCM(name, namespace string, entry *Entry) {
264264
c.mu.Lock()
265265
defer c.mu.Unlock()
266-
klog.V(5).Infof("Adding SA %s/%s to CM cache: %+v", namespace, name, resp)
267-
c.cmCache[namespace+"/"+name] = resp
266+
klog.V(5).Infof("Adding SA %s/%s to CM cache: %+v", namespace, name, entry)
267+
c.cmCache[namespace+"/"+name] = entry
268268
}
269269

270270
func New(defaultAudience, prefix string, defaultRegionalSTS bool, defaultTokenExpiration int64, saInformer coreinformers.ServiceAccountInformer, cmInformer coreinformers.ConfigMapInformer, composeRoleArn ComposeRoleArn) ServiceAccountCache {
@@ -286,7 +286,7 @@ func New(defaultAudience, prefix string, defaultRegionalSTS bool, defaultTokenEx
286286
defaultTokenExpiration: defaultTokenExpiration,
287287
hasSynced: hasSynced,
288288
webhookUsage: webhookUsage,
289-
notificationHandlers: map[string]chan any{},
289+
notificationHandlers: map[string]chan struct{}{},
290290
}
291291

292292
saInformer.Informer().AddEventHandler(
@@ -348,12 +348,12 @@ func (c *serviceAccountCache) populateCacheFromCM(oldCM, newCM *v1.ConfigMap) er
348348
if err != nil {
349349
return fmt.Errorf("failed to unmarshal new config %q: %v", newConfig, err)
350350
}
351-
for key, resp := range sas {
351+
for key, entry := range sas {
352352
parts := strings.Split(key, "/")
353-
if resp.TokenExpiration == 0 {
354-
resp.TokenExpiration = c.defaultTokenExpiration
353+
if entry.TokenExpiration == 0 {
354+
entry.TokenExpiration = c.defaultTokenExpiration
355355
}
356-
c.setCM(parts[1], parts[0], resp)
356+
c.setCM(parts[1], parts[0], entry)
357357
}
358358

359359
if oldCM != nil {

pkg/cache/cache_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ func TestNonRegionalSTS(t *testing.T) {
160160
t.Fatalf("cache never called addSA: %v", err)
161161
}
162162

163-
//gotRoleArn, gotAudience, useRegionalSTS, gotTokenExpiration, found := cache.Get("default", "default")
164163
resp := cache.Get("default", "default")
165164
assert.True(t, resp.FoundInSACache, "Expected cache entry to be found")
166165
if resp.RoleARN != roleArn {

pkg/cache/fake.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (f *FakeServiceAccountCache) Get(name, namespace string) Response {
6161
}
6262

6363
// GetOrNotify gets a service account from the cache
64-
func (f *FakeServiceAccountCache) GetOrNotify(name, namespace string, handler chan any) Response {
64+
func (f *FakeServiceAccountCache) GetOrNotify(name, namespace string, handler chan struct{}) Response {
6565
f.mu.RLock()
6666
defer f.mu.RUnlock()
6767
resp, ok := f.cache[namespace+"/"+name]

pkg/handler/handler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,24 +433,24 @@ func (m *Modifier) buildPodPatchConfig(pod *corev1.Pod) *podPatchConfig {
433433
}
434434

435435
// Use the STS WebIdentity method if set
436-
handler := make(chan any, 1)
436+
handler := make(chan struct{}, 1)
437437
response := m.Cache.GetOrNotify(pod.Spec.ServiceAccountName, pod.Namespace, handler)
438438
key := pod.Namespace + "/" + pod.Spec.ServiceAccountName
439439
if !response.FoundInSACache && !response.FoundInCMCache && m.saLookupGraceTime > 0 {
440-
klog.Warningf("Service account %q not found in the cache. Waiting up to %s to be notified", key, m.saLookupGraceTime)
440+
klog.Warningf("Service account %s not found in the cache. Waiting up to %s to be notified", key, m.saLookupGraceTime)
441441
select {
442442
case <-handler:
443443
response = m.Cache.Get(pod.Spec.ServiceAccountName, pod.Namespace)
444444
if !response.FoundInSACache && !response.FoundInCMCache {
445-
klog.Warningf("Service account %q not found in the cache after being notified. Not mutating.", key)
445+
klog.Warningf("Service account %s not found in the cache after being notified. Not mutating.", key)
446446
return nil
447447
}
448448
case <-time.After(m.saLookupGraceTime):
449-
klog.Warningf("Service account %q not found in the cache after %s. Not mutating.", key, m.saLookupGraceTime)
449+
klog.Warningf("Service account %s not found in the cache after %s. Not mutating.", key, m.saLookupGraceTime)
450450
return nil
451451
}
452452
}
453-
klog.V(5).Infof("Value of roleArn after after cache retrieval for service account %q: %s", key, response.RoleARN)
453+
klog.V(5).Infof("Value of roleArn after after cache retrieval for service account %s: %s", key, response.RoleARN)
454454
if response.RoleARN != "" {
455455
tokenExpiration, containersToSkip := m.parsePodAnnotations(pod, response.TokenExpiration)
456456

pkg/handler/handler_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,22 +83,22 @@ func TestMutatePod(t *testing.T) {
8383
want, _ := json.MarshalIndent(c.response, "", " ")
8484
t.Errorf("Unexpected response. Got \n%s\n wanted \n%s", string(got), string(want))
8585
}
86+
var expectedPatchOps, actualPatchOps []byte
8687
if len(response.Patch) > 0 {
8788
patchOps := make([]patchOperation, 0)
8889
if err := json.Unmarshal(response.Patch, &patchOps); err != nil {
8990
t.Errorf("Failed to unmarshal patch: %v", err)
9091
}
91-
indentedPatchOps, _ := json.MarshalIndent(patchOps, "", " ")
92-
t.Logf("got patch operations: %s", string(indentedPatchOps))
92+
actualPatchOps, _ = json.MarshalIndent(patchOps, "", " ")
9393
}
9494
if len(c.response.Patch) > 0 {
9595
patchOps := make([]patchOperation, 0)
9696
if err := json.Unmarshal(c.response.Patch, &patchOps); err != nil {
9797
t.Errorf("Failed to unmarshal patch: %v", err)
9898
}
99-
indentedPatchOps, _ := json.MarshalIndent(patchOps, "", " ")
100-
t.Logf("wanted patch operations: %s", string(indentedPatchOps))
99+
expectedPatchOps, _ = json.MarshalIndent(patchOps, "", " ")
101100
}
101+
assert.Equal(t, string(expectedPatchOps), string(actualPatchOps))
102102
})
103103
}
104104
}

0 commit comments

Comments
 (0)