Skip to content

Commit

Permalink
Update APIVersion for bootstrap references for Machine* resources upo…
Browse files Browse the repository at this point in the history
…n v2 to v3 conversion
  • Loading branch information
neeleshkorade committed Feb 5, 2020
1 parent 12ddd37 commit 2278298
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 14 deletions.
43 changes: 43 additions & 0 deletions api/v1alpha2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
45 changes: 41 additions & 4 deletions api/v1alpha2/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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()))
})
})

Expand Down Expand Up @@ -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()))
})
})

Expand Down Expand Up @@ -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()))
})
})

Expand All @@ -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())
Expand Down
20 changes: 10 additions & 10 deletions api/v1alpha2/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2278298

Please sign in to comment.