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 11, 2021
1 parent a60f5f5 commit 8563f84
Show file tree
Hide file tree
Showing 10 changed files with 999 additions and 35 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,15 @@ generate-manifests: $(addprefix generate-manifests-,$(ALL_GENERATE_MODULES)) ##

.PHONY: generate-manifests-core
generate-manifests-core: $(CONTROLLER_GEN) $(KUSTOMIZE)
## crd:allowDangerousTypes is required to allow using float64 in JSONSchemaProps
$(CONTROLLER_GEN) \
paths=./api/... \
paths=./controllers/... \
paths=./$(EXP_DIR)/api/... \
paths=./$(EXP_DIR)/controllers/... \
paths=./$(EXP_DIR)/addons/api/... \
paths=./$(EXP_DIR)/addons/controllers/... \
crd:crdVersions=v1 \
crd:allowDangerousTypes=true,crdVersions=v1 \
rbac:roleName=manager-role \
output:crd:dir=./config/crd/bases \
output:webhook:dir=./config/webhook \
Expand Down
16 changes: 16 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,17 @@ 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{}{
VariableTopologyFuzzer,
}
}

func VariableTopologyFuzzer(in *v1beta1.VariableTopology, c fuzz.Continue) {
c.FuzzNoCustom(in)

// apiextensiosnv1.JSON has custom MarshalJSON/UnamrshalJSON funcs which lead te errors.
// THis effectively disables fuzzing for this field.
in.Value = apiextensionsv1.JSON{Raw: []byte{}}
}
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)
}
49 changes: 45 additions & 4 deletions api/v1alpha4/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 @@ -28,12 +29,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 +74,41 @@ 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{}{
VariableTopologyFuzzer,
}
}

func VariableTopologyFuzzer(in *v1beta1.VariableTopology, c fuzz.Continue) {
c.FuzzNoCustom(in)

// apiextensiosnv1.JSON has custom MarshalJSON/UnamrshalJSON funcs which lead te errors.
// THis effectively disables fuzzing for this field.
in.Value = apiextensionsv1.JSON{Raw: []byte{}}
}

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

func JSONPatchDefinitionFuzzer(in *v1beta1.JSONPatchDefinition, c fuzz.Continue) {
c.FuzzNoCustom(in)

// apiextensiosnv1.JSON has custom MarshalJSON/UnamrshalJSON funcs which lead te errors.
// THis effectively disables fuzzing for this field.
in.Value = nil
}

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

// apiextensiosnv1.JSON has custom MarshalJSON/UnamrshalJSON funcs which lead te errors.
// THis effectively disables fuzzing for those fields.
in.Enum = nil
in.Default = nil
}
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 8563f84

Please sign in to comment.