Skip to content

Commit

Permalink
add function to controllerutil RemoveControllerReference
Browse files Browse the repository at this point in the history
Signed-off-by: Troy Connor <troy0820@users.noreply.github.com>
  • Loading branch information
troy0820 committed Aug 24, 2023
1 parent c20ea14 commit d0ae418
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
21 changes: 21 additions & 0 deletions pkg/controller/controllerutil/controllerutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ func SetControllerReference(owner, controlled metav1.Object, scheme *runtime.Sch
return nil
}

// RemoveControllerReference is a helper method to make sure the given object removes an controller reference to the object provided.
// This allows you to remove the owner to establish a new owner of the object in a subsequent call.
func RemoveControllerReference(owner, controlled metav1.Object) error {
owners := controlled.GetOwnerReferences()
length := len(owners)
if length < 1 {
return fmt.Errorf("%T does not have any owner references", controlled)
}
index := 0
for i := 0; i < length; i++ {
if owners[i].Name == owner.GetName() {
owners = append(owners[:index], owners[index+1:]...)
}
index++
}
if length == len(owners) {
return fmt.Errorf("%T does not have an owner reference for %T", controlled, owner)
}
return nil
}

// SetOwnerReference is a helper method to make sure the given object contains an object reference to the object provided.
// This allows you to declare that owner has a dependency on the object without specifying it as a controller.
// If a reference to the same object already exists, it'll be overwritten with the newly provided version.
Expand Down
41 changes: 40 additions & 1 deletion pkg/controller/controllerutil/controllerutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ var _ = Describe("Controllerutil", func() {
}))
})
})

Describe("SetControllerReference", func() {
It("should set the OwnerReference if it can find the group version kind", func() {
rs := &appsv1.ReplicaSet{}
Expand Down Expand Up @@ -256,6 +255,46 @@ var _ = Describe("Controllerutil", func() {
}))
})
})
Describe("RemoveControllerReference", func() {
It("should remove the owner reference established by the SetControllerReference function", func() {
rs := &appsv1.ReplicaSet{}
dep := &extensionsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
}

Expect(controllerutil.SetControllerReference(dep, rs, scheme.Scheme)).NotTo(HaveOccurred())
t := true
Expect(rs.OwnerReferences).To(ConsistOf(metav1.OwnerReference{
Name: "foo",
Kind: "Deployment",
APIVersion: "extensions/v1beta1",
UID: "foo-uid",
Controller: &t,
BlockOwnerDeletion: &t,
}))

Expect(controllerutil.RemoveControllerReference(dep, rs)).NotTo(HaveOccurred())
})
It("should fail and return an error if the length is less than 1", func() {
rs := &appsv1.ReplicaSet{}
dep := &extensionsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
}
Expect(controllerutil.RemoveControllerReference(dep, rs)).To(HaveOccurred())
})
It("should fail and return an error because the owner doesn't exist to remove", func() {
rs := &appsv1.ReplicaSet{}
dep := &extensionsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "foo", UID: "foo-uid"},
}
dep2 := &extensionsv1beta1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: "bar", UID: "bar-uid"},
}
Expect(controllerutil.SetControllerReference(dep, rs, scheme.Scheme)).NotTo(HaveOccurred())
Expect(controllerutil.RemoveControllerReference(dep2, rs)).To(HaveOccurred())
})

})

Describe("CreateOrUpdate", func() {
var deploy *appsv1.Deployment
Expand Down

0 comments on commit d0ae418

Please sign in to comment.