Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release-1.6] 🌱 Add ClusterClass variables metadata #10320

Merged
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
21 changes: 21 additions & 0 deletions api/v1beta1/clusterclass_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,31 @@ type ClusterClassVariable struct {
// required, this will be specified inside the schema.
Required bool `json:"required"`

// Metadata is the metadata of a variable.
// It can be used to add additional data for higher level tools to
// a ClusterClassVariable.
Metadata ClusterClassVariableMetadata `json:"metadata,omitempty"`

// Schema defines the schema of the variable.
Schema VariableSchema `json:"schema"`
}

// ClusterClassVariableMetadata is the metadata of a variable.
// It can be used to add additional data for higher level tools to
// a ClusterClassVariable.
type ClusterClassVariableMetadata struct {
// Map of string keys and values that can be used to organize and categorize
// (scope and select) variables.
// +optional
Labels map[string]string `json:"labels,omitempty"`

// Annotations is an unstructured key value map that can be used to store and
// retrieve arbitrary metadata.
// They are not queryable.
// +optional
Annotations map[string]string `json:"annotations,omitempty"`
}

// VariableSchema defines the schema of a variable.
type VariableSchema struct {
// OpenAPIV3Schema defines the schema of a variable via OpenAPI v3
Expand Down
30 changes: 30 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

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

55 changes: 54 additions & 1 deletion api/v1beta1/zz_generated.openapi.go

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

19 changes: 19 additions & 0 deletions config/crd/bases/cluster.x-k8s.io_clusterclasses.yaml

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

18 changes: 18 additions & 0 deletions internal/topology/variables/clusterclass_variable_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
structuraldefaulting "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting"
"k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
apivalidation "k8s.io/apimachinery/pkg/api/validation"
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"

Expand Down Expand Up @@ -77,6 +79,9 @@ func validateClusterClassVariable(ctx context.Context, variable *clusterv1.Clust
// Validate variable name.
allErrs = append(allErrs, validateClusterClassVariableName(variable.Name, fldPath.Child("name"))...)

// Validate variable metadata.
allErrs = append(allErrs, validateClusterClassVariableMetadata(variable.Metadata, fldPath.Child("metadata"))...)

// Validate schema.
allErrs = append(allErrs, validateRootSchema(ctx, variable, fldPath.Child("schema", "openAPIV3Schema"))...)

Expand All @@ -101,6 +106,19 @@ func validateClusterClassVariableName(variableName string, fldPath *field.Path)
return allErrs
}

// validateClusterClassVariableMetadata validates a variable metadata.
func validateClusterClassVariableMetadata(metadata clusterv1.ClusterClassVariableMetadata, fldPath *field.Path) field.ErrorList {
allErrs := metav1validation.ValidateLabels(
metadata.Labels,
fldPath.Child("labels"),
)
allErrs = append(allErrs, apivalidation.ValidateAnnotations(
metadata.Annotations,
fldPath.Child("annotations"),
)...)
return allErrs
}

var validVariableTypes = sets.Set[string]{}.Insert("object", "array", "string", "number", "integer", "boolean")

// validateRootSchema validates the schema.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,62 @@ func Test_ValidateClusterClassVariable(t *testing.T) {
},
wantErr: true,
},
{
name: "Valid variable metadata",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "validVariable",
Metadata: clusterv1.ClusterClassVariableMetadata{
Labels: map[string]string{
"label-key": "label-value",
},
Annotations: map[string]string{
"annotation-key": "annotation-value",
},
},
Schema: clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "string",
MinLength: pointer.Int64(1),
},
},
},
},
{
name: "fail on invalid variable label: key does not start with alphanumeric character",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "path.tovariable",
Metadata: clusterv1.ClusterClassVariableMetadata{
Labels: map[string]string{
".label-key": "label-value",
},
},
Schema: clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "string",
MinLength: pointer.Int64(1),
},
},
},
wantErr: true,
},
{
name: "fail on invalid variable annotation: key does not start with alphanumeric character",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Name: "path.tovariable",
Metadata: clusterv1.ClusterClassVariableMetadata{
Annotations: map[string]string{
".annotation-key": "annotation-value",
},
},
Schema: clusterv1.VariableSchema{
OpenAPIV3Schema: clusterv1.JSONSchemaProps{
Type: "string",
MinLength: pointer.Int64(1),
},
},
},
wantErr: true,
},
{
name: "Valid default value regular string",
clusterClassVariable: &clusterv1.ClusterClassVariable{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ spec:
default: kindest
- name: etcdImageTag
required: true
# This metadata has just been added to verify that we can set metadata.
metadata:
labels:
testLabelKey: testLabelValue
annotations:
testAnnotationKey: testAnnotationValue
schema:
openAPIV3Schema:
type: string
Expand Down
Loading