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

fix: Add schema defaults for CSI #10

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 29 additions & 23 deletions api/v1alpha1/addon_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

"github.com/d2iq-labs/cluster-api-runtime-extensions-nutanix/api/variables"
Expand Down Expand Up @@ -36,7 +37,7 @@ type Addons struct {
CPI *CPI `json:"cpi,omitempty"`

// +optional
CSIProviders *CSIProviders `json:"csi,omitempty"`
CSI *CSI `json:"csi,omitempty"`
}

func (Addons) VariableSchema() clusterv1.VariableSchema {
Expand All @@ -48,7 +49,7 @@ func (Addons) VariableSchema() clusterv1.VariableSchema {
"cni": CNI{}.VariableSchema().OpenAPIV3Schema,
"nfd": NFD{}.VariableSchema().OpenAPIV3Schema,
"clusterAutoscaler": ClusterAutoscaler{}.VariableSchema().OpenAPIV3Schema,
"csi": CSIProviders{}.VariableSchema().OpenAPIV3Schema,
"csi": CSI{}.VariableSchema().OpenAPIV3Schema,
"cpi": CPI{}.VariableSchema().OpenAPIV3Schema,
},
},
Expand Down Expand Up @@ -147,7 +148,7 @@ type DefaultStorage struct {
StorageClassConfigName string `json:"storageClassConfigName"`
}

type CSIProviders struct {
type CSI struct {
// +optional
Providers []CSIProvider `json:"providers,omitempty"`
// +optional
Expand Down Expand Up @@ -191,24 +192,34 @@ func (StorageClassConfig) VariableSchema() clusterv1.VariableSchema {
}
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "object",
Type: "object",
Required: []string{"name", "parameters", "reclaimPolicy", "volumeBindingMode"},
Properties: map[string]clusterv1.JSONSchemaProps{
"name": {
Type: "string",
Description: "Name of storage class config.",
MinLength: ptr.To(int64(1)),
},
"parameters": {
Type: "object",
Description: "Parameters passed into the storage class object.",
XPreserveUnknownFields: true,
Type: "object",
Description: "Parameters passed into the storage class object.",
AdditionalProperties: &clusterv1.JSONSchemaProps{
Type: "string",
},
Default: variables.MustMarshal(map[string]string{
"csi.storage.k8s.io/fstype": "ext4",
"type": "gp3",
}),
},
"reclaimPolicy": {
Type: "string",
Enum: variables.MustMarshalValuesToEnumJSON(supportedReclaimPolicies...),
Type: "string",
Enum: variables.MustMarshalValuesToEnumJSON(supportedReclaimPolicies...),
Default: variables.MustMarshal(VolumeReclaimDelete),
},
"volumeBindingMode": {
Type: "string",
Enum: variables.MustMarshalValuesToEnumJSON(supportedBindingModes...),
Type: "string",
Enum: variables.MustMarshalValuesToEnumJSON(supportedBindingModes...),
Default: variables.MustMarshal(VolumeBindingImmediate),
},
},
},
Expand All @@ -219,7 +230,8 @@ func (CSIProvider) VariableSchema() clusterv1.VariableSchema {
supportedCSIProviders := []string{CSIProviderAWSEBS, CSIProviderNutanix}
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "object",
Type: "object",
Required: []string{"name", "strategy"},
Properties: map[string]clusterv1.JSONSchemaProps{
"name": {
Description: "Name of the CSI Provider",
Expand Down Expand Up @@ -248,11 +260,8 @@ func (CSIProvider) VariableSchema() clusterv1.VariableSchema {
},
},
"storageClassConfig": {
Type: "array",
Items: &clusterv1.JSONSchemaProps{
Type: "object",
Properties: StorageClassConfig{}.VariableSchema().OpenAPIV3Schema.Properties,
},
Type: "array",
Items: ptr.To(StorageClassConfig{}.VariableSchema().OpenAPIV3Schema),
},
},
},
Expand Down Expand Up @@ -281,17 +290,14 @@ func (DefaultStorage) VariableSchema() clusterv1.VariableSchema {
}
}

func (CSIProviders) VariableSchema() clusterv1.VariableSchema {
func (CSI) VariableSchema() clusterv1.VariableSchema {
return clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "object",
Properties: map[string]clusterv1.JSONSchemaProps{
"providers": {
Type: "array",
Items: &clusterv1.JSONSchemaProps{
Type: "object",
Properties: CSIProvider{}.VariableSchema().OpenAPIV3Schema.Properties,
},
Type: "array",
Items: ptr.To(CSIProvider{}.VariableSchema().OpenAPIV3Schema),
},
"defaultStorage": DefaultStorage{}.VariableSchema().OpenAPIV3Schema,
},
Expand Down
48 changes: 24 additions & 24 deletions api/v1alpha1/zz_generated.deepcopy.go

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

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
2 changes: 1 addition & 1 deletion pkg/handlers/generic/lifecycle/csi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *CSIHandler) AfterControlPlaneInitialized(
)
varMap := variables.ClusterVariablesToVariablesMap(req.Cluster.Spec.Topology.Variables)
resp.SetStatus(runtimehooksv1.ResponseStatusSuccess)
csiProviders, found, err := variables.Get[v1alpha1.CSIProviders](
csiProviders, found, err := variables.Get[v1alpha1.CSI](
varMap,
c.variableName,
c.variablePath...)
Expand Down
Loading