Skip to content

Commit ce6bd39

Browse files
authored
Merge pull request #707 from sebgl/ignore-controllerref-version
🐛 Fix SetControllerReference to consider multiple CRD versions
2 parents 8f86004 + 0d1f458 commit ce6bd39

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pkg/controller/controllerutil/controllerutil.go

Lines changed: 1 addition & 1 deletion
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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,34 @@ var _ = Describe("Controllerutil", func() {
133133

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

138166
Describe("CreateOrUpdate", func() {

0 commit comments

Comments
 (0)