-
Notifications
You must be signed in to change notification settings - Fork 181
Feat/account region protection #203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
a-hilaly
wants to merge
2
commits into
aws-controllers-k8s:main
Choose a base branch
from
a-hilaly:feat/account-region-protection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,11 +257,49 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request) | |
| if err != nil { | ||
| return r.handleCacheError(ctx, err, desired) | ||
| } | ||
| parsedARN, err := arn.Parse(string(roleARN)) | ||
| if err != nil { | ||
| return ctrlrt.Result{}, fmt.Errorf("parsing role ARN %q from %q configmap: %v", roleARN, ackrtcache.ACKRoleTeamMap, err) | ||
| } | ||
| acctID = ackv1alpha1.AWSAccountID(parsedARN.AccountID) | ||
| } | ||
|
|
||
| region := r.getRegion(desired) | ||
| endpointURL := r.getEndpointURL(desired) | ||
| gvk := r.rd.GroupVersionKind() | ||
|
|
||
| // If the user has specified a region that is different from the | ||
| // region the resource currently exists in, we need to fail the | ||
| // reconciliation with a terminal error. | ||
| if r.regionDrifted(desired) { | ||
|
Comment on lines
267
to
+274
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use the region in line 267? It already parses resource annotation - namespace annotation - config |
||
| msg := fmt.Sprintf( | ||
| "Resource already exists in region %s, but the desired state specifies region %s. ", | ||
| region, desired.MetaObject().GetAnnotations()[ackv1alpha1.AnnotationRegion], | ||
| ) | ||
| rlog.Info( | ||
| msg, | ||
| "current_region", region, | ||
| "desired_region", desired.Identifiers().Region(), | ||
| ) | ||
| return ctrlrt.Result{}, ackerr.NewTerminalError(errors.New(msg)) | ||
| } | ||
|
|
||
| // Similarly, if the user has specified an account ID that is different | ||
| // from the account ID the resource currently exists in, we need to | ||
| // fail the reconciliation with a terminal error. | ||
| if desired.Identifiers() != nil && desired.Identifiers().OwnerAccountID() != nil && *desired.Identifiers().OwnerAccountID() != acctID { | ||
| msg := fmt.Sprintf( | ||
| "Resource already exists in account %s, but the role used for reconciliation is in account %s. ", | ||
| *desired.Identifiers().OwnerAccountID(), acctID, | ||
| ) | ||
| rlog.Info( | ||
| msg, | ||
| "current_account", *desired.Identifiers().OwnerAccountID(), | ||
| "desired_account", acctID, | ||
| ) | ||
| return ctrlrt.Result{}, ackerr.NewTerminalError(errors.New(msg)) | ||
| } | ||
|
|
||
| // The config pivot to the roleARN will happen if it is not empty. | ||
| // in the NewResourceManager | ||
| clientConfig, err := r.sc.NewAWSConfig(ctx, region, &endpointURL, roleARN, gvk) | ||
|
|
@@ -285,6 +323,36 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request) | |
| return r.HandleReconcileError(ctx, desired, latest, err) | ||
| } | ||
|
|
||
| // regionDrifted return true if the desired resource region is different | ||
| // from the target region. Target region can be derived from the two places | ||
| // in the following order: | ||
| // 1) the region annotation on the resource | ||
| // 2) from the namespace annotation | ||
| func (r *resourceReconciler) regionDrifted(desired acktypes.AWSResource) bool { | ||
| if desired.Identifiers() == nil || desired.Identifiers().Region() == nil { | ||
| return false | ||
| } | ||
|
|
||
| currentRegion := desired.Identifiers().Region() | ||
|
|
||
| // look for region in CR metadata annotations | ||
| resAnnotations := desired.MetaObject().GetAnnotations() | ||
| region, ok := resAnnotations[ackv1alpha1.AnnotationRegion] | ||
| if ok { | ||
| return ackv1alpha1.AWSRegion(region) == *currentRegion | ||
| } | ||
|
|
||
| // look for default region in namespace metadata annotations | ||
| ns := desired.MetaObject().GetNamespace() | ||
| nsRegion, ok := r.cache.Namespaces.GetDefaultRegion(ns) | ||
| if ok { | ||
| return ackv1alpha1.AWSRegion(nsRegion) == *currentRegion | ||
| } | ||
|
|
||
| // use controller configuration region | ||
| return ackv1alpha1.AWSRegion(r.cfg.Region) == *currentRegion | ||
| } | ||
|
|
||
| func (r *resourceReconciler) handleCacheError( | ||
| ctx context.Context, | ||
| err error, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use https://github.com/aws-controllers-k8s/runtime/blob/main/pkg/runtime/reconciler.go#L1146-L1167 to return here? so that this error can be patched to the resource status