Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from d2iq-labs/jimmi/variable-defaulting-in-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmidyson authored Mar 28, 2024
2 parents 0cd416f + 5656042 commit b17718a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.14.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion common/pkg/testutils/openapi/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

// convertToAPIExtensionsJSONSchemaProps converts a clusterv1.JSONSchemaProps to apiextensions.JSONSchemaProp.
// ConvertToAPIExtensionsJSONSchemaProps converts a clusterv1.JSONSchemaProps to apiextensions.JSONSchemaProp.
// NOTE: This is used whenever we want to use one of the upstream libraries, as they use apiextensions.JSONSchemaProp.
// NOTE: If new fields are added to clusterv1.JSONSchemaProps (e.g. to support complex types), the corresponding
// schema validation must be added to validateRootSchema too.
Expand Down
13 changes: 13 additions & 0 deletions common/pkg/testutils/openapi/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
"k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting"
structuralpruning "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning"
"k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand Down Expand Up @@ -62,6 +63,18 @@ func ValidateClusterVariable(
)}
}

s, err := structuralschema.NewStructural(apiExtensionsSchema)
if err != nil {
return field.ErrorList{field.InternalError(fldPath,
fmt.Errorf(
"failed to create structural schema for variable %q; ClusterClass should be checked: %v",
value.Name,
err,
),
)}
}
defaulting.Default(variableValue, s)

// Validate variable against the schema.
// NOTE: We're reusing a library func used in CRD validation.
if err := validation.ValidateCustomResource(fldPath, variableValue, validator); err != nil {
Expand Down

0 comments on commit b17718a

Please sign in to comment.