Skip to content
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

Add deletion policy CLI arguments and annotations #107

Merged
merged 4 commits into from
Jan 10, 2023

Conversation

RedbackThomson
Copy link
Contributor

Issue #, if available: aws-controllers-k8s/community#82

Description of changes:
As per the proposal: aws-controllers-k8s/community#1148

Adds support for a new optional CLI argument for the controller binary --deletion-policy, supporting delete and retain as available options. Setting this argument to delete leaves the controller as the current default behaviour, whereby it deletes the AWS resource under management before it is removed from the K8s API server. Setting this argument to retain modifies the default behaviour, leaving the AWS resources intact (taking no action on them) before removing it from the K8s API server.

Adds support for a new annotation on Namespace objects: {service}.services.k8s.aws/deletion-policy: delete/retain (where {service} is the service alias of the controller eg. s3). This annotation overrides the deletion policy behaviour configured for the controller for all resources deployed under the namespace.

Adds support for a new annotation on any ACK resource object: services.k8s.aws/deletion-policy: delete/retain. This annotation overrides only the deletion policy behaviour configured for the resource.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link

@jljaco jljaco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks very solid. I have a couple of very minor comments, both about logging.

How do you propose to functionally test this change? If, for example, in an e2e test of a controller, can you please link to the relevant PR or test here?

apis/core/v1alpha1/deletion_policy.go Outdated Show resolved Hide resolved
pkg/runtime/reconciler.go Show resolved Hide resolved
@RedbackThomson
Copy link
Contributor Author

How do you propose to functionally test this change? If, for example, in an e2e test of a controller, can you please link to the relevant PR or test here?

I am writing an additional test for S3 that creates a series of different namespaces and resources with varying annotations. The only thing that we cannot test with a standard controller e2e test is the CLI argument (we can't update the controller CLI args inside a Pytest). For that the best I can do for now is manual testing, sadly

@@ -202,6 +202,8 @@ func (r *resourceReconciler) reconcile(
return r.deleteResource(ctx, rm, res)
}

rlog := ackrtlog.FromContext(ctx)
rlog.Info("AWS resource will not be deleted - deletion policy set to retain")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to include a mention of which resource is being retained? Is there a convenient resource name available in res that you can copy a bit above before deletion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is automatically added into the logs when using the rlog object. I posted a comment underneath your last comment asking for logs with an example log message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RedbackThomson
Copy link
Contributor Author

How do you propose to functionally test this change? If, for example, in an e2e test of a controller, can you please link to the relevant PR or test here?

I am writing an additional test for S3 that creates a series of different namespaces and resources with varying annotations. The only thing that we cannot test with a standard controller e2e test is the CLI argument (we can't update the controller CLI args inside a Pytest). For that the best I can do for now is manual testing, sadly

These tests can be found in this pull request aws-controllers-k8s/s3-controller#91

@@ -1045,6 +1055,35 @@ func (r *resourceReconciler) getRegion(
return ackv1alpha1.AWSRegion(r.cfg.Region)
}

// getRegion returns the resource's deletion policy based on the default
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticed a typo here. Fixing now

@jljaco
Copy link

jljaco commented Jan 10, 2023

/lgtm

@ack-bot ack-bot added the lgtm Indicates that a PR is ready to be merged. label Jan 10, 2023
@ack-bot
Copy link
Collaborator

ack-bot commented Jan 10, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: jljaco, RedbackThomson

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [RedbackThomson,jljaco]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ack-bot ack-bot merged commit 201f68a into aws-controllers-k8s:main Jan 10, 2023
@RedbackThomson RedbackThomson deleted the deletionpolicy branch January 10, 2023 01:50
Copy link
Member

@a-hilaly a-hilaly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great one! Leaving few comments and questions!

pkg/config/config.go Show resolved Hide resolved
@@ -177,6 +201,17 @@ func (c *NamespaceCache) setNamespaceInfoFromK8sObject(ns *corev1.Namespace) {
if ok {
nsInfo.endpointURL = EndpointURL
}

nsInfo.deletionPolicies = map[string]string{}
nsDeletionPolicySuffix := "." + ackv1alpha1.AnnotationDeletionPolicy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Very tiny optimization: we can declare this as a constant outside of this function to avoid making unnecessary allocations everytime the cache syncs

continue
}
service := strings.TrimSuffix(key, nsDeletionPolicySuffix)
nsInfo.deletionPolicies[service] = elem
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can apply a string.ToLower to service variable here. Thinking users can provide an eCR or Eks ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also see that you're using strings.ToLower(service) to query the deletion policy

pkg/runtime/reconciler.go Show resolved Hide resolved
if r.getDeletionPolicy(res) == ackv1alpha1.DeletionPolicyDelete {
// Resolve references before deleting the resource.
// Ignore any errors while resolving the references
res, _ = rm.ResolveReferences(ctx, r.apiReader, res)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QQ: Any reason why we need to resolve the references here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't modify this code, here, I just added a conditional around it. But I believe we need to resolve references so that we have all the necessary fields to call sdkDelete if they require those fields.

pkg/runtime/reconciler.go Show resolved Hide resolved
apis/core/v1alpha1/deletion_policy.go Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants