🐛Remove owner passed in to RemoveControllerReference #2595
🐛Remove owner passed in to RemoveControllerReference #2595k8s-ci-robot merged 1 commit intokubernetes-sigs:mainfrom
Conversation
5f0a3f4 to
34aaa05
Compare
34aaa05 to
25bf123
Compare
| 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 |
There was a problem hiding this comment.
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
- Only one ControllerReference (1:1) be present on an object but many OwnerReferences (1:N)
- The index returned verifies that the above owner is in fact the controller reference on the object if the index is greater than -1.
|
/ok-to-test |
0ed0fc1 to
a2a0263
Compare
| dep2 := &extensionsv1beta1.Deployment{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "foo-2", UID: "foo-uid-42"}, | ||
| } | ||
| Expect(controllerutil.SetControllerReference(dep, rs, scheme.Scheme)).ToNot(HaveOccurred()) |
There was a problem hiding this comment.
| 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)
There was a problem hiding this comment.
So it exists, but the owner we passed in would still error because it doesn't have the controller == true. Makes sense.
a2a0263 to
dffeafa
Compare
|
/retest |
…er controller equals true Signed-off-by: Troy Connor <troy0820@users.noreply.github.com>
e204fb3 to
9c4611e
Compare
|
@troy0820 could you fix the typo in the PR title? |
|
[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 DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Fixes initial PR of #2509
Resolves #2594
/assign @sbueringer