Skip to content

Commit

Permalink
Fix v1alpha4 <-> v1beta1 conversions
Browse files Browse the repository at this point in the history
v1alpha4 <-> v1beta1 conversions were failing and never tested.

As part of the PR
- Add a missing gcpclustertemplate_conversion.go file
- Add conversion test to make sure conversions work
- Add ObjectMeta to GCPClusterTemplateResource and GCPMachineTemplateResource.
  See kubernetes-sigs/cluster-api#5240
  • Loading branch information
Winnie Kwon committed Mar 21, 2022
1 parent d845a39 commit 2087990
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 50 deletions.
6 changes: 6 additions & 0 deletions api/v1alpha3/gcpmachinetemplate_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha3

import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"

Expand Down Expand Up @@ -60,3 +61,8 @@ func (dst *GCPMachineTemplateList) ConvertFrom(srcRaw conversion.Hub) error { //
src := srcRaw.(*infrav1beta1.GCPMachineTemplateList)
return Convert_v1beta1_GCPMachineTemplateList_To_v1alpha3_GCPMachineTemplateList(src, dst, nil)
}

func Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha3_GCPMachineTemplateResource(in *infrav1beta1.GCPMachineTemplateResource, out *GCPMachineTemplateResource, s apiconversion.Scope) error {
// NOTE: custom conversion func is required because spec.template.metadata has been added in v1beta1.
return autoConvert_v1beta1_GCPMachineTemplateResource_To_v1alpha3_GCPMachineTemplateResource(in, out, s)
}
40 changes: 28 additions & 12 deletions api/v1alpha3/zz_generated.conversion.go

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

46 changes: 46 additions & 0 deletions api/v1alpha4/conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha4

import (
"testing"

"sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

func TestFuzzyConversion(t *testing.T) {
t.Run("for GCPCluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &v1beta1.GCPCluster{},
Spoke: &GCPCluster{},
}))

t.Run("for GCPClusterTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &v1beta1.GCPClusterTemplate{},
Spoke: &GCPClusterTemplate{},
}))

t.Run("for GCPMachine", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &v1beta1.GCPMachine{},
Spoke: &GCPMachine{},
}))

t.Run("for GCPMachineTemplate", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &v1beta1.GCPMachineTemplate{},
Spoke: &GCPMachineTemplate{},
}))
}
75 changes: 75 additions & 0 deletions api/v1alpha4/gcpclustertemplate_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha4

import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
infrav1beta1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// ConvertTo converts this GCPClusterTemplate to the Hub version (v1beta1).
func (src *GCPClusterTemplate) ConvertTo(dstRaw conversion.Hub) error { // nolint
dst := dstRaw.(*infrav1beta1.GCPClusterTemplate)

if err := Convert_v1alpha4_GCPClusterTemplate_To_v1beta1_GCPClusterTemplate(src, dst, nil); err != nil {
return err
}

// Manually restore data.
restored := &infrav1beta1.GCPClusterTemplate{}
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
return err
}

dst.Spec.Template.ObjectMeta = restored.Spec.Template.ObjectMeta

return nil
}

// ConvertFrom converts from the Hub version (v1beta1) to this version.
func (dst *GCPClusterTemplate) ConvertFrom(srcRaw conversion.Hub) error { // nolint
src := srcRaw.(*infrav1beta1.GCPClusterTemplate)
if err := Convert_v1beta1_GCPClusterTemplate_To_v1alpha4_GCPClusterTemplate(src, dst, nil); err != nil {
return err
}

// Preserve Hub data on down-conversion.
if err := utilconversion.MarshalData(src, dst); err != nil {
return err
}

return nil
}

// ConvertTo converts this GCPClusterTemplateList to the Hub version (v1beta1).
func (src *GCPClusterTemplateList) ConvertTo(dstRaw conversion.Hub) error { // nolint
dst := dstRaw.(*infrav1beta1.GCPClusterTemplateList)
return Convert_v1alpha4_GCPClusterTemplateList_To_v1beta1_GCPClusterTemplateList(src, dst, nil)
}

// ConvertFrom converts from the Hub version (v1beta1) to this version.
func (dst *GCPClusterTemplateList) ConvertFrom(srcRaw conversion.Hub) error { // nolint
src := srcRaw.(*infrav1beta1.GCPClusterTemplateList)
return Convert_v1beta1_GCPClusterTemplateList_To_v1alpha4_GCPClusterTemplateList(src, dst, nil)
}

func Convert_v1beta1_GCPClusterTemplateResource_To_v1alpha4_GCPClusterTemplateResource(in *infrav1beta1.GCPClusterTemplateResource, out *GCPClusterTemplateResource, s apiconversion.Scope) error {
// NOTE: custom conversion func is required because spec.template.metadata has been added in v1beta1.
return autoConvert_v1beta1_GCPClusterTemplateResource_To_v1alpha4_GCPClusterTemplateResource(in, out, s)
}
14 changes: 14 additions & 0 deletions api/v1alpha4/gcpmachinetemplate_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha4

import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
infrav1beta1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
Expand All @@ -30,6 +31,14 @@ func (src *GCPMachineTemplate) ConvertTo(dstRaw conversion.Hub) error { // nolin
return err
}

// Manually restore data.
restored := &infrav1beta1.GCPMachineTemplate{}
if ok, err := utilconversion.UnmarshalData(src, restored); err != nil || !ok {
return err
}

dst.Spec.Template.ObjectMeta = restored.Spec.Template.ObjectMeta

return nil
}

Expand Down Expand Up @@ -59,3 +68,8 @@ func (dst *GCPMachineTemplateList) ConvertFrom(srcRaw conversion.Hub) error { //
src := srcRaw.(*infrav1beta1.GCPMachineTemplateList)
return Convert_v1beta1_GCPMachineTemplateList_To_v1alpha4_GCPMachineTemplateList(src, dst, nil)
}

func Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha4_GCPMachineTemplateResource(in *infrav1beta1.GCPMachineTemplateResource, out *GCPMachineTemplateResource, s apiconversion.Scope) error {
// NOTE: custom conversion func is required because spec.template.metadata has been added in v1beta1.
return autoConvert_v1beta1_GCPMachineTemplateResource_To_v1alpha4_GCPMachineTemplateResource(in, out, s)
}
56 changes: 34 additions & 22 deletions api/v1alpha4/zz_generated.conversion.go

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

23 changes: 23 additions & 0 deletions api/v1beta1/gcpclustertemplate_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

// Hub marks GCPClusterTemplate as a conversion hub.
func (*GCPClusterTemplate) Hub() {}

// Hub marks GCPClusterTemplateList as a conversion hub.
func (*GCPClusterTemplateList) Hub() {}
Loading

0 comments on commit 2087990

Please sign in to comment.