forked from kubernetes-sigs/cluster-api-provider-gcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 kubernetes-sigs/cluster-api#5240
- Loading branch information
Winnie Kwon
committed
Mar 21, 2022
1 parent
d845a39
commit f527cf2
Showing
13 changed files
with
288 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{}, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.