@@ -52,12 +52,22 @@ func newAlreadyOwnedError(obj metav1.Object, owner metav1.OwnerReference) *Alrea
52
52
}
53
53
}
54
54
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
+
55
65
// SetControllerReference sets owner as a Controller OwnerReference on controlled.
56
66
// This is used for garbage collection of the controlled object and for
57
67
// reconciling the owner object on changes to controlled (with a Watch + EnqueueRequestForOwner).
58
68
// Since only one OwnerReference can be a controller, it returns an error if
59
69
// 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 {
61
71
// Validate the owner.
62
72
ro , ok := owner .(runtime.Object )
63
73
if ! ok {
@@ -80,6 +90,9 @@ func SetControllerReference(owner, controlled metav1.Object, scheme *runtime.Sch
80
90
BlockOwnerDeletion : ptr .To (true ),
81
91
Controller : ptr .To (true ),
82
92
}
93
+ for _ , opt := range opts {
94
+ opt (& ref )
95
+ }
83
96
84
97
// Return early with an error if the object is already controlled.
85
98
if existing := metav1 .GetControllerOf (controlled ); existing != nil && ! referSameObject (* existing , ref ) {
@@ -94,7 +107,7 @@ func SetControllerReference(owner, controlled metav1.Object, scheme *runtime.Sch
94
107
// SetOwnerReference is a helper method to make sure the given object contains an object reference to the object provided.
95
108
// This allows you to declare that owner has a dependency on the object without specifying it as a controller.
96
109
// 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 {
98
111
// Validate the owner.
99
112
ro , ok := owner .(runtime.Object )
100
113
if ! ok {
@@ -115,6 +128,9 @@ func SetOwnerReference(owner, object metav1.Object, scheme *runtime.Scheme) erro
115
128
UID : owner .GetUID (),
116
129
Name : owner .GetName (),
117
130
}
131
+ for _ , opt := range opts {
132
+ opt (& ref )
133
+ }
118
134
119
135
// Update owner references and return.
120
136
upsertOwnerRef (ref , object )
0 commit comments