diff --git a/api/v1alpha2/conversion.go b/api/v1alpha2/conversion.go index 1d7e274fdf16..fd6377181ed9 100644 --- a/api/v1alpha2/conversion.go +++ b/api/v1alpha2/conversion.go @@ -19,10 +19,16 @@ package v1alpha2 import ( apiconversion "k8s.io/apimachinery/pkg/conversion" "sigs.k8s.io/cluster-api/api/v1alpha3" + bootstrapv1a3 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3" utilconversion "sigs.k8s.io/cluster-api/util/conversion" "sigs.k8s.io/controller-runtime/pkg/conversion" ) +const ( + kubeadmConfigKind = "KubeadmConfig" + kubeadmConfigTemplateKind = "KubeadmConfigTemplate" +) + func (src *Cluster) ConvertTo(dstRaw conversion.Hub) error { dst := dstRaw.(*v1alpha3.Cluster) if err := Convert_v1alpha2_Cluster_To_v1alpha3_Cluster(src, dst, nil); err != nil { @@ -267,11 +273,48 @@ func Convert_v1alpha2_MachineSpec_To_v1alpha3_MachineSpec(in *MachineSpec, out * return err } + if in.Bootstrap.ConfigRef != nil { + gvk := in.Bootstrap.ConfigRef.GetObjectKind().GroupVersionKind() + if gvk.Group == bootstrapv1a3.GroupVersion.Group && gvk.Kind == kubeadmConfigKind { + out.Bootstrap.ConfigRef.APIVersion = bootstrapv1a3.GroupVersion.String() + } + } + // Discards unused ObjectMeta return nil } +func Convert_v1alpha2_MachineSetSpec_To_v1alpha3_MachineSetSpec(in *MachineSetSpec, out *v1alpha3.MachineSetSpec, s apiconversion.Scope) error { + if err := autoConvert_v1alpha2_MachineSetSpec_To_v1alpha3_MachineSetSpec(in, out, s); err != nil { + return err + } + + if in.Template.Spec.Bootstrap.ConfigRef != nil { + gvk := in.Template.Spec.Bootstrap.ConfigRef.GetObjectKind().GroupVersionKind() + if gvk.Group == bootstrapv1a3.GroupVersion.Group && gvk.Kind == kubeadmConfigTemplateKind { + out.Template.Spec.Bootstrap.ConfigRef.APIVersion = bootstrapv1a3.GroupVersion.String() + } + } + + return nil +} + +func Convert_v1alpha2_MachineDeploymentSpec_To_v1alpha3_MachineDeploymentSpec(in *MachineDeploymentSpec, out *v1alpha3.MachineDeploymentSpec, s apiconversion.Scope) error { + if err := autoConvert_v1alpha2_MachineDeploymentSpec_To_v1alpha3_MachineDeploymentSpec(in, out, s); err != nil { + return err + } + + if in.Template.Spec.Bootstrap.ConfigRef != nil { + gvk := in.Template.Spec.Bootstrap.ConfigRef.GetObjectKind().GroupVersionKind() + if gvk.Group == bootstrapv1a3.GroupVersion.Group && gvk.Kind == kubeadmConfigTemplateKind { + out.Template.Spec.Bootstrap.ConfigRef.APIVersion = bootstrapv1a3.GroupVersion.String() + } + } + + return nil +} + func Convert_v1alpha2_ClusterSpec_To_v1alpha3_ClusterSpec(in *ClusterSpec, out *v1alpha3.ClusterSpec, s apiconversion.Scope) error { if err := autoConvert_v1alpha2_ClusterSpec_To_v1alpha3_ClusterSpec(in, out, s); err != nil { return err diff --git a/api/v1alpha2/conversion_test.go b/api/v1alpha2/conversion_test.go index 5f41076af3d2..cfd8c08c1984 100644 --- a/api/v1alpha2/conversion_test.go +++ b/api/v1alpha2/conversion_test.go @@ -25,6 +25,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/utils/pointer" "sigs.k8s.io/cluster-api/api/v1alpha3" + bootstrapv1a2 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha2" + bootstrapv1a3 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3" utilconversion "sigs.k8s.io/cluster-api/util/conversion" ) @@ -112,18 +114,27 @@ func TestConvertMachine(t *testing.T) { g := NewWithT(t) t.Run("to hub", func(t *testing.T) { - t.Run("should convert the Spec.ClusterName from label", func(t *testing.T) { + t.Run("should convert all fields correctly", func(t *testing.T) { src := &Machine{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ MachineClusterLabelName: "test-cluster", }, }, + Spec: MachineSpec{ + Bootstrap: Bootstrap{ + ConfigRef: &corev1.ObjectReference{ + Kind: kubeadmConfigKind, + APIVersion: bootstrapv1a2.GroupVersion.String(), + }, + }, + }, } dst := &v1alpha3.Machine{} g.Expect(src.ConvertTo(dst)).To(Succeed()) g.Expect(dst.Spec.ClusterName).To(Equal("test-cluster")) + g.Expect(dst.Spec.Bootstrap.ConfigRef.APIVersion).To(Equal(bootstrapv1a3.GroupVersion.String())) }) }) @@ -161,19 +172,32 @@ func TestConvertMachineSet(t *testing.T) { g := NewWithT(t) t.Run("to hub", func(t *testing.T) { - t.Run("should convert the Spec.ClusterName from label", func(t *testing.T) { + t.Run("should convert all fields correctly", func(t *testing.T) { src := &MachineSet{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ MachineClusterLabelName: "test-cluster", }, }, + Spec: MachineSetSpec{ + Template: MachineTemplateSpec{ + Spec: MachineSpec{ + Bootstrap: Bootstrap{ + ConfigRef: &corev1.ObjectReference{ + Kind: kubeadmConfigKind, + APIVersion: bootstrapv1a2.GroupVersion.String(), + }, + }, + }, + }, + }, } dst := &v1alpha3.MachineSet{} g.Expect(src.ConvertTo(dst)).To(Succeed()) g.Expect(dst.Spec.ClusterName).To(Equal("test-cluster")) g.Expect(dst.Spec.Template.Spec.ClusterName).To(Equal("test-cluster")) + g.Expect(dst.Spec.Template.Spec.Bootstrap.ConfigRef.APIVersion).To(Equal(bootstrapv1a3.GroupVersion.String())) }) }) @@ -210,19 +234,33 @@ func TestConvertMachineDeployment(t *testing.T) { g := NewWithT(t) t.Run("to hub", func(t *testing.T) { - t.Run("should convert the Spec.ClusterName from label", func(t *testing.T) { + t.Run("should convert all fields correctly", func(t *testing.T) { src := &MachineDeployment{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{ MachineClusterLabelName: "test-cluster", }, }, + Spec: MachineDeploymentSpec{ + Paused: true, + Template: MachineTemplateSpec{ + Spec: MachineSpec{ + Bootstrap: Bootstrap{ + ConfigRef: &corev1.ObjectReference{ + Kind: kubeadmConfigTemplateKind, + APIVersion: bootstrapv1a2.GroupVersion.String(), + }, + }, + }, + }, + }, } dst := &v1alpha3.MachineDeployment{} g.Expect(src.ConvertTo(dst)).To(Succeed()) g.Expect(dst.Spec.ClusterName).To(Equal("test-cluster")) g.Expect(dst.Spec.Template.Spec.ClusterName).To(Equal("test-cluster")) + g.Expect(dst.Spec.Template.Spec.Bootstrap.ConfigRef.APIVersion).To(Equal(bootstrapv1a3.GroupVersion.String())) }) }) @@ -244,7 +282,6 @@ func TestConvertMachineDeployment(t *testing.T) { } src.Status.SetTypedPhase(v1alpha3.MachineDeploymentPhaseRunning) dst := &MachineDeployment{} - g.Expect(dst.ConvertFrom(src)).To(Succeed()) restored := &v1alpha3.MachineDeployment{} g.Expect(dst.ConvertTo(restored)).To(Succeed()) diff --git a/api/v1alpha2/zz_generated.conversion.go b/api/v1alpha2/zz_generated.conversion.go index 12cb30171423..51c45de399a7 100644 --- a/api/v1alpha2/zz_generated.conversion.go +++ b/api/v1alpha2/zz_generated.conversion.go @@ -228,6 +228,16 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } + if err := s.AddConversionFunc((*MachineDeploymentSpec)(nil), (*v1alpha3.MachineDeploymentSpec)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1alpha2_MachineDeploymentSpec_To_v1alpha3_MachineDeploymentSpec(a.(*MachineDeploymentSpec), b.(*v1alpha3.MachineDeploymentSpec), scope) + }); err != nil { + return err + } + if err := s.AddConversionFunc((*MachineSetSpec)(nil), (*v1alpha3.MachineSetSpec)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1alpha2_MachineSetSpec_To_v1alpha3_MachineSetSpec(a.(*MachineSetSpec), b.(*v1alpha3.MachineSetSpec), scope) + }); err != nil { + return err + } if err := s.AddConversionFunc((*MachineSetStatus)(nil), (*v1alpha3.MachineSetStatus)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1alpha2_MachineSetStatus_To_v1alpha3_MachineSetStatus(a.(*MachineSetStatus), b.(*v1alpha3.MachineSetStatus), scope) }); err != nil { @@ -609,11 +619,6 @@ func autoConvert_v1alpha2_MachineDeploymentSpec_To_v1alpha3_MachineDeploymentSpe return nil } -// Convert_v1alpha2_MachineDeploymentSpec_To_v1alpha3_MachineDeploymentSpec is an autogenerated conversion function. -func Convert_v1alpha2_MachineDeploymentSpec_To_v1alpha3_MachineDeploymentSpec(in *MachineDeploymentSpec, out *v1alpha3.MachineDeploymentSpec, s conversion.Scope) error { - return autoConvert_v1alpha2_MachineDeploymentSpec_To_v1alpha3_MachineDeploymentSpec(in, out, s) -} - func autoConvert_v1alpha3_MachineDeploymentSpec_To_v1alpha2_MachineDeploymentSpec(in *v1alpha3.MachineDeploymentSpec, out *MachineDeploymentSpec, s conversion.Scope) error { // WARNING: in.ClusterName requires manual conversion: does not exist in peer-type out.Replicas = (*int32)(unsafe.Pointer(in.Replicas)) @@ -828,11 +833,6 @@ func autoConvert_v1alpha2_MachineSetSpec_To_v1alpha3_MachineSetSpec(in *MachineS return nil } -// Convert_v1alpha2_MachineSetSpec_To_v1alpha3_MachineSetSpec is an autogenerated conversion function. -func Convert_v1alpha2_MachineSetSpec_To_v1alpha3_MachineSetSpec(in *MachineSetSpec, out *v1alpha3.MachineSetSpec, s conversion.Scope) error { - return autoConvert_v1alpha2_MachineSetSpec_To_v1alpha3_MachineSetSpec(in, out, s) -} - func autoConvert_v1alpha3_MachineSetSpec_To_v1alpha2_MachineSetSpec(in *v1alpha3.MachineSetSpec, out *MachineSetSpec, s conversion.Scope) error { // WARNING: in.ClusterName requires manual conversion: does not exist in peer-type out.Replicas = (*int32)(unsafe.Pointer(in.Replicas))