Skip to content

Commit ccbb720

Browse files
committed
pr feedback updates
1 parent 0178602 commit ccbb720

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

pkg/cache/cache.go

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,14 @@ func New(defaultAudience,
304304

305305
go func() {
306306
for req := range saFetchRequests {
307-
sa, err := fetchFromAPI(SAGetter, req)
308-
if err != nil {
309-
klog.Errorf("fetching SA: %s, but got error from API: %v", req.CacheKey(), err)
310-
continue
311-
}
312-
c.addSA(sa)
307+
go func() {
308+
sa, err := fetchFromAPI(SAGetter, req)
309+
if err != nil {
310+
klog.Errorf("fetching SA: %s, but got error from API: %v", req.CacheKey(), err)
311+
return
312+
}
313+
c.addSA(sa)
314+
}()
313315
}
314316
}()
315317

@@ -367,22 +369,8 @@ func fetchFromAPI(getter corev1.ServiceAccountsGetter, req *Request) (*v1.Servic
367369
defer cancel()
368370

369371
klog.V(5).Infof("fetching SA: %s", req.CacheKey())
370-
saList, err := getter.ServiceAccounts(req.Namespace).List(
371-
ctx,
372-
metav1.ListOptions{},
373-
)
374-
if err != nil {
375-
return nil, err
376-
}
377-
378-
// Find the ServiceAccount
379-
for _, sa := range saList.Items {
380-
if sa.Name == req.Name {
381-
return &sa, nil
382372

383-
}
384-
}
385-
return nil, fmt.Errorf("no SA found in namespace: %s", req.CacheKey())
373+
return getter.ServiceAccounts(req.Namespace).Get(ctx, req.Name, metav1.GetOptions{})
386374
}
387375

388376
func (c *serviceAccountCache) populateCacheFromCM(oldCM, newCM *v1.ConfigMap) error {

pkg/cache/cache_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func TestNotification(t *testing.T) {
157157
func TestFetchFromAPIServer(t *testing.T) {
158158
testSA := &v1.ServiceAccount{
159159
ObjectMeta: metav1.ObjectMeta{
160-
Name: "default",
160+
Name: "my-sa",
161161
Namespace: "default",
162162
Annotations: map[string]string{
163163
"eks.amazonaws.com/role-arn": "arn:aws:iam::111122223333:role/s3-reader",
@@ -196,15 +196,15 @@ func TestFetchFromAPIServer(t *testing.T) {
196196
t.Fatalf("informer never called client: %v", err)
197197
}
198198

199-
resp := cache.Get(Request{Name: "default", Namespace: "default", RequestNotification: true})
199+
resp := cache.Get(Request{Name: "my-sa", Namespace: "default", RequestNotification: true})
200200
assert.False(t, resp.FoundInCache, "Expected cache entry to not be found")
201201

202202
// wait for the notification while we fetch the SA from the API server:
203203
select {
204204
case <-resp.Notifier:
205205
// expected
206206
// test that the requested SA is now in the cache
207-
resp := cache.Get(Request{Name: "default", Namespace: "default", RequestNotification: false})
207+
resp := cache.Get(Request{Name: "my-sa", Namespace: "default", RequestNotification: false})
208208
assert.True(t, resp.FoundInCache, "Expected cache entry to be found in cache")
209209
case <-time.After(1 * time.Second):
210210
t.Fatal("timeout waiting for notification")

0 commit comments

Comments
 (0)