-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
🐛Remove owner passed in to RemoveControllerReference #2595
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.
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
- 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thx!
a2a0263
to
dffeafa
Compare
/retest |
…er controller equals true Signed-off-by: Troy Connor <[email protected]>
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
Needs 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