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