Skip to content

Commit d0ae418

Browse files
committed
add function to controllerutil RemoveControllerReference
Signed-off-by: Troy Connor <[email protected]>
1 parent c20ea14 commit d0ae418

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

pkg/controller/controllerutil/controllerutil.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ func SetControllerReference(owner, controlled metav1.Object, scheme *runtime.Sch
9191
return nil
9292
}
9393

94+
// RemoveControllerReference is a helper method to make sure the given object removes an controller reference to the object provided.
95+
// This allows you to remove the owner to establish a new owner of the object in a subsequent call.
96+
func RemoveControllerReference(owner, controlled metav1.Object) error {
97+
owners := controlled.GetOwnerReferences()
98+
length := len(owners)
99+
if length < 1 {
100+
return fmt.Errorf("%T does not have any owner references", controlled)
101+
}
102+
index := 0
103+
for i := 0; i < length; i++ {
104+
if owners[i].Name == owner.GetName() {
105+
owners = append(owners[:index], owners[index+1:]...)
106+
}
107+
index++
108+
}
109+
if length == len(owners) {
110+
return fmt.Errorf("%T does not have an owner reference for %T", controlled, owner)
111+
}
112+
return nil
113+
}
114+
94115
// SetOwnerReference is a helper method to make sure the given object contains an object reference to the object provided.
95116
// This allows you to declare that owner has a dependency on the object without specifying it as a controller.
96117
// If a reference to the same object already exists, it'll be overwritten with the newly provided version.

pkg/controller/controllerutil/controllerutil_test.go

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ var _ = Describe("Controllerutil", func() {
103103
}))
104104
})
105105
})
106-
107106
Describe("SetControllerReference", func() {
108107
It("should set the OwnerReference if it can find the group version kind", func() {
109108
rs := &appsv1.ReplicaSet{}
@@ -256,6 +255,46 @@ var _ = Describe("Controllerutil", func() {
256255
}))
257256
})
258257
})
258+
Describe("RemoveControllerReference", func() {
259+
It("should remove the owner reference established by the SetControllerReference function", func() {
260+
rs := &appsv1.ReplicaSet{}
261+
dep := &extensionsv1beta1.Deployment{
262+
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
263+
}
264+
265+
Expect(controllerutil.SetControllerReference(dep, rs, scheme.Scheme)).NotTo(HaveOccurred())
266+
t := true
267+
Expect(rs.OwnerReferences).To(ConsistOf(metav1.OwnerReference{
268+
Name: "foo",
269+
Kind: "Deployment",
270+
APIVersion: "extensions/v1beta1",
271+
UID: "foo-uid",
272+
Controller: &t,
273+
BlockOwnerDeletion: &t,
274+
}))
275+
276+
Expect(controllerutil.RemoveControllerReference(dep, rs)).NotTo(HaveOccurred())
277+
})
278+
It("should fail and return an error if the length is less than 1", func() {
279+
rs := &appsv1.ReplicaSet{}
280+
dep := &extensionsv1beta1.Deployment{
281+
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
282+
}
283+
Expect(controllerutil.RemoveControllerReference(dep, rs)).To(HaveOccurred())
284+
})
285+
It("should fail and return an error because the owner doesn't exist to remove", func() {
286+
rs := &appsv1.ReplicaSet{}
287+
dep := &extensionsv1beta1.Deployment{
288+
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
289+
}
290+
dep2 := &extensionsv1beta1.Deployment{
291+
ObjectMeta: metav1.ObjectMeta{Name: "bar", UID: "bar-uid"},
292+
}
293+
Expect(controllerutil.SetControllerReference(dep, rs, scheme.Scheme)).NotTo(HaveOccurred())
294+
Expect(controllerutil.RemoveControllerReference(dep2, rs)).To(HaveOccurred())
295+
})
296+
297+
})
259298

260299
Describe("CreateOrUpdate", func() {
261300
var deploy *appsv1.Deployment

0 commit comments

Comments
 (0)