@@ -52,12 +52,22 @@ func newAlreadyOwnedError(obj metav1.Object, owner metav1.OwnerReference) *Alrea
5252 }
5353}
5454
55+ // OwnerReferenceOption is a function that can modify a `metav1.OwnerReference`.
56+ type OwnerReferenceOption func (* metav1.OwnerReference )
57+
58+ // WithBlockOwnerDeletion allows configuring the BlockOwnerDeletion field on the `metav1.OwnerReference`.
59+ func WithBlockOwnerDeletion (blockOwnerDeletion bool ) OwnerReferenceOption {
60+ return func (ref * metav1.OwnerReference ) {
61+ ref .BlockOwnerDeletion = & blockOwnerDeletion
62+ }
63+ }
64+
5565// SetControllerReference sets owner as a Controller OwnerReference on controlled.
5666// This is used for garbage collection of the controlled object and for
5767// reconciling the owner object on changes to controlled (with a Watch + EnqueueRequestForOwner).
5868// Since only one OwnerReference can be a controller, it returns an error if
5969// there is another OwnerReference with Controller flag set.
60- func SetControllerReference (owner , controlled metav1.Object , scheme * runtime.Scheme ) error {
70+ func SetControllerReference (owner , controlled metav1.Object , scheme * runtime.Scheme , opts ... OwnerReferenceOption ) error {
6171 // Validate the owner.
6272 ro , ok := owner .(runtime.Object )
6373 if ! ok {
@@ -80,6 +90,9 @@ func SetControllerReference(owner, controlled metav1.Object, scheme *runtime.Sch
8090 BlockOwnerDeletion : ptr .To (true ),
8191 Controller : ptr .To (true ),
8292 }
93+ for _ , opt := range opts {
94+ opt (& ref )
95+ }
8396
8497 // Return early with an error if the object is already controlled.
8598 if existing := metav1 .GetControllerOf (controlled ); existing != nil && ! referSameObject (* existing , ref ) {
@@ -94,7 +107,7 @@ func SetControllerReference(owner, controlled metav1.Object, scheme *runtime.Sch
94107// SetOwnerReference is a helper method to make sure the given object contains an object reference to the object provided.
95108// This allows you to declare that owner has a dependency on the object without specifying it as a controller.
96109// If a reference to the same object already exists, it'll be overwritten with the newly provided version.
97- func SetOwnerReference (owner , object metav1.Object , scheme * runtime.Scheme ) error {
110+ func SetOwnerReference (owner , object metav1.Object , scheme * runtime.Scheme , opts ... OwnerReferenceOption ) error {
98111 // Validate the owner.
99112 ro , ok := owner .(runtime.Object )
100113 if ! ok {
@@ -115,6 +128,9 @@ func SetOwnerReference(owner, object metav1.Object, scheme *runtime.Scheme) erro
115128 UID : owner .GetUID (),
116129 Name : owner .GetName (),
117130 }
131+ for _ , opt := range opts {
132+ opt (& ref )
133+ }
118134
119135 // Update owner references and return.
120136 upsertOwnerRef (ref , object )
0 commit comments