@@ -33,6 +33,7 @@ import (
33
33
ackv1alpha1 "github.com/aws-controllers-k8s/runtime/apis/core/v1alpha1"
34
34
ackcfg "github.com/aws-controllers-k8s/runtime/pkg/config"
35
35
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
36
+ "github.com/aws-controllers-k8s/runtime/pkg/featuregate"
36
37
ackmetrics "github.com/aws-controllers-k8s/runtime/pkg/metrics"
37
38
"github.com/aws-controllers-k8s/runtime/pkg/requeue"
38
39
ackrtcache "github.com/aws-controllers-k8s/runtime/pkg/runtime/cache"
@@ -109,10 +110,6 @@ func (r *adoptionReconciler) reconcile(ctx context.Context, req ctrlrt.Request)
109
110
return ackerr .NotAdoptable
110
111
}
111
112
112
- // If a user specified a namespace with role ARN annotation,
113
- // we need to get the role and set the accout ID to that role.
114
- teamID := r .getTeamID (res )
115
-
116
113
// If a user has specified a namespace that is annotated with the
117
114
// an owner account ID, we need an appropriate role ARN to assume
118
115
// in order to perform the reconciliation. The roles ARN are typically
@@ -123,31 +120,39 @@ func (r *adoptionReconciler) reconcile(ctx context.Context, req ctrlrt.Request)
123
120
acctID , needCARMLookup := r .getOwnerAccountID (res )
124
121
125
122
var roleARN ackv1alpha1.AWSResourceName
126
- if teamID != "" {
127
- roleARN , err = r .getTeamRoleARN (teamID )
128
- if err != nil {
129
- ackrtlog .InfoAdoptedResource (r .log , res , fmt .Sprintf ("Unable to start adoption reconcilliation %s: %v" , acctID , err ))
130
- // r.getRoleARN errors are not terminal, we should requeue.
131
- return requeue .NeededAfter (err , roleARNNotAvailableRequeueDelay )
132
- }
133
- parsedARN , err := arn .Parse (string (roleARN ))
134
- if err != nil {
135
- return fmt .Errorf ("failed to parsed role ARN %q from namespace annotation: %v" , roleARN , err )
136
- }
137
- acctID = ackv1alpha1 .AWSAccountID (parsedARN .AccountID )
138
- } else {
139
- if needCARMLookup {
140
- // This means that the user is specifying a namespace that is
141
- // annotated with an owner account ID or team ID. We need to retrieve the
142
- // roleARN from the ConfigMap and properly requeue if the roleARN
143
- // is not available.
144
- roleARN , err = r .getOwnerAccountRoleARN (acctID )
123
+ if r .cfg .FeatureGates .IsEnabled (featuregate .CARMv2 ) {
124
+ teamID := r .getTeamID (res )
125
+ if teamID != "" {
126
+ // The user is specifying a namespace that is annotated with a team ID.
127
+ // Requeue if the corresponding roleARN is not available in the CARMv2 configmap.
128
+ // Additionally, set the account ID to the role's account ID.
129
+ roleARN , err = r .getRoleARNv2 (string (teamID ))
130
+ if err != nil {
131
+ ackrtlog .InfoAdoptedResource (r .log , res , fmt .Sprintf ("Unable to start adoption reconcilliation %s: %v" , acctID , err ))
132
+ return requeue .NeededAfter (err , roleARNNotAvailableRequeueDelay )
133
+ }
134
+ parsedARN , err := arn .Parse (string (roleARN ))
135
+ if err != nil {
136
+ return fmt .Errorf ("parsing role ARN %q from namespace annotation: %v" , roleARN , err )
137
+ }
138
+ acctID = ackv1alpha1 .AWSAccountID (parsedARN .AccountID )
139
+ } else if needCARMLookup {
140
+ // The user is specifying a namespace that is annotated with an owner account ID.
141
+ // Requeue if the corresponding roleARN is not available in the CARMv2 configmap.
142
+ roleARN , err = r .getRoleARNv2 (string (acctID ))
145
143
if err != nil {
146
144
ackrtlog .InfoAdoptedResource (r .log , res , fmt .Sprintf ("Unable to start adoption reconcilliation %s: %v" , acctID , err ))
147
- // r.getRoleARN errors are not terminal, we should requeue.
148
145
return requeue .NeededAfter (err , roleARNNotAvailableRequeueDelay )
149
146
}
150
147
}
148
+ } else if needCARMLookup {
149
+ // The user is specifying a namespace that is annotated with an owner account ID.
150
+ // Requeue if the corresponding roleARN is not available in the Accounts (CARMv1) configmap.
151
+ roleARN , err = r .getRoleARN (acctID )
152
+ if err != nil {
153
+ ackrtlog .InfoAdoptedResource (r .log , res , fmt .Sprintf ("Unable to start adoption reconcilliation %s: %v" , acctID , err ))
154
+ return requeue .NeededAfter (err , roleARNNotAvailableRequeueDelay )
155
+ }
151
156
}
152
157
153
158
region := r .getRegion (res )
@@ -512,34 +517,29 @@ func (r *adoptionReconciler) getEndpointURL(
512
517
return r .cfg .EndpointURL
513
518
}
514
519
515
- // getRoleARN return the Role ARN that should be assumed for accoutn ID
516
- // in order to manage the resources.
517
- func (r * adoptionReconciler ) getOwnerAccountRoleARN (
518
- acctID ackv1alpha1.AWSAccountID ,
519
- ) (ackv1alpha1.AWSResourceName , error ) {
520
- roleARN , err := r .cache .CARMMaps .GetValue (ackrtcache .OwnerAccountIDPrefix + string (acctID ))
521
- if err == ackrtcache .ErrCARMConfigMapNotFound || err == ackrtcache .ErrKeyNotFound {
522
- // CARM map v2 not defined. Check v1 map.
523
- roleARN , err = r .cache .Accounts .GetValue (string (acctID ))
524
- if err != nil {
525
- return "" , fmt .Errorf ("unable to retrieve role ARN for account %s: %v" , acctID , err )
526
- }
527
- } else if err != nil {
528
- return "" , fmt .Errorf ("unable to retrieve role ARN from CARM v2 for account %s: %v" , acctID , err )
520
+ // getRoleARNv2 returns the Role ARN that should be assumed for the given account/team ID,
521
+ // from the CARMv2 configmap, in order to manage the resources.
522
+ func (r * adoptionReconciler ) getRoleARNv2 (id string ) (ackv1alpha1.AWSResourceName , error ) {
523
+ // use service level roleARN if present
524
+ serviceID := r .sc .GetMetadata ().ServiceAlias + "." + id
525
+ if roleARN , err := r .cache .CARMMaps .GetValue (serviceID ); err == nil {
526
+ return ackv1alpha1 .AWSResourceName (roleARN ), nil
527
+ }
528
+ // otherwise use account/team level roleARN
529
+ roleARN , err := r .cache .CARMMaps .GetValue (id )
530
+ if err != nil {
531
+ return "" , fmt .Errorf ("retrieving role ARN for account/team ID %q from %q configmap: %v" , id , ackrtcache .ACKCARMMapV2 , err )
529
532
}
530
533
return ackv1alpha1 .AWSResourceName (roleARN ), nil
531
534
}
532
535
533
- // getTeamRoleARN return the Role ARN that should be assumed for a team ID
534
- // in order to manage the resources.
535
- func (r * adoptionReconciler ) getTeamRoleARN (
536
- teamID ackv1alpha1.TeamID ,
537
- ) (ackv1alpha1.AWSResourceName , error ) {
538
- roleARN , err := r .cache .CARMMaps .GetValue (ackrtcache .TeamIDPrefix + string (teamID ))
539
- if err == ackrtcache .ErrCARMConfigMapNotFound || err == ackrtcache .ErrKeyNotFound {
540
- return "" , fmt .Errorf ("unable to retrieve role ARN from CARM v2 for account %s: %v" , teamID , err )
536
+ // getRoleARN returns the Role ARN that should be assumed for the given account ID,
537
+ // from the CARMv1 configmap, in order to manage the resources.
538
+ func (r * adoptionReconciler ) getRoleARN (acctID ackv1alpha1.AWSAccountID ) (ackv1alpha1.AWSResourceName , error ) {
539
+ roleARN , err := r .cache .Accounts .GetValue (string (acctID ))
540
+ if err != nil {
541
+ return "" , fmt .Errorf ("retrieving role ARN for account ID %q from %q configMap: %v" , acctID , ackrtcache .ACKRoleAccountMap , err )
541
542
}
542
-
543
543
return ackv1alpha1 .AWSResourceName (roleARN ), nil
544
544
}
545
545
0 commit comments