@@ -15,6 +15,7 @@ package cache
15
15
16
16
import (
17
17
"errors"
18
+ "fmt"
18
19
"sync"
19
20
20
21
"github.com/go-logr/logr"
@@ -28,49 +29,57 @@ var (
28
29
// ErrCARMConfigMapNotFound is an error that is returned when the CARM
29
30
// configmap is not found.
30
31
ErrCARMConfigMapNotFound = errors .New ("CARM configmap not found" )
31
- // ErrAccountIDNotFound is an error that is returned when the account ID
32
+ // ErrKeyNotFound is an error that is returned when the key
32
33
// is not found in the CARM configmap.
33
- ErrAccountIDNotFound = errors .New ("account ID not found in CARM configmap" )
34
+ ErrKeyNotFound = errors .New ("key not found in CARM configmap" )
34
35
// ErrEmptyRoleARN is an error that is returned when the role ARN is empty
35
36
// in the CARM configmap.
36
37
ErrEmptyRoleARN = errors .New ("role ARN is empty in CARM configmap" )
37
38
)
38
39
40
+ type CARMName string
41
+
39
42
const (
40
43
// ACKRoleAccountMap is the name of the configmap map object storing
41
44
// all the AWS Account IDs associated with their AWS Role ARNs.
42
- ACKRoleAccountMap = "ack-role-account-map"
45
+ ACKRoleAccountMap CARMName = "ack-role-account-map"
46
+
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"
43
50
)
44
51
45
- // AccountCache is responsible for caching the CARM configmap
52
+ // CARMCache is responsible for caching the CARM configmap
46
53
// data. It is listening to all the events related to the CARM map and
47
54
// make the changes accordingly.
48
- type AccountCache struct {
55
+ type CARMCache struct {
49
56
sync.RWMutex
57
+ name CARMName
50
58
log logr.Logger
51
59
roleARNs map [string ]string
52
60
configMapCreated bool
53
61
}
54
62
55
- // NewAccountCache instanciate a new AccountCache.
56
- func NewAccountCache (log logr.Logger ) * AccountCache {
57
- return & AccountCache {
58
- log : log .WithName ("cache.account" ),
63
+ // NewCARMCache instanciate a new CARMCache.
64
+ func NewCARMCache (name CARMName , log logr.Logger ) * CARMCache {
65
+ return & CARMCache {
66
+ log : log .WithName (fmt .Sprintf ("cache.%s" , name )),
67
+ name : name ,
59
68
roleARNs : make (map [string ]string ),
60
69
configMapCreated : false ,
61
70
}
62
71
}
63
72
64
- // resourceMatchACKRoleAccountConfigMap verifies if a resource is
73
+ // resourceMatchACKRoleConfigMap verifies if a resource is
65
74
// the CARM configmap. It verifies the name, namespace and object type.
66
- func resourceMatchACKRoleAccountsConfigMap ( raw interface {}) bool {
75
+ func resourceMatchACKRoleConfigMap ( name CARMName , raw interface {}) bool {
67
76
object , ok := raw .(* corev1.ConfigMap )
68
- return ok && object .ObjectMeta .Name == ACKRoleAccountMap
77
+ return ok && object .ObjectMeta .Name == string ( name )
69
78
}
70
79
71
80
// Run instantiate a new SharedInformer for ConfigMaps and runs it to begin processing items.
72
- func (c * AccountCache ) Run (clientSet kubernetes.Interface , stopCh <- chan struct {}) {
73
- c .log .V (1 ).Info ("Starting shared informer for accounts cache" , "targetConfigMap" , ACKRoleAccountMap )
81
+ func (c * CARMCache ) Run (clientSet kubernetes.Interface , stopCh <- chan struct {}) {
82
+ c .log .V (1 ).Info ("Starting shared informer for CARM cache" , "targetConfigMap" , c . name )
74
83
informer := informersv1 .NewConfigMapInformer (
75
84
clientSet ,
76
85
ackSystemNamespace ,
@@ -79,65 +88,65 @@ func (c *AccountCache) Run(clientSet kubernetes.Interface, stopCh <-chan struct{
79
88
)
80
89
informer .AddEventHandler (k8scache.ResourceEventHandlerFuncs {
81
90
AddFunc : func (obj interface {}) {
82
- if resourceMatchACKRoleAccountsConfigMap ( obj ) {
91
+ if resourceMatchACKRoleConfigMap ( c . name , obj ) {
83
92
cm := obj .(* corev1.ConfigMap )
84
93
object := cm .DeepCopy ()
85
94
// To avoid multiple mutex locks, we are updating the cache
86
95
// and the configmap existence flag in the same function.
87
96
configMapCreated := true
88
- c .updateAccountRoleData (configMapCreated , object .Data )
89
- c .log .V (1 ).Info ("created account config map" , "name" , cm .ObjectMeta .Name )
97
+ c .updateRoleData (configMapCreated , object .Data )
98
+ c .log .V (1 ).Info ("created config map" , "name" , cm .ObjectMeta .Name )
90
99
}
91
100
},
92
101
UpdateFunc : func (orig , desired interface {}) {
93
- if resourceMatchACKRoleAccountsConfigMap ( desired ) {
102
+ if resourceMatchACKRoleConfigMap ( c . name , desired ) {
94
103
cm := desired .(* corev1.ConfigMap )
95
104
object := cm .DeepCopy ()
96
105
//TODO(a-hilaly): compare data checksum before updating the cache
97
- c .updateAccountRoleData (true , object .Data )
98
- c .log .V (1 ).Info ("updated account config map" , "name" , cm .ObjectMeta .Name )
106
+ c .updateRoleData (true , object .Data )
107
+ c .log .V (1 ).Info ("updated config map" , "name" , cm .ObjectMeta .Name )
99
108
}
100
109
},
101
110
DeleteFunc : func (obj interface {}) {
102
- if resourceMatchACKRoleAccountsConfigMap ( obj ) {
111
+ if resourceMatchACKRoleConfigMap ( c . name , obj ) {
103
112
cm := obj .(* corev1.ConfigMap )
104
113
newMap := make (map [string ]string )
105
114
// To avoid multiple mutex locks, we are updating the cache
106
115
// and the configmap existence flag in the same function.
107
116
configMapCreated := false
108
- c .updateAccountRoleData (configMapCreated , newMap )
109
- c .log .V (1 ).Info ("deleted account config map" , "name" , cm .ObjectMeta .Name )
117
+ c .updateRoleData (configMapCreated , newMap )
118
+ c .log .V (1 ).Info ("deleted config map" , "name" , cm .ObjectMeta .Name )
110
119
}
111
120
},
112
121
})
113
122
go informer .Run (stopCh )
114
123
}
115
124
116
- // GetAccountRoleARN queries the AWS accountID associated Role ARN
125
+ // GetRoleARN queries the associated Role ARN
117
126
// from the cached CARM configmap. It will return an error if the
118
- // configmap is not found, the accountID is not found or the role ARN
127
+ // configmap is not found, the key is not found or the role ARN
119
128
// is empty.
120
129
//
121
130
// This function is thread safe.
122
- func (c * AccountCache ) GetAccountRoleARN ( accountID string ) (string , error ) {
131
+ func (c * CARMCache ) GetRoleARN ( key string ) (string , error ) {
123
132
c .RLock ()
124
133
defer c .RUnlock ()
125
134
126
135
if ! c .configMapCreated {
127
136
return "" , ErrCARMConfigMapNotFound
128
137
}
129
- roleARN , ok := c .roleARNs [accountID ]
138
+ roleARN , ok := c .roleARNs [key ]
130
139
if ! ok {
131
- return "" , ErrAccountIDNotFound
140
+ return "" , ErrKeyNotFound
132
141
}
133
142
if roleARN == "" {
134
143
return "" , ErrEmptyRoleARN
135
144
}
136
145
return roleARN , nil
137
146
}
138
147
139
- // updateAccountRoleData updates the CARM map. This function is thread safe.
140
- func (c * AccountCache ) updateAccountRoleData (exist bool , data map [string ]string ) {
148
+ // updateRoleData updates the CARM map. This function is thread safe.
149
+ func (c * CARMCache ) updateRoleData (exist bool , data map [string ]string ) {
141
150
c .Lock ()
142
151
defer c .Unlock ()
143
152
c .roleARNs = data
0 commit comments