Skip to content

Commit

Permalink
Add ClusterClass variable and patch types
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Oct 25, 2021
1 parent 68927b7 commit 1022e26
Show file tree
Hide file tree
Showing 10 changed files with 1,031 additions and 96 deletions.
15 changes: 15 additions & 0 deletions api/v1alpha3/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

fuzz "github.com/google/gofuzz"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/cluster-api/api/v1beta1"
Expand All @@ -32,6 +33,7 @@ func TestFuzzyConversion(t *testing.T) {
Hub: &v1beta1.Cluster{},
Spoke: &Cluster{},
SpokeAfterMutation: clusterSpokeAfterMutation,
FuzzerFuncs: []fuzzer.FuzzerFuncs{ClusterJSONFuzzFuncs},
}))

t.Run("for Machine", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Expand Down Expand Up @@ -123,3 +125,16 @@ func clusterSpokeAfterMutation(c conversion.Convertible) {
// Point cluster.Status.Conditions and our slice that does not have ControlPlaneInitializedCondition
cluster.Status.Conditions = tmp
}

func ClusterJSONFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
ClusterVariableFuzzer,
}
}

func ClusterVariableFuzzer(in *v1beta1.ClusterVariable, c fuzz.Continue) {
c.FuzzNoCustom(in)

// Not every random byte array is valid JSON, e.g. a string without `""`,so we're setting a valid value.
in.Value = apiextensionsv1.JSON{Raw: []byte("\"test-string\"")}
}
64 changes: 60 additions & 4 deletions api/v1alpha4/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,43 @@ package v1alpha4
import (
apiconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/cluster-api/api/v1beta1"
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

func (src *Cluster) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1beta1.Cluster)

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

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

if restored.Spec.Topology != nil {
dst.Spec.Topology.Variables = restored.Spec.Topology.Variables
}

return nil
}

func (dst *Cluster) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1beta1.Cluster)

return Convert_v1beta1_Cluster_To_v1alpha4_Cluster(src, dst, nil)
if err := Convert_v1beta1_Cluster_To_v1alpha4_Cluster(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
}

func (src *ClusterList) ConvertTo(dstRaw conversion.Hub) error {
Expand All @@ -49,13 +73,35 @@ func (dst *ClusterList) ConvertFrom(srcRaw conversion.Hub) error {
func (src *ClusterClass) ConvertTo(dstRaw conversion.Hub) error {
dst := dstRaw.(*v1beta1.ClusterClass)

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

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

dst.Spec.Patches = restored.Spec.Patches
dst.Spec.Variables = restored.Spec.Variables

return nil
}

func (dst *ClusterClass) ConvertFrom(srcRaw conversion.Hub) error {
src := srcRaw.(*v1beta1.ClusterClass)

return Convert_v1beta1_ClusterClass_To_v1alpha4_ClusterClass(src, dst, nil)
if err := Convert_v1beta1_ClusterClass_To_v1alpha4_ClusterClass(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
}

func (src *ClusterClassList) ConvertTo(dstRaw conversion.Hub) error {
Expand Down Expand Up @@ -170,3 +216,13 @@ func Convert_v1alpha4_MachineStatus_To_v1beta1_MachineStatus(in *MachineStatus,
// Status.version has been removed in v1beta1, thus requiring custom conversion function. the information will be dropped.
return autoConvert_v1alpha4_MachineStatus_To_v1beta1_MachineStatus(in, out, s)
}

func Convert_v1beta1_ClusterClassSpec_To_v1alpha4_ClusterClassSpec(in *v1beta1.ClusterClassSpec, out *ClusterClassSpec, s apiconversion.Scope) error {
// spec.{variables,patches} has been added with v1beta1.
return autoConvert_v1beta1_ClusterClassSpec_To_v1alpha4_ClusterClassSpec(in, out, s)
}

func Convert_v1beta1_Topology_To_v1alpha4_Topology(in *v1beta1.Topology, out *Topology, s apiconversion.Scope) error {
// spec.topology.variables has been added with v1beta1.
return autoConvert_v1beta1_Topology_To_v1alpha4_Topology(in, out, s)
}
57 changes: 53 additions & 4 deletions api/v1alpha4/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package v1alpha4

import (
"encoding/json"
"testing"

fuzz "github.com/google/gofuzz"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/api/apitesting/fuzzer"
runtimeserializer "k8s.io/apimachinery/pkg/runtime/serializer"
"sigs.k8s.io/cluster-api/api/v1beta1"
Expand All @@ -28,12 +30,14 @@ import (

func TestFuzzyConversion(t *testing.T) {
t.Run("for Cluster", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &v1beta1.Cluster{},
Spoke: &Cluster{},
Hub: &v1beta1.Cluster{},
Spoke: &Cluster{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{ClusterJSONFuzzFuncs},
}))
t.Run("for ClusterClass", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Hub: &v1beta1.ClusterClass{},
Spoke: &ClusterClass{},
Hub: &v1beta1.ClusterClass{},
Spoke: &ClusterClass{},
FuzzerFuncs: []fuzzer.FuzzerFuncs{ClusterClassJSONFuzzFuncs},
}))

t.Run("for Machine", utilconversion.FuzzTestFunc(utilconversion.FuzzTestFuncInput{
Expand Down Expand Up @@ -71,3 +75,48 @@ func MachineStatusFuzzer(in *MachineStatus, c fuzz.Continue) {
// data is going to be lost, so we're forcing zero values to avoid round trip errors.
in.Version = nil
}

func ClusterJSONFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
ClusterVariableFuzzer,
}
}

func ClusterVariableFuzzer(in *v1beta1.ClusterVariable, c fuzz.Continue) {
c.FuzzNoCustom(in)

// Not every random byte array is valid JSON, e.g. a string without `""`,so we're setting a valid value.
in.Value = apiextensionsv1.JSON{Raw: []byte("\"test-string\"")}
}

func ClusterClassJSONFuzzFuncs(_ runtimeserializer.CodecFactory) []interface{} {
return []interface{}{
JSONPatchFuzzer,
JSONSchemaPropsFuzzer,
}
}

func JSONPatchFuzzer(in *v1beta1.JSONPatch, c fuzz.Continue) {
c.FuzzNoCustom(in)

// Not every random byte array is valid JSON, e.g. a string without `""`,so we're setting a valid value.
in.Value = &apiextensionsv1.JSON{Raw: []byte("5")}
}

func JSONSchemaPropsFuzzer(in *v1beta1.JSONSchemaProps, c fuzz.Continue) {
c.FuzzNoCustom(in)

// Not every random byte array is valid JSON, e.g. a string without `""`,so we're setting valid values.
in.Enum = []apiextensionsv1.JSON{
{Raw: []byte("\"a\"")},
{Raw: []byte("\"b\"")},
{Raw: []byte("\"c\"")},
}
in.Default = &apiextensionsv1.JSON{Raw: []byte("true")}

// Not every random string is a valid JSON number, so we're setting a valid JSON number.
number := json.Number("1")
in.MultipleOf = &number
in.Minimum = &number
in.Maximum = &number
}
101 changes: 75 additions & 26 deletions api/v1alpha4/zz_generated.conversion.go

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

Loading

0 comments on commit 1022e26

Please sign in to comment.