Skip to content

Commit

Permalink
Merge pull request #10320 from k8s-infra-cherrypick-robot/cherry-pick…
Browse files Browse the repository at this point in the history
…-10308-to-release-1.6

[release-1.6] 🌱 Add ClusterClass variables metadata
  • Loading branch information
k8s-ci-robot committed Mar 26, 2024
2 parents 0e2bb35 + 9765ee4 commit c71cd89
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 1 deletion.
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

0 comments on commit c71cd89

Please sign in to comment.