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

🐛Remove owner passed in to RemoveControllerReference #2595

Conversation

troy0820
Copy link
Member

Fixes initial PR of #2509

Resolves #2594

  • Add a check to find owner reference that equals the passed in owner and remove if controller = true

/assign @sbueringer

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Nov 24, 2023
@k8s-ci-robot k8s-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Nov 24, 2023
@troy0820 troy0820 force-pushed the troy0820/remove-controller-ref-bug branch 2 times, most recently from 5f0a3f4 to 34aaa05 Compare November 24, 2023 19:17
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 24, 2023
@troy0820 troy0820 force-pushed the troy0820/remove-controller-ref-bug branch from 34aaa05 to 25bf123 Compare November 24, 2023 19:20
Comment on lines 174 to 199
ro, ok := owner.(runtime.Object)
if !ok {
return fmt.Errorf("%T is not a runtime.Object, cannot call RemoveControllerReference", owner)
}
gvk, err := apiutil.GVKForObject(ro, scheme)
if err != nil {
return err
}
ownerRefs := object.GetOwnerReferences()
index := indexOwnerRef(ownerRefs, metav1.OwnerReference{
APIVersion: gvk.GroupVersion().String(),
Name: owner.GetName(),
Kind: gvk.Kind,
Controller: ptr.To(true),
})
if index == -1 {
return fmt.Errorf("%T does not have an controller reference for %T", object, owner)
}
ownerRefs = append(ownerRefs[:index], ownerRefs[index+1:]...)
object.SetOwnerReferences(ownerRefs)
return nil
Copy link
Member Author

@troy0820 troy0820 Nov 24, 2023

Choose a reason for hiding this comment

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

Not a fan of this replicated logic but didn't want to conflate the errors that may exist passing into another function. Also, finding the index happens twice but we need to find the index to verify that the owner is the controllerReference for the object.

This seems to make sure the only controllerReference is the owner that is passed in because

  1. Only one ControllerReference (1:1) be present on an object but many OwnerReferences (1:N)
  2. The index returned verifies that the above owner is in fact the controller reference on the object if the index is greater than -1.

@troy0820
Copy link
Member Author

/ok-to-test

@k8s-ci-robot k8s-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Nov 27, 2023
pkg/controller/controllerutil/controllerutil.go Outdated Show resolved Hide resolved
@@ -171,7 +171,27 @@ func RemoveControllerReference(owner, object metav1.Object, scheme *runtime.Sche
if ok := HasControllerReference(object); !ok {
Copy link
Member

Choose a reason for hiding this comment

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

WDYT about adding additional test cases for the cases which weren't captured by the existing tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can do that. My work here still doesn't show that the owner reference that exists is in fact the actual controller Reference that is being removed.

Copy link
Member Author

Choose a reason for hiding this comment

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

The test I added was to show that this errors when passing in the wrong owner. @sbueringer

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Nov 27, 2023
@troy0820 troy0820 force-pushed the troy0820/remove-controller-ref-bug branch 2 times, most recently from 0ed0fc1 to a2a0263 Compare November 27, 2023 17:26
dep2 := &extensionsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "foo-2", UID: "foo-uid-42"},
}
Expect(controllerutil.SetControllerReference(dep, rs, scheme.Scheme)).ToNot(HaveOccurred())
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Expect(controllerutil.SetControllerReference(dep, rs, scheme.Scheme)).ToNot(HaveOccurred())
Expect(controllerutil.SetControllerReference(dep, rs, scheme.Scheme)).ToNot(HaveOccurred())
Expect(controllerutil.SetOwnerReference(dep2, rs, scheme.Scheme)).ToNot(HaveOccurred())

Wondering if that would add useful coverage. Basically we error out because the owner we want to remove is only owner but not the controller

(which is exactly the case we missed before, I think)

Copy link
Member Author

Choose a reason for hiding this comment

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

So it exists, but the owner we passed in would still error because it doesn't have the controller == true. Makes sense.

Copy link
Member

Choose a reason for hiding this comment

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

Thx!

@troy0820 troy0820 force-pushed the troy0820/remove-controller-ref-bug branch from a2a0263 to dffeafa Compare November 27, 2023 18:27
@troy0820
Copy link
Member Author

/retest

…er controller equals true

Signed-off-by: Troy Connor <troy0820@users.noreply.github.com>
@troy0820 troy0820 force-pushed the troy0820/remove-controller-ref-bug branch 2 times, most recently from e204fb3 to 9c4611e Compare November 27, 2023 20:27
@inteon
Copy link
Member

inteon commented Dec 2, 2023

@troy0820 could you fix the typo in the PR title?

@troy0820 troy0820 changed the title 🐛Remove owner passed in to RemoveControlleReference 🐛Remove owner passed in to RemoveControllerReference Dec 2, 2023
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 4, 2023
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, troy0820

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 Dec 4, 2023
@k8s-ci-robot k8s-ci-robot merged commit 1fe3341 into kubernetes-sigs:main Dec 4, 2023
10 checks passed
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/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RemoveControllerReference in controllerutil bug.
5 participants