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