Skip to content

Commit a599682

Browse files
committed
Adapt consumerRef/ownerRef checks to controller-runtime changes
Compare ownerRef/consumerRef groupversions, not TypeMeta, since controller runtime changes here https://github.com/kubernetes-sigs/controller-runtime/pull/3229wq Signed-off-by: Sunnatillo <[email protected]>
1 parent ef5aaa5 commit a599682

File tree

2 files changed

+57
-38
lines changed

2 files changed

+57
-38
lines changed

baremetal/metal3machine_manager.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,12 @@ func (m *MachineManager) Delete(ctx context.Context) error {
433433

434434
if host.Spec.ConsumerRef != nil {
435435
// don't remove the ConsumerRef if it references some other metal3 machine
436-
if !consumerRefMatches(host.Spec.ConsumerRef, m.Metal3Machine) {
436+
ok, err := consumerRefMatches(host.Spec.ConsumerRef, m.Metal3Machine)
437+
if err != nil {
438+
m.Log.Info("ConsumerRef APIVersion field is malformed, consumerRef: ", host.Spec.ConsumerRef.Name)
439+
return err
440+
}
441+
if !ok {
437442
m.Log.Info("host already associated with another metal3 machine",
438443
"host", host.Name)
439444
// Remove the ownerreference to this machine, even if the consumer ref
@@ -783,7 +788,12 @@ func (m *MachineManager) chooseHost(ctx context.Context) (*bmov1alpha1.BareMetal
783788
availableHostsWithNodeReuse := []*bmov1alpha1.BareMetalHost{}
784789

785790
for i, host := range hosts.Items {
786-
if host.Spec.ConsumerRef != nil && consumerRefMatches(host.Spec.ConsumerRef, m.Metal3Machine) {
791+
ok, err := consumerRefMatches(host.Spec.ConsumerRef, m.Metal3Machine)
792+
if err != nil {
793+
m.Log.Info("ConsumerRef APIVersion field is malformed, consumerRef: ", host.Spec.ConsumerRef.Name)
794+
continue
795+
}
796+
if host.Spec.ConsumerRef != nil && ok {
787797
var helper *v1beta1patch.Helper
788798
m.Log.Info("Found host with existing ConsumerRef", "host", host.Name)
789799
helper, err = v1beta1patch.NewHelper(&hosts.Items[i], m.client)
@@ -916,20 +926,29 @@ func hostLabelSelectorForMachine(machine *infrav1.Metal3Machine, log logr.Logger
916926

917927
// consumerRefMatches returns a boolean based on whether the consumer
918928
// reference and bare metal machine metadata match.
919-
func consumerRefMatches(consumer *corev1.ObjectReference, m3machine *infrav1.Metal3Machine) bool {
929+
func consumerRefMatches(consumer *corev1.ObjectReference, m3machine *infrav1.Metal3Machine) (bool, error) {
930+
if m3machine == nil || consumer == nil {
931+
return false, nil
932+
}
920933
if consumer.Name != m3machine.Name {
921-
return false
934+
return false, nil
922935
}
923936
if consumer.Namespace != m3machine.Namespace {
924-
return false
937+
return false, nil
925938
}
926-
if consumer.Kind != m3machine.Kind {
927-
return false
939+
if consumer.Kind != "Metal3Machine" {
940+
return false, nil
928941
}
929-
if consumer.GroupVersionKind().Group != m3machine.GroupVersionKind().Group {
930-
return false
942+
943+
// Parse and compare Group from consumer APIVersion
944+
gv, err := schema.ParseGroupVersion(consumer.APIVersion)
945+
if err != nil {
946+
return false, err
931947
}
932-
return true
948+
if gv.Group != infrav1.GroupVersion.Group {
949+
return false, nil
950+
}
951+
return true, nil
933952
}
934953

935954
// nodeReuseLabelMatches returns true if nodeReuseLabelName matches ControlPlane or MachineDeployment name on the host.
@@ -1855,10 +1874,10 @@ func (m *MachineManager) getMachineSet(ctx context.Context) (*clusterv1.MachineS
18551874
for index := range machineSets.Items {
18561875
machineset := &machineSets.Items[index]
18571876
for _, mOwnerRef := range m.Machine.ObjectMeta.OwnerReferences {
1858-
if mOwnerRef.Kind != machineset.Kind {
1877+
if mOwnerRef.Kind != "MachineSet" {
18591878
continue
18601879
}
1861-
if mOwnerRef.APIVersion != machineset.APIVersion {
1880+
if mOwnerRef.APIVersion != clusterv1.GroupVersion.String() {
18621881
machineSetError += fmt.Sprintf("MachineSet %s has different API version %s than Machine %s with API version %s",
18631882
machineset.Name, machineset.APIVersion, m.Machine.Name, mOwnerRef.APIVersion)
18641883
continue

baremetal/metal3machine_manager_test.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func consumerRef() *corev1.ObjectReference {
106106
return &corev1.ObjectReference{
107107
Name: metal3machineName,
108108
Namespace: namespaceName,
109-
Kind: "M3Machine",
109+
Kind: "Metal3Machine",
110110
APIVersion: infrav1.GroupVersion.String(),
111111
}
112112
}
@@ -115,7 +115,7 @@ func consumerRefSome() *corev1.ObjectReference {
115115
return &corev1.ObjectReference{
116116
Name: "someoneelsesmachine",
117117
Namespace: namespaceName,
118-
Kind: "M3Machine",
118+
Kind: "Metal3Machine",
119119
APIVersion: clusterv1beta1.GroupVersion.String(),
120120
}
121121
}
@@ -515,7 +515,7 @@ var _ = Describe("Metal3Machine manager", func() {
515515
ConsumerRef: &corev1.ObjectReference{
516516
Name: "someothermachine",
517517
Namespace: namespaceName,
518-
Kind: "M3Machine",
518+
Kind: "Metal3Machine",
519519
APIVersion: infrav1.GroupVersion.String(),
520520
},
521521
},
@@ -532,7 +532,7 @@ var _ = Describe("Metal3Machine manager", func() {
532532
ConsumerRef: &corev1.ObjectReference{
533533
Name: metal3machineName,
534534
Namespace: namespaceName,
535-
Kind: "M3Machine",
535+
Kind: "Metal3Machine",
536536
APIVersion: infrav1.GroupVersion.String(),
537537
},
538538
},
@@ -1010,7 +1010,7 @@ var _ = Describe("Metal3Machine manager", func() {
10101010
ConsumerRef: &corev1.ObjectReference{
10111011
Name: metal3machineName,
10121012
Namespace: namespaceName,
1013-
Kind: "M3Machine",
1013+
Kind: "Metal3Machine",
10141014
APIVersion: infrav1.GroupVersion.String(),
10151015
},
10161016
},
@@ -1027,7 +1027,7 @@ var _ = Describe("Metal3Machine manager", func() {
10271027
ConsumerRef: &corev1.ObjectReference{
10281028
Name: metal3machineName,
10291029
Namespace: namespaceName,
1030-
Kind: "M3Machine",
1030+
Kind: "Metal3Machine",
10311031
APIVersion: infrav1.GroupVersion.String(),
10321032
},
10331033
},
@@ -1044,7 +1044,7 @@ var _ = Describe("Metal3Machine manager", func() {
10441044
ConsumerRef: &corev1.ObjectReference{
10451045
Name: metal3machineName,
10461046
Namespace: namespaceName,
1047-
Kind: "M3Machine",
1047+
Kind: "Metal3Machine",
10481048
APIVersion: infrav1.GroupVersion.String(),
10491049
},
10501050
},
@@ -1113,7 +1113,7 @@ var _ = Describe("Metal3Machine manager", func() {
11131113
ConsumerRef: &corev1.ObjectReference{
11141114
Name: metal3machineName,
11151115
Namespace: namespaceName,
1116-
Kind: "M3Machine",
1116+
Kind: "Metal3Machine",
11171117
APIVersion: infrav1.GroupVersion.String(),
11181118
},
11191119
},
@@ -1131,7 +1131,7 @@ var _ = Describe("Metal3Machine manager", func() {
11311131
ConsumerRef: &corev1.ObjectReference{
11321132
Name: metal3machineName,
11331133
Namespace: namespaceName,
1134-
Kind: "M3Machine",
1134+
Kind: "Metal3Machine",
11351135
APIVersion: infrav1.GroupVersion.String(),
11361136
},
11371137
},
@@ -1149,7 +1149,7 @@ var _ = Describe("Metal3Machine manager", func() {
11491149
ConsumerRef: &corev1.ObjectReference{
11501150
Name: metal3machineName,
11511151
Namespace: namespaceName,
1152-
Kind: "M3Machine",
1152+
Kind: "Metal3Machine",
11531153
APIVersion: infrav1.GroupVersion.String(),
11541154
},
11551155
},
@@ -2358,7 +2358,7 @@ var _ = Describe("Metal3Machine manager", func() {
23582358
Namespace: namespaceName,
23592359
},
23602360
TypeMeta: metav1.TypeMeta{
2361-
Kind: "M3Machine",
2361+
Kind: "Metal3Machine",
23622362
APIVersion: clusterv1beta1.GroupVersion.String(),
23632363
},
23642364
Status: infrav1.Metal3MachineStatus{
@@ -2422,7 +2422,7 @@ var _ = Describe("Metal3Machine manager", func() {
24222422
Namespace: namespaceName,
24232423
},
24242424
TypeMeta: metav1.TypeMeta{
2425-
Kind: "M3Machine",
2425+
Kind: "Metal3Machine",
24262426
APIVersion: clusterv1beta1.GroupVersion.String(),
24272427
},
24282428
Status: infrav1.Metal3MachineStatus{
@@ -2470,7 +2470,7 @@ var _ = Describe("Metal3Machine manager", func() {
24702470
Namespace: namespaceName,
24712471
},
24722472
TypeMeta: metav1.TypeMeta{
2473-
Kind: "M3Machine",
2473+
Kind: "Metal3Machine",
24742474
APIVersion: clusterv1beta1.GroupVersion.String(),
24752475
},
24762476
Status: infrav1.Metal3MachineStatus{
@@ -3124,7 +3124,7 @@ var _ = Describe("Metal3Machine manager", func() {
31243124
M3Machine: *newMetal3Machine("myName", nil, nil, nil),
31253125
OwnerRefs: []metav1.OwnerReference{
31263126
{
3127-
Kind: "M3Machine",
3127+
Kind: "Metal3Machine",
31283128
APIVersion: infrav1.GroupVersion.String(),
31293129
Name: "myName",
31303130
UID: "adfasdf",
@@ -3149,7 +3149,7 @@ var _ = Describe("Metal3Machine manager", func() {
31493149
UID: "adfasdf",
31503150
},
31513151
{
3152-
Kind: "M3Machine",
3152+
Kind: "Metal3Machine",
31533153
APIVersion: infrav1.GroupVersion.String(),
31543154
Name: "myName",
31553155
UID: "adfasdf",
@@ -3168,7 +3168,7 @@ var _ = Describe("Metal3Machine manager", func() {
31683168
UID: "adfasdf",
31693169
},
31703170
{
3171-
Kind: "M3Machine",
3171+
Kind: "Metal3Machine",
31723172
APIVersion: "infrastructure.cluster.x-k8s.io/v1alpha1",
31733173
Name: "myName",
31743174
UID: "adfasdf",
@@ -3187,7 +3187,7 @@ var _ = Describe("Metal3Machine manager", func() {
31873187
UID: "adfasdf",
31883188
},
31893189
{
3190-
Kind: "M3Machine",
3190+
Kind: "Metal3Machine",
31913191
APIVersion: "nfrastructure.cluster.x-k8s.io/v1alpha1",
31923192
Name: "myName",
31933193
UID: "adfasdf",
@@ -3234,7 +3234,7 @@ var _ = Describe("Metal3Machine manager", func() {
32343234
M3Machine: *newMetal3Machine("myName", nil, nil, nil),
32353235
OwnerRefs: []metav1.OwnerReference{
32363236
{
3237-
Kind: "M3Machine",
3237+
Kind: "Metal3Machine",
32383238
APIVersion: infrav1.GroupVersion.String(),
32393239
Name: "myName",
32403240
UID: "adfasdf",
@@ -3257,7 +3257,7 @@ var _ = Describe("Metal3Machine manager", func() {
32573257
UID: "adfasdf",
32583258
},
32593259
{
3260-
Kind: "M3Machine",
3260+
Kind: "Metal3Machine",
32613261
APIVersion: infrav1.GroupVersion.String(),
32623262
Name: "myName",
32633263
UID: "adfasdf",
@@ -3268,7 +3268,7 @@ var _ = Describe("Metal3Machine manager", func() {
32683268
M3Machine: *newMetal3Machine("myName", nil, nil, nil),
32693269
OwnerRefs: []metav1.OwnerReference{
32703270
{
3271-
Kind: "M3Machine",
3271+
Kind: "Metal3Machine",
32723272
APIVersion: infrav1.GroupVersion.String(),
32733273
Name: "myName",
32743274
UID: "adfasdf",
@@ -3309,7 +3309,7 @@ var _ = Describe("Metal3Machine manager", func() {
33093309
M3Machine: *newMetal3Machine("myName", nil, nil, nil),
33103310
OwnerRefs: []metav1.OwnerReference{
33113311
{
3312-
Kind: "M3Machine",
3312+
Kind: "Metal3Machine",
33133313
APIVersion: infrav1.GroupVersion.String(),
33143314
Name: "myName",
33153315
UID: "adfasdf",
@@ -3332,7 +3332,7 @@ var _ = Describe("Metal3Machine manager", func() {
33323332
UID: "adfasdf",
33333333
},
33343334
{
3335-
Kind: "M3Machine",
3335+
Kind: "Metal3Machine",
33363336
APIVersion: infrav1.GroupVersion.String(),
33373337
Name: "myName",
33383338
UID: "adfasdf",
@@ -3429,7 +3429,7 @@ var _ = Describe("Metal3Machine manager", func() {
34293429
OwnerReferences: []metav1.OwnerReference{
34303430
{
34313431
Name: "myName",
3432-
Kind: "M3Machine",
3432+
Kind: "Metal3Machine",
34333433
APIVersion: infrav1.GroupVersion.String(),
34343434
},
34353435
},
@@ -3457,7 +3457,7 @@ var _ = Describe("Metal3Machine manager", func() {
34573457
OwnerReferences: []metav1.OwnerReference{
34583458
{
34593459
Name: "myName",
3460-
Kind: "M3Machine",
3460+
Kind: "Metal3Machine",
34613461
APIVersion: infrav1.GroupVersion.String(),
34623462
},
34633463
},
@@ -4711,7 +4711,7 @@ func newConfig(userDataNamespace string,
47114711

47124712
infrastructureRef := &clusterv1.ContractVersionedObjectReference{
47134713
Name: "someothermachine",
4714-
Kind: "M3Machine",
4714+
Kind: "Metal3Machine",
47154715
APIGroup: infrav1.GroupVersion.Group,
47164716
}
47174717
return &config, infrastructureRef
@@ -4763,7 +4763,7 @@ func newMetal3Machine(name string,
47634763
}
47644764

47654765
typeMeta := &metav1.TypeMeta{
4766-
Kind: "M3Machine",
4766+
Kind: "Metal3Machine",
47674767
APIVersion: infrav1.GroupVersion.String(),
47684768
}
47694769

0 commit comments

Comments
 (0)