Skip to content

Commit d31a005

Browse files
committed
Fix SetControllerReference to consider multiple CRD versions
An error was returned by a call to `SetControllerReference` if the owner is already set, but with a different apiVersion (eg. `group/v1beta1` instead of `group/v1`). This commit fixes it by ignoring the version in the ref comparison. It still considers the group though. Having an owner with a different version does not return an error, but the call to `SetControllerReference` effectively changes the owner to match the newer apiVersion.
1 parent afac980 commit d31a005

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pkg/controller/controllerutil/controllerutil.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func referSameObject(a, b metav1.OwnerReference) bool {
111111
return false
112112
}
113113

114-
return aGV == bGV && a.Kind == b.Kind && a.Name == b.Name
114+
return aGV.Group == bGV.Group && a.Kind == b.Kind && a.Name == b.Name
115115
}
116116

117117
// OperationResult is the action result of a CreateOrUpdate call

pkg/controller/controllerutil/controllerutil_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,35 @@ var _ = Describe("Controllerutil", func() {
133133

134134
Expect(err).To(HaveOccurred())
135135
})
136+
137+
138+
It("should not return any error if the existing owner has a different version", func() {
139+
f := false
140+
t := true
141+
rsOwners := []metav1.OwnerReference{
142+
{
143+
Name: "foo",
144+
Kind: "Deployment",
145+
APIVersion: "extensions/v1alpha1",
146+
UID: "foo-uid",
147+
Controller: &f,
148+
BlockOwnerDeletion: &t,
149+
},
150+
}
151+
rs := &appsv1.ReplicaSet{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default", OwnerReferences: rsOwners}}
152+
dep := &extensionsv1beta1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default", UID: "foo-uid"}}
153+
154+
Expect(controllerutil.SetControllerReference(dep, rs, scheme.Scheme)).NotTo(HaveOccurred())
155+
Expect(rs.OwnerReferences).To(ConsistOf(metav1.OwnerReference{
156+
Name: "foo",
157+
Kind: "Deployment",
158+
// APIVersion is the new owner's one
159+
APIVersion: "extensions/v1beta1",
160+
UID: "foo-uid",
161+
Controller: &t,
162+
BlockOwnerDeletion: &t,
163+
}))
164+
})
136165
})
137166

138167
Describe("CreateOrUpdate", func() {

0 commit comments

Comments
 (0)