-
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
✨ Add public functions to add/remove OwnerReferences without the referenced object #2991
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,6 +168,36 @@ func RemoveOwnerReference(owner, object metav1.Object, scheme *runtime.Scheme) e | |
return nil | ||
} | ||
|
||
// AppendOwnerReference is a helper method to make sure the given object contains a provided owner reference. | ||
// This allows you to declare that owner has a dependency on the object without specifying it as a controller. | ||
// If a reference already exists, it'll be overwritten with the newly provided version. | ||
func AppendOwnerReference(owner metav1.OwnerReference, object metav1.Object) { | ||
upsertOwnerRef(owner, object) | ||
} | ||
|
||
// DropOwnerReference is a helper method to make sure the given object removes a provided owner reference. | ||
// This allows you to remove the owner to establish a new owner of the object in a subsequent call. | ||
func DropOwnerReference(owner metav1.OwnerReference, object metav1.Object) error { | ||
ownerRefs := object.GetOwnerReferences() | ||
index := indexOwnerRef(ownerRefs, metav1.OwnerReference{ | ||
APIVersion: owner.APIVersion, | ||
Name: owner.Name, | ||
Kind: owner.Kind, | ||
}) | ||
Comment on lines
+182
to
+186
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually do this with GVK because relying on this may not be stable. (The workaround exist because of issues around this). Getting the GVK requires the scheme. Adding the scheme makes this method the I am trying to understand the repeat logic around this functionality. What are we trying to circumvent utilizing this logic? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you elaborate on that instability? Is that documented somewhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this concern be addressed with groupVersion, err := schema.ParseGroupVersion(owner.APIVersion)
if err != nil {
return err
}
ownerRefs := object.GetOwnerReferences()
index := indexOwnerRef(ownerRefs, metav1.OwnerReference{
APIVersion: groupVersion.String(),
Name: owner.Name,
Kind: owner.Kind,
}) ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed here: #2882 (comment) I am still trying to understand what you are trying to do because this looks very similar to what is already in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function I added just so there was a pair of functions that work with OwnerReference instead of Object. I don't personally need the DropOwnerReference function. |
||
|
||
if index == -1 { | ||
return fmt.Errorf("%T does not have an controller reference for %T", object, owner) | ||
} | ||
|
||
if ownerRefs[index].Controller == nil || !*ownerRefs[index].Controller { | ||
return fmt.Errorf("%T owner is not the controller reference for %T", owner, object) | ||
} | ||
|
||
ownerRefs = append(ownerRefs[:index], ownerRefs[index+1:]...) | ||
object.SetOwnerReferences(ownerRefs) | ||
return nil | ||
} | ||
|
||
// HasControllerReference returns true if the object | ||
// has an owner ref with controller equal to true | ||
func HasControllerReference(object metav1.Object) bool { | ||
|
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.
This comment is exactly like
SetOwnerReference
. How is this any different besides not using the scheme to get the GVK and skippingvalidateOwner
?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.
I couldn't think of a way to use
Get
insigs.ki8s.io/controller-runtime/pkg/client
without know what struct I need in advance (ReplicaSet, Job, etc). If there's a way to do this, then perhaps this entire request is unneeded.The difference between this function and
SetOwnerReference
is that it takes anOwnerReference
rather than anObject
to use as the new owner.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.
This also sidesteps the validation that the function provides when adding an owner reference. Not sure if want to do that or allow the appending of owner references to any object outside of the scope where the owner would be setting it. This seems to be very dangerous where the lifecycle is now beholden to an owner reference the owner object didn't explicitly set.
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.
I don't follow this second concern. In my experience, it's not at all unusual for a controller to create a resource on behalf of something, and have the resource share the lifecycle of that thing. Is your concern only about resources created by other controllers?