Skip to content

✨ Add ContainsFinalizer helper to the controllerutil #922

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 1 commit into from
May 22, 2020

Conversation

dmvolod
Copy link
Member

@dmvolod dmvolod commented Apr 23, 2020

This is ContainsFinalizer helper in the controllerutil package which checks that provided finalizer exists in the Object meta

Fixes #920

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Apr 23, 2020
@k8s-ci-robot k8s-ci-robot requested review from alenkacz and droot April 23, 2020 10:43
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Apr 23, 2020
@k8s-ci-robot
Copy link
Contributor

Hi @dmvolod. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 23, 2020
@dmvolod
Copy link
Member Author

dmvolod commented Apr 24, 2020

@joelanford, @alexeldeib could you please to have a look at this changes.

Copy link
Member

@joelanford joelanford left a comment

Choose a reason for hiding this comment

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

Thanks! This is something I've meaning to get to 🙂

/ok-to-test

@@ -446,6 +446,18 @@ var _ = Describe("Controllerutil", func() {
Expect(deploy.ObjectMeta.GetFinalizers()).To(Equal([]string{}))
})
})

Copy link
Member

Choose a reason for hiding this comment

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

Can you also add a test for ContainsFinalizerWithError, similar to the ones in lines 413-423?

Copy link
Member Author

Choose a reason for hiding this comment

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

@joelanford , thanks, fixed

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 25, 2020
@joelanford
Copy link
Member

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: dmvolod, joelanford

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:

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

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Apr 30, 2020

// ContainsFinalizerWithError checks a metav1 object that the provided finalizer is present.
// It returns an error if the provided object cannot provide an accessor.
func ContainsFinalizerWithError(o runtime.Object, finalizer string) (bool, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this needed? All concrete api types, be they built-in or CRDs, have am emedded metav1.ObjectMeta and thus fulfill the metav1.Object interface. If you really end up writing generic code that acts on a runtime.Object you can just assert it as metav1.Object prior to trying to find out if it has the given finalizer.

The only type of runtime.Object where this will not work is list types, but that makes sense as they do not implement the metav1.Object interface. IMHO it would be better to tell ppl at compile time that this isn't possible for Lists rather than at runtime when meta.Accessor errors.

Copy link
Member

Choose a reason for hiding this comment

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

I asked for this to be added to align with the other existing *WithError functions in this package.

I agree, though, that compile time type checking is better than runtime type assertions. #898 may help with this.

Copy link
Member

Choose a reason for hiding this comment

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

#898 merged so maybe we should remove ContainsFinalizerWithError again and update ContainsFinalizer to take an controllerutil.Object as first agument?

Copy link
Member Author

Choose a reason for hiding this comment

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

@alvaroaleman , thanks, I will do. Do we need also delete other *WithError methods and replace with controllerutil.Object methods which are not handling errors?

Copy link
Member

Choose a reason for hiding this comment

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

For now, please leave the existing code as it is. Removing the WithError funcs that we already have is a breaking change, so we should do it in a dedicated PR to make sure it gets its own release note and only gets merged when we are ready to take breaking changes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

Copy link
Member

Choose a reason for hiding this comment

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

Thank you!
/lgtm

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 22, 2020
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels May 22, 2020
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label May 22, 2020
@k8s-ci-robot k8s-ci-robot merged commit 2f1457b into kubernetes-sigs:master May 22, 2020
XudongLiuHarold added a commit to vmware-tanzu/load-balancer-operator-for-kubernetes that referenced this pull request Jun 3, 2022
XudongLiuHarold added a commit to vmware-tanzu/load-balancer-operator-for-kubernetes that referenced this pull request Jun 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ContainsFinalizer helper to the controllerutil
4 participants