-
Notifications
You must be signed in to change notification settings - Fork 172
Add addoption by annotation feature #164
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -57,6 +57,10 @@ const ( | |
// resource if the CARM cache is not synced yet, or if the roleARN is not | ||
// available. | ||
roleARNNotAvailableRequeueDelay = 15 * time.Second | ||
// adoptOrCreate is an annotation field that decides whether to create the | ||
// resource if it doesn't exist, or adopt the resource if it exists. | ||
// value comes from getAdoptionPolicy | ||
// adoptOrCreate = "adopt-or-create" | ||
) | ||
|
||
// reconciler describes a generic reconciler within ACK. | ||
|
@@ -298,6 +302,70 @@ func (r *resourceReconciler) handleCacheError( | |
return r.HandleReconcileError(ctx, desired, latest, requeue.NeededAfter(err, roleARNNotAvailableRequeueDelay)) | ||
} | ||
|
||
func (r *resourceReconciler) handleAdoption( | ||
ctx context.Context, | ||
rm acktypes.AWSResourceManager, | ||
desired acktypes.AWSResource, | ||
rlog acktypes.Logger, | ||
) (acktypes.AWSResource, error) { | ||
// If the resource is being adopted by force, we need to access | ||
// the required field passed by annotation and attempt a read. | ||
|
||
rlog.Info("Adopting Resource") | ||
extractedFields, err := ExtractAdoptionFields(desired) | ||
if err != nil { | ||
return desired, ackerr.NewTerminalError(err) | ||
} | ||
if len(extractedFields) == 0 { | ||
// TODO(michaelhtm) Here we need to figure out if we want to have an | ||
// error or not. should we consider accepting values from Spec? | ||
// And then we can let the ReadOne figure out if we have missing | ||
// required fields for a Read | ||
return nil, fmt.Errorf("failed extracting fields from annotation") | ||
} | ||
resolved := desired.DeepCopy() | ||
err = resolved.PopulateResourceFromAnnotation(extractedFields) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rlog.Enter("rm.EnsureTags") | ||
err = rm.EnsureTags(ctx, resolved, r.sc.GetMetadata()) | ||
rlog.Exit("rm.EnsureTags", err) | ||
if err != nil { | ||
return resolved, err | ||
} | ||
rlog.Enter("rm.ReadOne") | ||
latest, err := rm.ReadOne(ctx, resolved) | ||
if err != nil { | ||
return latest, err | ||
} | ||
|
||
if err = r.setResourceManaged(ctx, rm, latest); err != nil { | ||
return latest, err | ||
} | ||
|
||
// Ensure tags again after adding the finalizer and patching the | ||
// resource. Patching desired resource omits the controller tags | ||
// because they are not persisted in etcd. So we again ensure | ||
// that tags are present before performing the create operation. | ||
rlog.Enter("rm.EnsureTags") | ||
err = rm.EnsureTags(ctx, latest, r.sc.GetMetadata()) | ||
rlog.Exit("rm.EnsureTags", err) | ||
if err != nil { | ||
return latest, err | ||
} | ||
r.rd.MarkAdopted(latest) | ||
rlog.WithValues("is_adopted", "true") | ||
latest, err = r.patchResourceMetadataAndSpec(ctx, rm, desired, latest) | ||
if err != nil { | ||
return latest, err | ||
} | ||
|
||
rlog.Info("Resource Adopted") | ||
return latest, nil | ||
} | ||
|
||
// reconcile either cleans up a deleted resource or ensures that the supplied | ||
// AWSResource's backing API resource matches the supplied desired state. | ||
// | ||
|
@@ -360,13 +428,30 @@ func (r *resourceReconciler) Sync( | |
isAdopted := IsAdopted(desired) | ||
rlog.WithValues("is_adopted", isAdopted) | ||
|
||
if r.cfg.FeatureGates.IsEnabled(featuregate.ResourceAdoption) { | ||
if NeedAdoption(desired) && !r.rd.IsManaged(desired) { | ||
latest, err := r.handleAdoption(ctx, rm, desired, rlog) | ||
|
||
if err != nil { | ||
// If we get an error, we want to return here | ||
// TODO(michaelhtm): Change the handling of | ||
// the error to allow Adopt or Create here | ||
// when supported | ||
return latest, err | ||
} | ||
return latest, nil | ||
} | ||
} | ||
|
||
if r.cfg.FeatureGates.IsEnabled(featuregate.ReadOnlyResources) { | ||
isReadOnly := IsReadOnly(desired) | ||
rlog.WithValues("is_read_only", isReadOnly) | ||
|
||
// NOTE(a-hilaly): When the time comes to support adopting resources | ||
// using annotations, we will need to think a little bit more about | ||
// the case where a user, wants to adopt a resource as read-only. | ||
// | ||
// NOTE(michaelhtm): Done, tnx :) | ||
Comment on lines
+453
to
+454
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. 🚀 |
||
|
||
// If the resource is read-only, we enter a different code path where we | ||
// only read the resource and patch the metadata and spec. | ||
|
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
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.
Uh oh!
There was an error while loading. Please reload this page.