@@ -19,6 +19,7 @@ package cluster
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "strings"
22
23
23
24
"github.com/pkg/errors"
24
25
corev1 "k8s.io/api/core/v1"
@@ -84,7 +85,7 @@ func (r *Reconciler) getCurrentState(ctx context.Context, s *scope.Scope) (*scop
84
85
// getCurrentInfrastructureClusterState looks for the state of the InfrastructureCluster. If a reference is set but not
85
86
// found, either from an error or the object not being found, an error is thrown.
86
87
func (r * Reconciler ) getCurrentInfrastructureClusterState (ctx context.Context , blueprintInfrastructureClusterTemplate * unstructured.Unstructured , cluster * clusterv1.Cluster ) (* unstructured.Unstructured , error ) {
87
- ref , err := alignRefAPIVersion (blueprintInfrastructureClusterTemplate , cluster .Spec .InfrastructureRef )
88
+ ref , err := alignRefAPIVersion (blueprintInfrastructureClusterTemplate , cluster .Spec .InfrastructureRef , false )
88
89
if err != nil {
89
90
return nil , errors .Wrapf (err , "failed to read %s %s" , cluster .Spec .InfrastructureRef .Kind , klog .KRef (cluster .Spec .InfrastructureRef .Namespace , cluster .Spec .InfrastructureRef .Name ))
90
91
}
@@ -109,7 +110,7 @@ func (r *Reconciler) getCurrentControlPlaneState(ctx context.Context, blueprintC
109
110
res := & scope.ControlPlaneState {}
110
111
111
112
// Get the control plane object.
112
- ref , err := alignRefAPIVersion (blueprintControlPlane .Template , cluster .Spec .ControlPlaneRef )
113
+ ref , err := alignRefAPIVersion (blueprintControlPlane .Template , cluster .Spec .ControlPlaneRef , false )
113
114
if err != nil {
114
115
return nil , errors .Wrapf (err , "failed to read %s %s" , cluster .Spec .ControlPlaneRef .Kind , klog .KRef (cluster .Spec .ControlPlaneRef .Namespace , cluster .Spec .ControlPlaneRef .Name ))
115
116
}
@@ -134,7 +135,7 @@ func (r *Reconciler) getCurrentControlPlaneState(ctx context.Context, blueprintC
134
135
if err != nil {
135
136
return res , errors .Wrapf (err , "failed to get InfrastructureMachineTemplate reference for %s %s" , res .Object .GetKind (), klog .KObj (res .Object ))
136
137
}
137
- ref , err = alignRefAPIVersion (blueprintControlPlane .InfrastructureMachineTemplate , machineInfrastructureRef )
138
+ ref , err = alignRefAPIVersion (blueprintControlPlane .InfrastructureMachineTemplate , machineInfrastructureRef , true )
138
139
if err != nil {
139
140
return nil , errors .Wrapf (err , "failed to get InfrastructureMachineTemplate for %s %s" , res .Object .GetKind (), klog .KObj (res .Object ))
140
141
}
@@ -229,11 +230,11 @@ func (r *Reconciler) getCurrentMachineDeploymentState(ctx context.Context, bluep
229
230
if ! ok {
230
231
return nil , fmt .Errorf ("failed to find MachineDeployment class %s in ClusterClass" , mdClassName )
231
232
}
232
- bootstrapRef , err = alignRefAPIVersion (mdBluePrint .BootstrapTemplate , bootstrapRef )
233
+ bootstrapRef , err = alignRefAPIVersion (mdBluePrint .BootstrapTemplate , bootstrapRef , true )
233
234
if err != nil {
234
235
return nil , errors .Wrap (err , fmt .Sprintf ("MachineDeployment %s Bootstrap reference could not be retrieved" , klog .KObj (m )))
235
236
}
236
- infraRef , err = alignRefAPIVersion (mdBluePrint .InfrastructureMachineTemplate , infraRef )
237
+ infraRef , err = alignRefAPIVersion (mdBluePrint .InfrastructureMachineTemplate , infraRef , true )
237
238
if err != nil {
238
239
return nil , errors .Wrap (err , fmt .Sprintf ("MachineDeployment %s Infrastructure reference could not be retrieved" , klog .KObj (m )))
239
240
}
@@ -349,11 +350,11 @@ func (r *Reconciler) getCurrentMachinePoolState(ctx context.Context, blueprintMa
349
350
if ! ok {
350
351
return nil , fmt .Errorf ("failed to find MachinePool class %s in ClusterClass" , mpClassName )
351
352
}
352
- bootstrapRef , err = alignRefAPIVersion (mpBluePrint .BootstrapTemplate , bootstrapRef )
353
+ bootstrapRef , err = alignRefAPIVersion (mpBluePrint .BootstrapTemplate , bootstrapRef , false )
353
354
if err != nil {
354
355
return nil , errors .Wrap (err , fmt .Sprintf ("MachinePool %s Bootstrap reference could not be retrieved" , klog .KObj (m )))
355
356
}
356
- infraRef , err = alignRefAPIVersion (mpBluePrint .InfrastructureMachinePoolTemplate , infraRef )
357
+ infraRef , err = alignRefAPIVersion (mpBluePrint .InfrastructureMachinePoolTemplate , infraRef , false )
357
358
if err != nil {
358
359
return nil , errors .Wrap (err , fmt .Sprintf ("MachinePool %s Infrastructure reference could not be retrieved" , klog .KObj (m )))
359
360
}
@@ -398,17 +399,27 @@ func (r *Reconciler) getCurrentMachinePoolState(ctx context.Context, blueprintMa
398
399
// If group or kind was changed in the ClusterClass, an exact copy of the currentRef is returned because
399
400
// it will end up in a diff and a rollout anyway.
400
401
// Only bootstrap template refs in a ClusterClass can change their group and kind.
401
- func alignRefAPIVersion (templateFromClusterClass * unstructured.Unstructured , currentRef * corev1.ObjectReference ) (* corev1.ObjectReference , error ) {
402
+ func alignRefAPIVersion (templateFromClusterClass * unstructured.Unstructured , currentRef * corev1.ObjectReference , isCurrentTemplate bool ) (* corev1.ObjectReference , error ) {
402
403
currentGV , err := schema .ParseGroupVersion (currentRef .APIVersion )
403
404
if err != nil {
404
405
return nil , errors .Wrapf (err , "failed to parse apiVersion: %q" , currentRef .APIVersion )
405
406
}
406
407
407
408
apiVersion := currentRef .APIVersion
408
409
// Use apiVersion from ClusterClass if group and kind is the same.
409
- if templateFromClusterClass .GroupVersionKind ().Group == currentGV .Group &&
410
- templateFromClusterClass .GetKind () == currentRef .Kind {
411
- apiVersion = templateFromClusterClass .GetAPIVersion ()
410
+ if templateFromClusterClass .GroupVersionKind ().Group == currentGV .Group {
411
+ if isCurrentTemplate {
412
+ // If the current object is a template, kind has to be identical.
413
+ if templateFromClusterClass .GetKind () == currentRef .Kind {
414
+ apiVersion = templateFromClusterClass .GetAPIVersion ()
415
+ }
416
+ } else {
417
+ // If the current object is not a template, currentRef.Kind should be the kind from CC without the Template suffix,
418
+ // e.g. KubeadmControlPlaneTemplate == KubeadmControlPlane
419
+ if strings .TrimSuffix (templateFromClusterClass .GetKind (), "Template" ) == currentRef .Kind {
420
+ apiVersion = templateFromClusterClass .GetAPIVersion ()
421
+ }
422
+ }
412
423
}
413
424
414
425
return & corev1.ObjectReference {
0 commit comments