Skip to content

Commit 745d075

Browse files
committed
Load more data in the team ID configMap
1 parent b0b0346 commit 745d075

File tree

5 files changed

+67
-33
lines changed

5 files changed

+67
-33
lines changed

pkg/runtime/adoption_reconciler.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (r *adoptionReconciler) reconcile(ctx context.Context, req ctrlrt.Request)
128128
if teamID != "" {
129129
CARMLookupKey = string(teamID)
130130
needCARMLookup = true
131-
cmName = cache.ACKRoleTeamMap
131+
cmName = cache.ACKTeamMap
132132
} else {
133133
CARMLookupKey = string(acctID)
134134
cmName = cache.ACKRoleAccountMap
@@ -530,12 +530,21 @@ func (r *adoptionReconciler) getRoleARN(
530530
return "", fmt.Errorf("unable to retrieve role ARN for annotation %q: %v", key, err)
531531
}
532532
return ackv1alpha1.AWSResourceName(roleARN), nil
533-
} else if cmName == cache.ACKRoleTeamMap {
534-
roleARN, err := r.cache.Teams.GetRoleARN(key)
533+
} else if cmName == cache.ACKTeamMap {
534+
value, err := r.cache.Teams.GetConfigMapValue(key)
535535
if err != nil {
536536
return "", fmt.Errorf("unable to retrieve role ARN for team ID %s: %v", key, err)
537537
}
538-
return ackv1alpha1.AWSResourceName(roleARN), nil
538+
if valueMap, ok := value.(map[string]string); ok {
539+
if roleARN, ok := valueMap[cache.ACKRoleTeamKey]; ok {
540+
return ackv1alpha1.AWSResourceName(roleARN), nil
541+
}
542+
return "", fmt.Errorf("%q not set for team-id %q in configMap %q",
543+
cache.ACKRoleTeamKey, key, cache.ACKTeamMap)
544+
} else {
545+
return "", fmt.Errorf("team-id %q in configMap %q is not map[stirng]string",
546+
key, cache.ACKTeamMap)
547+
}
539548
}
540549
return "", fmt.Errorf("unexpected CARM name %q", cmName)
541550
}

pkg/runtime/cache/account.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ const (
4444
// all the AWS Account IDs associated with their AWS Role ARNs.
4545
ACKRoleAccountMap CARMName = "ack-role-account-map"
4646

47-
// ACKRoleTeamMap is the name of the configmap map object storing
48-
// all the AWS Team IDs associated with their AWS Role ARNs.
49-
ACKRoleTeamMap CARMName = "ack-role-team-map"
47+
// ACKTeamMap is the name of the configmap map object storing
48+
// all the AWS Team IDs associated with their configs.
49+
ACKTeamMap CARMName = "ack-team-map"
50+
51+
// ACKRoleTeamKey stores the role ARN key to the ACKTeamMap
52+
ACKRoleTeamKey = "roleArn"
5053
)
5154

5255
// CARMCache is responsible for caching the CARM configmap
@@ -122,27 +125,40 @@ func (c *CARMCache) Run(clientSet kubernetes.Interface, stopCh <-chan struct{})
122125
go informer.Run(stopCh)
123126
}
124127

125-
// GetRoleARN queries the associated Role ARN
128+
// GetConfigMapValue queries the associated value
126129
// from the cached CARM configmap. It will return an error if the
127-
// configmap is not found, the key is not found or the role ARN
128-
// is empty.
130+
// configmap is not found, the key is not found.
129131
//
130132
// This function is thread safe.
131-
func (c *CARMCache) GetRoleARN(key string) (string, error) {
133+
func (c *CARMCache) GetConfigMapValue(key string) (any, error) {
132134
c.RLock()
133135
defer c.RUnlock()
134136

135137
if !c.configMapCreated {
136138
return "", ErrCARMConfigMapNotFound
137139
}
138-
roleARN, ok := c.roleARNs[key]
140+
value, ok := c.roleARNs[key]
139141
if !ok {
140142
return "", ErrKeyNotFound
141143
}
142-
if roleARN == "" {
143-
return "", ErrEmptyRoleARN
144+
return value, nil
145+
}
146+
147+
// GetRoleARN queries the role ARN
148+
// from the cached CARM configmap. It will return an error if the
149+
// configmap is not found, the key is not found or the value
150+
// is empty.
151+
//
152+
// This function is thread safe.
153+
func (c *CARMCache) GetRoleARN(key string) (string, error) {
154+
value, err := c.GetConfigMapValue(key)
155+
if err != nil {
156+
return "", err
157+
}
158+
if roleARN, ok := value.(string); ok {
159+
return roleARN, nil
144160
}
145-
return roleARN, nil
161+
return "", fmt.Errorf("unexpected type in comfig map key %q: %v", key, value)
146162
}
147163

148164
// updateRoleData updates the CARM map. This function is thread safe.

pkg/runtime/cache/account_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestAccountCache(t *testing.T) {
7171

7272
// Before creating the configmap, the accountCache should error for any
7373
// GetAccountRoleARN call.
74-
_, err := accountCache.GetRoleARN(testAccount1)
74+
_, err := accountCache.GetConfigMapValue(testAccount1)
7575
require.NotNil(t, err)
7676
require.Equal(t, err, ackrtcache.ErrCARMConfigMapNotFound)
7777

@@ -91,12 +91,12 @@ func TestAccountCache(t *testing.T) {
9191
time.Sleep(time.Second)
9292

9393
// Test with non existing account
94-
_, err = accountCache.GetRoleARN("random-account-not-exist")
94+
_, err = accountCache.GetConfigMapValue("random-account-not-exist")
9595
require.NotNil(t, err)
9696
require.Equal(t, err, ackrtcache.ErrCARMConfigMapNotFound)
9797

9898
// Test with existing account
99-
_, err = accountCache.GetRoleARN(testAccount1)
99+
_, err = accountCache.GetConfigMapValue(testAccount1)
100100
require.NotNil(t, err)
101101
require.Equal(t, err, ackrtcache.ErrCARMConfigMapNotFound)
102102

@@ -115,17 +115,17 @@ func TestAccountCache(t *testing.T) {
115115
time.Sleep(time.Second)
116116

117117
// Test with non existing account
118-
_, err = accountCache.GetRoleARN("random-account-not-exist")
118+
_, err = accountCache.GetConfigMapValue("random-account-not-exist")
119119
require.NotNil(t, err)
120120
require.Equal(t, err, ackrtcache.ErrKeyNotFound)
121121

122122
// Test with existing account - but role ARN is empty
123-
_, err = accountCache.GetRoleARN(testAccount3)
123+
_, err = accountCache.GetConfigMapValue(testAccount3)
124124
require.NotNil(t, err)
125125
require.Equal(t, err, ackrtcache.ErrEmptyRoleARN)
126126

127127
// Test with existing account
128-
roleARN, err := accountCache.GetRoleARN(testAccount1)
128+
roleARN, err := accountCache.GetConfigMapValue(testAccount1)
129129
require.Nil(t, err)
130130
require.Equal(t, roleARN, testAccountARN1)
131131

@@ -145,21 +145,21 @@ func TestAccountCache(t *testing.T) {
145145
time.Sleep(time.Second)
146146

147147
// Test with non existing account
148-
_, err = accountCache.GetRoleARN("random-account-not-exist")
148+
_, err = accountCache.GetConfigMapValue("random-account-not-exist")
149149
require.NotNil(t, err)
150150
require.Equal(t, err, ackrtcache.ErrKeyNotFound)
151151

152152
// Test that account was removed
153-
_, err = accountCache.GetRoleARN(testAccount3)
153+
_, err = accountCache.GetConfigMapValue(testAccount3)
154154
require.NotNil(t, err)
155155
require.Equal(t, err, ackrtcache.ErrKeyNotFound)
156156

157157
// Test with existing account
158-
roleARN, err = accountCache.GetRoleARN(testAccount1)
158+
roleARN, err = accountCache.GetConfigMapValue(testAccount1)
159159
require.Nil(t, err)
160160
require.Equal(t, roleARN, testAccountARN1)
161161

162-
roleARN, err = accountCache.GetRoleARN(testAccount2)
162+
roleARN, err = accountCache.GetConfigMapValue(testAccount2)
163163
require.Nil(t, err)
164164
require.Equal(t, roleARN, testAccountARN2)
165165

@@ -173,15 +173,15 @@ func TestAccountCache(t *testing.T) {
173173
time.Sleep(time.Second)
174174

175175
// Test that accounts ware removed
176-
_, err = accountCache.GetRoleARN(testAccount1)
176+
_, err = accountCache.GetConfigMapValue(testAccount1)
177177
require.NotNil(t, err)
178178
require.Equal(t, err, ackrtcache.ErrCARMConfigMapNotFound)
179179

180-
_, err = accountCache.GetRoleARN(testAccount2)
180+
_, err = accountCache.GetConfigMapValue(testAccount2)
181181
require.NotNil(t, err)
182182
require.Equal(t, err, ackrtcache.ErrCARMConfigMapNotFound)
183183

184-
_, err = accountCache.GetRoleARN(testAccount3)
184+
_, err = accountCache.GetConfigMapValue(testAccount3)
185185
require.NotNil(t, err)
186186
require.Equal(t, err, ackrtcache.ErrCARMConfigMapNotFound)
187187
}

pkg/runtime/cache/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type Caches struct {
8686
func New(log logr.Logger, config Config) Caches {
8787
return Caches{
8888
Accounts: NewCARMCache(ACKRoleAccountMap, log),
89-
Teams: NewCARMCache(ACKRoleTeamMap, log),
89+
Teams: NewCARMCache(ACKTeamMap, log),
9090
Namespaces: NewNamespaceCache(log, config.WatchScope, config.Ignored),
9191
}
9292
}

pkg/runtime/reconciler.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
196196
if teamID != "" {
197197
CARMLookupKey = string(teamID)
198198
needCARMLookup = true
199-
cmName = cache.ACKRoleTeamMap
199+
cmName = cache.ACKTeamMap
200200
} else {
201201
CARMLookupKey = string(acctID)
202202
cmName = cache.ACKRoleAccountMap
@@ -1092,12 +1092,21 @@ func (r *resourceReconciler) getRoleARN(
10921092
return "", fmt.Errorf("unable to retrieve role ARN for account %s: %v", key, err)
10931093
}
10941094
return ackv1alpha1.AWSResourceName(roleARN), nil
1095-
} else if cmName == cache.ACKRoleTeamMap {
1096-
roleARN, err := r.cache.Teams.GetRoleARN(key)
1095+
} else if cmName == cache.ACKTeamMap {
1096+
value, err := r.cache.Teams.GetConfigMapValue(key)
10971097
if err != nil {
10981098
return "", fmt.Errorf("unable to retrieve role ARN for team ID %s: %v", key, err)
10991099
}
1100-
return ackv1alpha1.AWSResourceName(roleARN), nil
1100+
if valueMap, ok := value.(map[string]string); ok {
1101+
if roleARN, ok := valueMap[cache.ACKRoleTeamKey]; ok {
1102+
return ackv1alpha1.AWSResourceName(roleARN), nil
1103+
}
1104+
return "", fmt.Errorf("%q not set for team-id %q in configMap %q",
1105+
cache.ACKRoleTeamKey, key, cache.ACKTeamMap)
1106+
} else {
1107+
return "", fmt.Errorf("team-id %q in configMap %q is not map[stirng]string",
1108+
key, cache.ACKTeamMap)
1109+
}
11011110
}
11021111
return "", fmt.Errorf("unexpected CARM name %q", cmName)
11031112
}

0 commit comments

Comments
 (0)