@@ -28,49 +28,54 @@ var (
2828 // ErrCARMConfigMapNotFound is an error that is returned when the CARM
2929 // configmap is not found.
3030 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
3232 // 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
3535 // 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" )
3737)
3838
3939const (
4040 // ACKRoleAccountMap is the name of the configmap map object storing
4141 // all the AWS Account IDs associated with their AWS Role ARNs.
4242 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"
4348)
4449
45- // AccountCache is responsible for caching the CARM configmap
50+ // CARMMap is responsible for caching the CARM configmap
4651// data. It is listening to all the events related to the CARM map and
4752// make the changes accordingly.
48- type AccountCache struct {
53+ type CARMMap struct {
4954 sync.RWMutex
5055 log logr.Logger
51- roleARNs map [string ]string
56+ data map [string ]string
5257 configMapCreated bool
5358 hasSynced func () bool
5459}
5560
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 ),
6166 configMapCreated : false ,
6267 }
6368}
6469
65- // resourceMatchACKRoleAccountConfigMap verifies if a resource is
70+ // resourceMatchCARMConfigMap verifies if a resource is
6671// 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 {
6873 object , ok := raw .(* corev1.ConfigMap )
69- return ok && object .ObjectMeta .Name == ACKRoleAccountMap
74+ return ok && object .ObjectMeta .Name == name
7075}
7176
7277// 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 {}) {
7479 c .log .V (1 ).Info ("Starting shared informer for accounts cache" , "targetConfigMap" , ACKRoleAccountMap )
7580 informer := informersv1 .NewConfigMapInformer (
7681 clientSet ,
@@ -80,33 +85,33 @@ func (c *AccountCache) Run(clientSet kubernetes.Interface, stopCh <-chan struct{
8085 )
8186 informer .AddEventHandler (k8scache.ResourceEventHandlerFuncs {
8287 AddFunc : func (obj interface {}) {
83- if resourceMatchACKRoleAccountsConfigMap (obj ) {
88+ if resourceMatchCARMConfigMap (obj , name ) {
8489 cm := obj .(* corev1.ConfigMap )
8590 object := cm .DeepCopy ()
8691 // To avoid multiple mutex locks, we are updating the cache
8792 // and the configmap existence flag in the same function.
8893 configMapCreated := true
89- c .updateAccountRoleData (configMapCreated , object .Data )
94+ c .updateData (configMapCreated , object .Data )
9095 c .log .V (1 ).Info ("created account config map" , "name" , cm .ObjectMeta .Name )
9196 }
9297 },
9398 UpdateFunc : func (orig , desired interface {}) {
94- if resourceMatchACKRoleAccountsConfigMap (desired ) {
99+ if resourceMatchCARMConfigMap (desired , name ) {
95100 cm := desired .(* corev1.ConfigMap )
96101 object := cm .DeepCopy ()
97102 //TODO(a-hilaly): compare data checksum before updating the cache
98- c .updateAccountRoleData (true , object .Data )
103+ c .updateData (true , object .Data )
99104 c .log .V (1 ).Info ("updated account config map" , "name" , cm .ObjectMeta .Name )
100105 }
101106 },
102107 DeleteFunc : func (obj interface {}) {
103- if resourceMatchACKRoleAccountsConfigMap (obj ) {
108+ if resourceMatchCARMConfigMap (obj , name ) {
104109 cm := obj .(* corev1.ConfigMap )
105110 newMap := make (map [string ]string )
106111 // To avoid multiple mutex locks, we are updating the cache
107112 // and the configmap existence flag in the same function.
108113 configMapCreated := false
109- c .updateAccountRoleData (configMapCreated , newMap )
114+ c .updateData (configMapCreated , newMap )
110115 c .log .V (1 ).Info ("deleted account config map" , "name" , cm .ObjectMeta .Name )
111116 }
112117 },
@@ -115,33 +120,33 @@ func (c *AccountCache) Run(clientSet kubernetes.Interface, stopCh <-chan struct{
115120 c .hasSynced = informer .HasSynced
116121}
117122
118- // GetAccountRoleARN queries the AWS accountID associated Role ARN
123+ // GetValue queries the value
119124// 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
121126// is empty.
122127//
123128// This function is thread safe.
124- func (c * AccountCache ) GetAccountRoleARN ( accountID string ) (string , error ) {
129+ func (c * CARMMap ) GetValue ( key string ) (string , error ) {
125130 c .RLock ()
126131 defer c .RUnlock ()
127132
128133 if ! c .configMapCreated {
129134 return "" , ErrCARMConfigMapNotFound
130135 }
131- roleARN , ok := c .roleARNs [ accountID ]
136+ roleARN , ok := c .data [ key ]
132137 if ! ok {
133- return "" , ErrAccountIDNotFound
138+ return "" , ErrKeyNotFound
134139 }
135140 if roleARN == "" {
136- return "" , ErrEmptyRoleARN
141+ return "" , ErrEmptyValue
137142 }
138143 return roleARN , nil
139144}
140145
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 ) {
143148 c .Lock ()
144149 defer c .Unlock ()
145- c .roleARNs = data
150+ c .data = data
146151 c .configMapCreated = exist
147152}
0 commit comments