From 20879905c466977bdff49588e64dcfa33ba7c04a Mon Sep 17 00:00:00 2001 From: Winnie Kwon Date: Mon, 21 Mar 2022 07:40:05 -0700 Subject: [PATCH] Fix v1alpha4 <-> v1beta1 conversions 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 https://github.com/kubernetes-sigs/cluster-api/issues/5240 --- api/v1alpha3/gcpmachinetemplate_conversion.go | 6 ++ api/v1alpha3/zz_generated.conversion.go | 40 +++++++--- api/v1alpha4/conversion_test.go | 46 ++++++++++++ api/v1alpha4/gcpclustertemplate_conversion.go | 75 +++++++++++++++++++ api/v1alpha4/gcpmachinetemplate_conversion.go | 14 ++++ api/v1alpha4/zz_generated.conversion.go | 56 ++++++++------ api/v1beta1/gcpclustertemplate_conversion.go | 23 ++++++ api/v1beta1/gcpclustertemplate_types.go | 6 ++ .../gcpclustertemplate_webhook_test.go | 36 +++++---- api/v1beta1/types.go | 7 ++ api/v1beta1/zz_generated.deepcopy.go | 2 + ....cluster.x-k8s.io_gcpclustertemplates.yaml | 21 ++++++ ....cluster.x-k8s.io_gcpmachinetemplates.yaml | 21 ++++++ 13 files changed, 303 insertions(+), 50 deletions(-) create mode 100644 api/v1alpha4/conversion_test.go create mode 100644 api/v1alpha4/gcpclustertemplate_conversion.go create mode 100644 api/v1beta1/gcpclustertemplate_conversion.go diff --git a/api/v1alpha3/gcpmachinetemplate_conversion.go b/api/v1alpha3/gcpmachinetemplate_conversion.go index 6f86f53eb..f4926c2ed 100644 --- a/api/v1alpha3/gcpmachinetemplate_conversion.go +++ b/api/v1alpha3/gcpmachinetemplate_conversion.go @@ -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" @@ -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) +} diff --git a/api/v1alpha3/zz_generated.conversion.go b/api/v1alpha3/zz_generated.conversion.go index f93eb65c8..87b44ad12 100644 --- a/api/v1alpha3/zz_generated.conversion.go +++ b/api/v1alpha3/zz_generated.conversion.go @@ -155,11 +155,6 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } - if err := s.AddGeneratedConversionFunc((*v1beta1.GCPMachineTemplateResource)(nil), (*GCPMachineTemplateResource)(nil), func(a, b interface{}, scope conversion.Scope) error { - return Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha3_GCPMachineTemplateResource(a.(*v1beta1.GCPMachineTemplateResource), b.(*GCPMachineTemplateResource), scope) - }); err != nil { - return err - } if err := s.AddGeneratedConversionFunc((*GCPMachineTemplateSpec)(nil), (*v1beta1.GCPMachineTemplateSpec)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1alpha3_GCPMachineTemplateSpec_To_v1beta1_GCPMachineTemplateSpec(a.(*GCPMachineTemplateSpec), b.(*v1beta1.GCPMachineTemplateSpec), scope) }); err != nil { @@ -235,6 +230,11 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } + if err := s.AddConversionFunc((*v1beta1.GCPMachineTemplateResource)(nil), (*GCPMachineTemplateResource)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha3_GCPMachineTemplateResource(a.(*v1beta1.GCPMachineTemplateResource), b.(*GCPMachineTemplateResource), scope) + }); err != nil { + return err + } if err := s.AddConversionFunc((*v1beta1.NetworkSpec)(nil), (*NetworkSpec)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1beta1_NetworkSpec_To_v1alpha3_NetworkSpec(a.(*v1beta1.NetworkSpec), b.(*NetworkSpec), scope) }); err != nil { @@ -615,7 +615,17 @@ func Convert_v1beta1_GCPMachineTemplate_To_v1alpha3_GCPMachineTemplate(in *v1bet func autoConvert_v1alpha3_GCPMachineTemplateList_To_v1beta1_GCPMachineTemplateList(in *GCPMachineTemplateList, out *v1beta1.GCPMachineTemplateList, s conversion.Scope) error { out.ListMeta = in.ListMeta - out.Items = *(*[]v1beta1.GCPMachineTemplate)(unsafe.Pointer(&in.Items)) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]v1beta1.GCPMachineTemplate, len(*in)) + for i := range *in { + if err := Convert_v1alpha3_GCPMachineTemplate_To_v1beta1_GCPMachineTemplate(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Items = nil + } return nil } @@ -626,7 +636,17 @@ func Convert_v1alpha3_GCPMachineTemplateList_To_v1beta1_GCPMachineTemplateList(i func autoConvert_v1beta1_GCPMachineTemplateList_To_v1alpha3_GCPMachineTemplateList(in *v1beta1.GCPMachineTemplateList, out *GCPMachineTemplateList, s conversion.Scope) error { out.ListMeta = in.ListMeta - out.Items = *(*[]GCPMachineTemplate)(unsafe.Pointer(&in.Items)) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]GCPMachineTemplate, len(*in)) + for i := range *in { + if err := Convert_v1beta1_GCPMachineTemplate_To_v1alpha3_GCPMachineTemplate(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Items = nil + } return nil } @@ -648,17 +668,13 @@ func Convert_v1alpha3_GCPMachineTemplateResource_To_v1beta1_GCPMachineTemplateRe } func autoConvert_v1beta1_GCPMachineTemplateResource_To_v1alpha3_GCPMachineTemplateResource(in *v1beta1.GCPMachineTemplateResource, out *GCPMachineTemplateResource, s conversion.Scope) error { + // WARNING: in.ObjectMeta requires manual conversion: does not exist in peer-type if err := Convert_v1beta1_GCPMachineSpec_To_v1alpha3_GCPMachineSpec(&in.Spec, &out.Spec, s); err != nil { return err } return nil } -// Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha3_GCPMachineTemplateResource is an autogenerated conversion function. -func Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha3_GCPMachineTemplateResource(in *v1beta1.GCPMachineTemplateResource, out *GCPMachineTemplateResource, s conversion.Scope) error { - return autoConvert_v1beta1_GCPMachineTemplateResource_To_v1alpha3_GCPMachineTemplateResource(in, out, s) -} - func autoConvert_v1alpha3_GCPMachineTemplateSpec_To_v1beta1_GCPMachineTemplateSpec(in *GCPMachineTemplateSpec, out *v1beta1.GCPMachineTemplateSpec, s conversion.Scope) error { if err := Convert_v1alpha3_GCPMachineTemplateResource_To_v1beta1_GCPMachineTemplateResource(&in.Template, &out.Template, s); err != nil { return err diff --git a/api/v1alpha4/conversion_test.go b/api/v1alpha4/conversion_test.go new file mode 100644 index 000000000..deebe8240 --- /dev/null +++ b/api/v1alpha4/conversion_test.go @@ -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{}, + })) +} diff --git a/api/v1alpha4/gcpclustertemplate_conversion.go b/api/v1alpha4/gcpclustertemplate_conversion.go new file mode 100644 index 000000000..f811741a9 --- /dev/null +++ b/api/v1alpha4/gcpclustertemplate_conversion.go @@ -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) +} diff --git a/api/v1alpha4/gcpmachinetemplate_conversion.go b/api/v1alpha4/gcpmachinetemplate_conversion.go index cb8653d64..4a0af4218 100644 --- a/api/v1alpha4/gcpmachinetemplate_conversion.go +++ b/api/v1alpha4/gcpmachinetemplate_conversion.go @@ -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" @@ -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 } @@ -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) +} diff --git a/api/v1alpha4/zz_generated.conversion.go b/api/v1alpha4/zz_generated.conversion.go index 79f788d7e..b0dcb40ee 100644 --- a/api/v1alpha4/zz_generated.conversion.go +++ b/api/v1alpha4/zz_generated.conversion.go @@ -135,11 +135,6 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } - if err := s.AddGeneratedConversionFunc((*v1beta1.GCPClusterTemplateResource)(nil), (*GCPClusterTemplateResource)(nil), func(a, b interface{}, scope conversion.Scope) error { - return Convert_v1beta1_GCPClusterTemplateResource_To_v1alpha4_GCPClusterTemplateResource(a.(*v1beta1.GCPClusterTemplateResource), b.(*GCPClusterTemplateResource), scope) - }); err != nil { - return err - } if err := s.AddGeneratedConversionFunc((*GCPClusterTemplateSpec)(nil), (*v1beta1.GCPClusterTemplateSpec)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1alpha4_GCPClusterTemplateSpec_To_v1beta1_GCPClusterTemplateSpec(a.(*GCPClusterTemplateSpec), b.(*v1beta1.GCPClusterTemplateSpec), scope) }); err != nil { @@ -215,11 +210,6 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } - if err := s.AddGeneratedConversionFunc((*v1beta1.GCPMachineTemplateResource)(nil), (*GCPMachineTemplateResource)(nil), func(a, b interface{}, scope conversion.Scope) error { - return Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha4_GCPMachineTemplateResource(a.(*v1beta1.GCPMachineTemplateResource), b.(*GCPMachineTemplateResource), scope) - }); err != nil { - return err - } if err := s.AddGeneratedConversionFunc((*GCPMachineTemplateSpec)(nil), (*v1beta1.GCPMachineTemplateSpec)(nil), func(a, b interface{}, scope conversion.Scope) error { return Convert_v1alpha4_GCPMachineTemplateSpec_To_v1beta1_GCPMachineTemplateSpec(a.(*GCPMachineTemplateSpec), b.(*v1beta1.GCPMachineTemplateSpec), scope) }); err != nil { @@ -280,6 +270,16 @@ func RegisterConversions(s *runtime.Scheme) error { }); err != nil { return err } + if err := s.AddConversionFunc((*v1beta1.GCPClusterTemplateResource)(nil), (*GCPClusterTemplateResource)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1beta1_GCPClusterTemplateResource_To_v1alpha4_GCPClusterTemplateResource(a.(*v1beta1.GCPClusterTemplateResource), b.(*GCPClusterTemplateResource), scope) + }); err != nil { + return err + } + if err := s.AddConversionFunc((*v1beta1.GCPMachineTemplateResource)(nil), (*GCPMachineTemplateResource)(nil), func(a, b interface{}, scope conversion.Scope) error { + return Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha4_GCPMachineTemplateResource(a.(*v1beta1.GCPMachineTemplateResource), b.(*GCPMachineTemplateResource), scope) + }); err != nil { + return err + } return nil } @@ -600,17 +600,13 @@ func Convert_v1alpha4_GCPClusterTemplateResource_To_v1beta1_GCPClusterTemplateRe } func autoConvert_v1beta1_GCPClusterTemplateResource_To_v1alpha4_GCPClusterTemplateResource(in *v1beta1.GCPClusterTemplateResource, out *GCPClusterTemplateResource, s conversion.Scope) error { + // WARNING: in.ObjectMeta requires manual conversion: does not exist in peer-type if err := Convert_v1beta1_GCPClusterSpec_To_v1alpha4_GCPClusterSpec(&in.Spec, &out.Spec, s); err != nil { return err } return nil } -// Convert_v1beta1_GCPClusterTemplateResource_To_v1alpha4_GCPClusterTemplateResource is an autogenerated conversion function. -func Convert_v1beta1_GCPClusterTemplateResource_To_v1alpha4_GCPClusterTemplateResource(in *v1beta1.GCPClusterTemplateResource, out *GCPClusterTemplateResource, s conversion.Scope) error { - return autoConvert_v1beta1_GCPClusterTemplateResource_To_v1alpha4_GCPClusterTemplateResource(in, out, s) -} - func autoConvert_v1alpha4_GCPClusterTemplateSpec_To_v1beta1_GCPClusterTemplateSpec(in *GCPClusterTemplateSpec, out *v1beta1.GCPClusterTemplateSpec, s conversion.Scope) error { if err := Convert_v1alpha4_GCPClusterTemplateResource_To_v1beta1_GCPClusterTemplateResource(&in.Template, &out.Template, s); err != nil { return err @@ -791,7 +787,17 @@ func Convert_v1beta1_GCPMachineTemplate_To_v1alpha4_GCPMachineTemplate(in *v1bet func autoConvert_v1alpha4_GCPMachineTemplateList_To_v1beta1_GCPMachineTemplateList(in *GCPMachineTemplateList, out *v1beta1.GCPMachineTemplateList, s conversion.Scope) error { out.ListMeta = in.ListMeta - out.Items = *(*[]v1beta1.GCPMachineTemplate)(unsafe.Pointer(&in.Items)) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]v1beta1.GCPMachineTemplate, len(*in)) + for i := range *in { + if err := Convert_v1alpha4_GCPMachineTemplate_To_v1beta1_GCPMachineTemplate(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Items = nil + } return nil } @@ -802,7 +808,17 @@ func Convert_v1alpha4_GCPMachineTemplateList_To_v1beta1_GCPMachineTemplateList(i func autoConvert_v1beta1_GCPMachineTemplateList_To_v1alpha4_GCPMachineTemplateList(in *v1beta1.GCPMachineTemplateList, out *GCPMachineTemplateList, s conversion.Scope) error { out.ListMeta = in.ListMeta - out.Items = *(*[]GCPMachineTemplate)(unsafe.Pointer(&in.Items)) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]GCPMachineTemplate, len(*in)) + for i := range *in { + if err := Convert_v1beta1_GCPMachineTemplate_To_v1alpha4_GCPMachineTemplate(&(*in)[i], &(*out)[i], s); err != nil { + return err + } + } + } else { + out.Items = nil + } return nil } @@ -824,17 +840,13 @@ func Convert_v1alpha4_GCPMachineTemplateResource_To_v1beta1_GCPMachineTemplateRe } func autoConvert_v1beta1_GCPMachineTemplateResource_To_v1alpha4_GCPMachineTemplateResource(in *v1beta1.GCPMachineTemplateResource, out *GCPMachineTemplateResource, s conversion.Scope) error { + // WARNING: in.ObjectMeta requires manual conversion: does not exist in peer-type if err := Convert_v1beta1_GCPMachineSpec_To_v1alpha4_GCPMachineSpec(&in.Spec, &out.Spec, s); err != nil { return err } return nil } -// Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha4_GCPMachineTemplateResource is an autogenerated conversion function. -func Convert_v1beta1_GCPMachineTemplateResource_To_v1alpha4_GCPMachineTemplateResource(in *v1beta1.GCPMachineTemplateResource, out *GCPMachineTemplateResource, s conversion.Scope) error { - return autoConvert_v1beta1_GCPMachineTemplateResource_To_v1alpha4_GCPMachineTemplateResource(in, out, s) -} - func autoConvert_v1alpha4_GCPMachineTemplateSpec_To_v1beta1_GCPMachineTemplateSpec(in *GCPMachineTemplateSpec, out *v1beta1.GCPMachineTemplateSpec, s conversion.Scope) error { if err := Convert_v1alpha4_GCPMachineTemplateResource_To_v1beta1_GCPMachineTemplateResource(&in.Template, &out.Template, s); err != nil { return err diff --git a/api/v1beta1/gcpclustertemplate_conversion.go b/api/v1beta1/gcpclustertemplate_conversion.go new file mode 100644 index 000000000..a1c5824fe --- /dev/null +++ b/api/v1beta1/gcpclustertemplate_conversion.go @@ -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() {} diff --git a/api/v1beta1/gcpclustertemplate_types.go b/api/v1beta1/gcpclustertemplate_types.go index 288c49af0..977bbcd77 100644 --- a/api/v1beta1/gcpclustertemplate_types.go +++ b/api/v1beta1/gcpclustertemplate_types.go @@ -18,6 +18,7 @@ package v1beta1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" ) // GCPClusterTemplateSpec defines the desired state of GCPClusterTemplate. @@ -27,6 +28,11 @@ type GCPClusterTemplateSpec struct { // GCPClusterTemplateResource contains spec for GCPClusterSpec. type GCPClusterTemplateResource struct { + // Standard object's metadata. + // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata + // +optional + ObjectMeta clusterv1.ObjectMeta `json:"metadata,omitempty"` + Spec GCPClusterSpec `json:"spec"` } diff --git a/api/v1beta1/gcpclustertemplate_webhook_test.go b/api/v1beta1/gcpclustertemplate_webhook_test.go index 7d974f670..30eabc333 100644 --- a/api/v1beta1/gcpclustertemplate_webhook_test.go +++ b/api/v1beta1/gcpclustertemplate_webhook_test.go @@ -35,18 +35,20 @@ func TestGCPClusterTemplate_ValidateUpdate(t *testing.T) { name: "GCPClusterTemplated with immutable spec", newTemplate: &GCPClusterTemplate{ Spec: GCPClusterTemplateSpec{ - Template: GCPClusterTemplateResource{GCPClusterSpec{ - Project: "test-gcp-cluster", - Region: "ap-south-1", - }}, + Template: GCPClusterTemplateResource{ + Spec: GCPClusterSpec{ + Project: "test-gcp-cluster", + Region: "ap-south-1", + }}, }, }, oldTemplate: &GCPClusterTemplate{ Spec: GCPClusterTemplateSpec{ - Template: GCPClusterTemplateResource{GCPClusterSpec{ - Project: "test-gcp-cluster", - Region: "ap-south-1", - }}, + Template: GCPClusterTemplateResource{ + Spec: GCPClusterSpec{ + Project: "test-gcp-cluster", + Region: "ap-south-1", + }}, }, }, wantErr: false, @@ -55,18 +57,20 @@ func TestGCPClusterTemplate_ValidateUpdate(t *testing.T) { name: "GCPClusterTemplated with mutable spec", newTemplate: &GCPClusterTemplate{ Spec: GCPClusterTemplateSpec{ - Template: GCPClusterTemplateResource{GCPClusterSpec{ - Project: "test-gcp-cluster", - Region: "ap-south-1", - }}, + Template: GCPClusterTemplateResource{ + Spec: GCPClusterSpec{ + Project: "test-gcp-cluster", + Region: "ap-south-1", + }}, }, }, oldTemplate: &GCPClusterTemplate{ Spec: GCPClusterTemplateSpec{ - Template: GCPClusterTemplateResource{GCPClusterSpec{ - Project: "test-gcp-cluster", - Region: "ap-east-1", - }}, + Template: GCPClusterTemplateResource{ + Spec: GCPClusterSpec{ + Project: "test-gcp-cluster", + Region: "ap-east-1", + }}, }, }, wantErr: true, diff --git a/api/v1beta1/types.go b/api/v1beta1/types.go index ead972ef8..c5a5811fa 100644 --- a/api/v1beta1/types.go +++ b/api/v1beta1/types.go @@ -18,10 +18,17 @@ package v1beta1 import ( "fmt" + + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" ) // GCPMachineTemplateResource describes the data needed to create am GCPMachine from a template. type GCPMachineTemplateResource struct { + // Standard object's metadata. + // More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata + // +optional + ObjectMeta clusterv1.ObjectMeta `json:"metadata,omitempty"` + // Spec is the specification of the desired behavior of the machine. Spec GCPMachineSpec `json:"spec"` } diff --git a/api/v1beta1/zz_generated.deepcopy.go b/api/v1beta1/zz_generated.deepcopy.go index a759aa4e6..523eb9b16 100644 --- a/api/v1beta1/zz_generated.deepcopy.go +++ b/api/v1beta1/zz_generated.deepcopy.go @@ -272,6 +272,7 @@ func (in *GCPClusterTemplateList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *GCPClusterTemplateResource) DeepCopyInto(out *GCPClusterTemplateResource) { *out = *in + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) in.Spec.DeepCopyInto(&out.Spec) } @@ -532,6 +533,7 @@ func (in *GCPMachineTemplateList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *GCPMachineTemplateResource) DeepCopyInto(out *GCPMachineTemplateResource) { *out = *in + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) in.Spec.DeepCopyInto(&out.Spec) } diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclustertemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclustertemplates.yaml index b4dfe5b59..96e3ea993 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclustertemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpclustertemplates.yaml @@ -189,6 +189,27 @@ spec: template: description: GCPClusterTemplateResource contains spec for GCPClusterSpec. properties: + metadata: + description: 'Standard object''s metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata' + properties: + annotations: + additionalProperties: + type: string + description: 'Annotations is an unstructured key value map + stored with a resource that may be set by external tools + to store and retrieve arbitrary metadata. They are not queryable + and should be preserved when modifying objects. More info: + http://kubernetes.io/docs/user-guide/annotations' + type: object + labels: + additionalProperties: + type: string + description: 'Map of string keys and values that can be used + to organize and categorize (scope and select) objects. May + match selectors of replication controllers and services. + More info: http://kubernetes.io/docs/user-guide/labels' + type: object + type: object spec: description: GCPClusterSpec defines the desired state of GCPCluster. properties: diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachinetemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachinetemplates.yaml index d7571ac81..d4598e1ad 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_gcpmachinetemplates.yaml @@ -354,6 +354,27 @@ spec: description: GCPMachineTemplateResource describes the data needed to create am GCPMachine from a template. properties: + metadata: + description: 'Standard object''s metadata. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata' + properties: + annotations: + additionalProperties: + type: string + description: 'Annotations is an unstructured key value map + stored with a resource that may be set by external tools + to store and retrieve arbitrary metadata. They are not queryable + and should be preserved when modifying objects. More info: + http://kubernetes.io/docs/user-guide/annotations' + type: object + labels: + additionalProperties: + type: string + description: 'Map of string keys and values that can be used + to organize and categorize (scope and select) objects. May + match selectors of replication controllers and services. + More info: http://kubernetes.io/docs/user-guide/labels' + type: object + type: object spec: description: Spec is the specification of the desired behavior of the machine.