sigs.k8s.io/cluster-api@v1.7.1/bootstrap/kubeadm/types/upstreamv1beta2/conversion_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package upstreamv1beta2
    18  
    19  import (
    20  	"testing"
    21  
    22  	fuzz "github.com/google/gofuzz"
    23  	"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
    24  	runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
    25  
    26  	bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
    27  	utilconversion "sigs.k8s.io/cluster-api/util/conversion"
    28  )
    29  
    30  func TestFuzzyConversion(t *testing.T) {
    31  	t.Run("for ClusterConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    32  		Hub:   &bootstrapv1.ClusterConfiguration{},
    33  		Spoke: &ClusterConfiguration{},
    34  		// NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test.
    35  		SkipSpokeAnnotationCleanup: true,
    36  		FuzzerFuncs:                []fuzzer.FuzzerFuncs{fuzzFuncs},
    37  	}))
    38  	t.Run("for ClusterStatus", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    39  		Hub:   &bootstrapv1.ClusterStatus{},
    40  		Spoke: &ClusterStatus{},
    41  		// NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test.
    42  		SkipSpokeAnnotationCleanup: true,
    43  		FuzzerFuncs:                []fuzzer.FuzzerFuncs{fuzzFuncs},
    44  	}))
    45  	t.Run("for InitConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    46  		Hub:   &bootstrapv1.InitConfiguration{},
    47  		Spoke: &InitConfiguration{},
    48  		// NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test.
    49  		SkipSpokeAnnotationCleanup: true,
    50  		FuzzerFuncs:                []fuzzer.FuzzerFuncs{fuzzFuncs},
    51  	}))
    52  	t.Run("for JoinConfiguration", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
    53  		Hub:   &bootstrapv1.JoinConfiguration{},
    54  		Spoke: &JoinConfiguration{},
    55  		// NOTE: Kubeadm types does not have ObjectMeta, so we are required to skip data annotation cleanup in the spoke-hub-spoke round trip test.
    56  		SkipSpokeAnnotationCleanup: true,
    57  		FuzzerFuncs:                []fuzzer.FuzzerFuncs{fuzzFuncs},
    58  	}))
    59  }
    60  
    61  func fuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
    62  	return []interface{}{
    63  		initConfigurationFuzzer,
    64  		joinControlPlanesFuzzer,
    65  		dnsFuzzer,
    66  		clusterConfigurationFuzzer,
    67  		kubeadmInitConfigurationFuzzer,
    68  		kubeadmJoinConfigurationFuzzer,
    69  		kubeadmNodeRegistrationOptionsFuzzer,
    70  	}
    71  }
    72  
    73  func joinControlPlanesFuzzer(obj *JoinControlPlane, c fuzz.Continue) {
    74  	c.FuzzNoCustom(obj)
    75  
    76  	// JoinControlPlane.CertificateKey does not exists in v1alpha4, so setting it to empty string in order to avoid v1beta2 --> v1alpha4 --> v1beta2 round trip errors.
    77  	obj.CertificateKey = ""
    78  }
    79  
    80  func initConfigurationFuzzer(obj *InitConfiguration, c fuzz.Continue) {
    81  	c.Fuzz(obj)
    82  
    83  	// InitConfiguration.CertificateKey does not exists in v1alpha4, so setting it to empty string in order to avoid v1beta2 --> v1alpha4 --> v1beta2 round trip errors.
    84  	obj.CertificateKey = ""
    85  }
    86  
    87  func dnsFuzzer(obj *DNS, c fuzz.Continue) {
    88  	c.FuzzNoCustom(obj)
    89  
    90  	// DNS.Type does not exists in v1alpha4, so setting it to empty string in order to avoid v1beta2 --> v1alpha4 --> v1beta2 round trip errors.
    91  	obj.Type = ""
    92  }
    93  
    94  func clusterConfigurationFuzzer(obj *ClusterConfiguration, c fuzz.Continue) {
    95  	c.FuzzNoCustom(obj)
    96  
    97  	// ClusterConfiguration.UseHyperKubeImage has been removed in v1alpha4, so setting it to false in order to avoid v1beta2 --> v1alpha4 --> v1beta2 round trip errors.
    98  	obj.UseHyperKubeImage = false
    99  }
   100  
   101  func kubeadmInitConfigurationFuzzer(obj *bootstrapv1.InitConfiguration, c fuzz.Continue) {
   102  	c.FuzzNoCustom(obj)
   103  
   104  	// InitConfiguration.Patches does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
   105  	// v1beta1 --> upstream v1beta2 -> v1beta1 round trip errors.
   106  	obj.Patches = nil
   107  
   108  	// InitConfiguration.SkipPhases does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
   109  	// v1beta1 --> upstream v1beta2 -> v1beta1 round trip errors.
   110  	obj.SkipPhases = nil
   111  }
   112  
   113  func kubeadmJoinConfigurationFuzzer(obj *bootstrapv1.JoinConfiguration, c fuzz.Continue) {
   114  	c.FuzzNoCustom(obj)
   115  
   116  	// JoinConfiguration.Patches does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
   117  	// v1beta1 --> upstream v1beta2 -> v1beta1 round trip errors.
   118  	obj.Patches = nil
   119  
   120  	// JoinConfiguration.SkipPhases does not exist in kubeadm v1beta1 API, so setting it to nil in order to avoid
   121  	// v1beta1 --> upstream v1beta2 -> v1beta1 round trip errors.
   122  	obj.SkipPhases = nil
   123  }
   124  
   125  func kubeadmNodeRegistrationOptionsFuzzer(obj *bootstrapv1.NodeRegistrationOptions, c fuzz.Continue) {
   126  	c.FuzzNoCustom(obj)
   127  
   128  	// NodeRegistrationOptions.ImagePullPolicy does not exist in
   129  	// kubeadm v1beta2 API, so setting it to empty in order to
   130  	// avoid round trip errors.
   131  	obj.ImagePullPolicy = ""
   132  }