Skip to content

Commit

Permalink
restore fields in top-level types
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Jul 29, 2021
1 parent 0462f85 commit 0aa4de9
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 20 deletions.
79 changes: 75 additions & 4 deletions bootstrap/kubeadm/api/v1alpha3/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,55 @@ import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
kubeadmbootstrapv1alpha4 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4"
kubeadmbootstrapv1beta1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

// ConvertTo converts this KubeadmConfig to the Hub version (v1alpha4).
func (src *KubeadmConfig) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*kubeadmbootstrapv1alpha4.KubeadmConfig)
return Convert_v1alpha3_KubeadmConfig_To_v1alpha4_KubeadmConfig(src, dst, nil)

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

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

if restored.Spec.JoinConfiguration != nil && restored.Spec.JoinConfiguration.NodeRegistration.IgnorePreflightErrors != nil {
if dst.Spec.JoinConfiguration == nil {
dst.Spec.JoinConfiguration = &kubeadmbootstrapv1alpha4.JoinConfiguration{}
}
dst.Spec.JoinConfiguration.NodeRegistration.IgnorePreflightErrors = restored.Spec.JoinConfiguration.NodeRegistration.IgnorePreflightErrors
}

if restored.Spec.InitConfiguration != nil && restored.Spec.InitConfiguration.NodeRegistration.IgnorePreflightErrors != nil {
if dst.Spec.InitConfiguration == nil {
dst.Spec.InitConfiguration = &kubeadmbootstrapv1alpha4.InitConfiguration{}
}
dst.Spec.InitConfiguration.NodeRegistration.IgnorePreflightErrors = restored.Spec.InitConfiguration.NodeRegistration.IgnorePreflightErrors
}

return nil
}

// ConvertFrom converts from the KubeadmConfig Hub version (v1alpha4) to this version.
func (dst *KubeadmConfig) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*kubeadmbootstrapv1alpha4.KubeadmConfig)
return Convert_v1alpha4_KubeadmConfig_To_v1alpha3_KubeadmConfig(src, dst, nil)

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

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

return nil
}

// ConvertTo converts this KubeadmConfigList to the Hub version (v1alpha4).
Expand All @@ -50,13 +86,48 @@ func (dst *KubeadmConfigList) ConvertFrom(srcRaw conversion.Hub) error {
// ConvertTo converts this KubeadmConfigTemplate to the Hub version (v1alpha4).
func (src *KubeadmConfigTemplate) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*kubeadmbootstrapv1alpha4.KubeadmConfigTemplate)
return Convert_v1alpha3_KubeadmConfigTemplate_To_v1alpha4_KubeadmConfigTemplate(src, dst, nil)

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

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

if restored.Spec.Template.Spec.JoinConfiguration != nil && restored.Spec.Template.Spec.JoinConfiguration.NodeRegistration.IgnorePreflightErrors != nil {
if dst.Spec.Template.Spec.JoinConfiguration == nil {
dst.Spec.Template.Spec.JoinConfiguration = &kubeadmbootstrapv1alpha4.JoinConfiguration{}
}
dst.Spec.Template.Spec.JoinConfiguration.NodeRegistration.IgnorePreflightErrors = restored.Spec.Template.Spec.JoinConfiguration.NodeRegistration.IgnorePreflightErrors
}

if restored.Spec.Template.Spec.InitConfiguration != nil && restored.Spec.Template.Spec.InitConfiguration.NodeRegistration.IgnorePreflightErrors != nil {
if dst.Spec.Template.Spec.InitConfiguration == nil {
dst.Spec.Template.Spec.InitConfiguration = &kubeadmbootstrapv1alpha4.InitConfiguration{}
}
dst.Spec.Template.Spec.InitConfiguration.NodeRegistration.IgnorePreflightErrors = restored.Spec.Template.Spec.InitConfiguration.NodeRegistration.IgnorePreflightErrors
}

return nil
}

// ConvertFrom converts from the KubeadmConfigTemplate Hub version (v1alpha4) to this version.
func (dst *KubeadmConfigTemplate) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*kubeadmbootstrapv1alpha4.KubeadmConfigTemplate)
return Convert_v1alpha4_KubeadmConfigTemplate_To_v1alpha3_KubeadmConfigTemplate(src, dst, nil)

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

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

return nil
}

// ConvertTo converts this KubeadmConfigTemplateList to the Hub version (v1alpha3).
Expand Down
26 changes: 20 additions & 6 deletions bootstrap/kubeadm/api/v1alpha3/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4"
kubeadmbootstrapv1alpha4 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4"
"sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1"
kubeadmv1beta1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
)

Expand All @@ -46,7 +47,18 @@ func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
KubeadmConfigStatusFuzzer,
dnsFuzzer,
clusterConfigurationFuzzer,
kubeadmNodeRegistrationOptionsFuzzer,
// This custom functions are needed when ConvertTo/ConvertFrom functions
// uses the json package to unmarshal the bootstrap token string.
//
// The Kubeadm BootstrapTokenString type ships with a custom
// json string representation, in particular it supplies a customized
// UnmarshalJSON function that can return an error if the string
// isn't in the correct form.
//
// This function effectively disables any fuzzing for the token by setting
// the values for ID and Secret to working alphanumeric values.
kubeadmBootstrapTokenStringFuzzerV1beta1,
kubeadmBootstrapTokenStringFuzzerV1Alpha4,
}
}

Expand All @@ -71,10 +83,12 @@ func clusterConfigurationFuzzer(obj *v1beta1.ClusterConfiguration, c fuzz.Contin
obj.UseHyperKubeImage = false
}

func kubeadmNodeRegistrationOptionsFuzzer(obj *kubeadmbootstrapv1alpha4.NodeRegistrationOptions, c fuzz.Continue) {
c.FuzzNoCustom(obj)
func kubeadmBootstrapTokenStringFuzzerV1beta1(in *kubeadmv1beta1.BootstrapTokenString, c fuzz.Continue) {
in.ID = "abcdef"
in.Secret = "abcdef0123456789"
}

// NodeRegistrationOptions.IgnorePreflightErrors does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
// v1alpha4 --> v1alpha3 -> v1alpha4 round trip errors.
obj.IgnorePreflightErrors = nil
func kubeadmBootstrapTokenStringFuzzerV1Alpha4(in *kubeadmbootstrapv1alpha4.BootstrapTokenString, c fuzz.Continue) {
in.ID = "abcdef"
in.Secret = "abcdef0123456789"
}
15 changes: 15 additions & 0 deletions controlplane/kubeadm/api/v1alpha3/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package v1alpha3

import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
kubeadmbootstrapv1alpha4 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4"
"sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
Expand All @@ -39,6 +40,20 @@ func (src *KubeadmControlPlane) ConvertTo(destRaw conversion.Hub) error {
dest.Spec.RolloutStrategy = restored.Spec.RolloutStrategy
dest.Spec.MachineTemplate.ObjectMeta = restored.Spec.MachineTemplate.ObjectMeta

if restored.Spec.KubeadmConfigSpec.JoinConfiguration != nil && restored.Spec.KubeadmConfigSpec.JoinConfiguration.NodeRegistration.IgnorePreflightErrors != nil {
if dest.Spec.KubeadmConfigSpec.JoinConfiguration == nil {
dest.Spec.KubeadmConfigSpec.JoinConfiguration = &kubeadmbootstrapv1alpha4.JoinConfiguration{}
}
dest.Spec.KubeadmConfigSpec.JoinConfiguration.NodeRegistration.IgnorePreflightErrors = restored.Spec.KubeadmConfigSpec.JoinConfiguration.NodeRegistration.IgnorePreflightErrors
}

if restored.Spec.KubeadmConfigSpec.InitConfiguration != nil && restored.Spec.KubeadmConfigSpec.InitConfiguration.NodeRegistration.IgnorePreflightErrors != nil {
if dest.Spec.KubeadmConfigSpec.InitConfiguration == nil {
dest.Spec.KubeadmConfigSpec.InitConfiguration = &kubeadmbootstrapv1alpha4.InitConfiguration{}
}
dest.Spec.KubeadmConfigSpec.InitConfiguration.NodeRegistration.IgnorePreflightErrors = restored.Spec.KubeadmConfigSpec.InitConfiguration.NodeRegistration.IgnorePreflightErrors
}

return nil
}

Expand Down
10 changes: 0 additions & 10 deletions controlplane/kubeadm/api/v1alpha3/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
cabpkv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4"
kubeadmbootstrapv1alpha4 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha4"
kubeadmv1beta1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/types/v1beta1"
"sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha4"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
Expand Down Expand Up @@ -54,7 +53,6 @@ func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
cabpkBootstrapTokenStringFuzzer,
dnsFuzzer,
kubeadmClusterConfigurationFuzzer,
kubeadmNodeRegistrationOptionsFuzzer,
}
}

Expand All @@ -80,11 +78,3 @@ func kubeadmClusterConfigurationFuzzer(obj *kubeadmv1beta1.ClusterConfiguration,
// ClusterConfiguration.UseHyperKubeImage has been removed in v1alpha4, so setting it to false in order to avoid v1alpha3 --> v1alpha4 --> v1alpha3 round trip errors.
obj.UseHyperKubeImage = false
}

func kubeadmNodeRegistrationOptionsFuzzer(obj *kubeadmbootstrapv1alpha4.NodeRegistrationOptions, c fuzz.Continue) {
c.FuzzNoCustom(obj)

// NodeRegistrationOptions.IgnorePreflightErrors does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
// v1alpha4 --> v1alpha3 -> v1alpha4 round trip errors.
obj.IgnorePreflightErrors = nil
}

0 comments on commit 0aa4de9

Please sign in to comment.