diff --git a/api/v1beta1/common_validate.go b/api/v1beta1/common_validate.go index 46bf685811b2..3e1fa770a59b 100644 --- a/api/v1beta1/common_validate.go +++ b/api/v1beta1/common_validate.go @@ -1,3 +1,19 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + package v1beta1 import ( @@ -6,6 +22,7 @@ import ( "k8s.io/apimachinery/pkg/util/validation/field" ) +// Validate validates the labels and annotations in ObjectMeta. func (metadata *ObjectMeta) Validate(parent *field.Path) field.ErrorList { allErrs := metav1validation.ValidateLabels( metadata.Labels, diff --git a/internal/webhooks/clusterclass.go b/internal/webhooks/clusterclass.go index 0e3b65886d1f..86b34455a24c 100644 --- a/internal/webhooks/clusterclass.go +++ b/internal/webhooks/clusterclass.go @@ -155,6 +155,9 @@ func (webhook *ClusterClass) validate(ctx context.Context, oldClusterClass, newC // Validate patches. allErrs = append(allErrs, validatePatches(newClusterClass)...) + // Validate metadata + allErrs = append(allErrs, validateClusterClassMetadata(newClusterClass)...) + // If this is an update run additional validation. if oldClusterClass != nil { // Ensure spec changes are compatible. @@ -364,3 +367,17 @@ func validateMachineHealthCheckClass(fldPath *field.Path, namepace string, m *cl return mhc.ValidateCommonFields(fldPath) } + +func validateClusterClassMetadata(clusterClass *clusterv1.ClusterClass) field.ErrorList { + var allErrs field.ErrorList + allErrs = append(allErrs, clusterClass.Spec.ControlPlane.Metadata.Validate(field.NewPath("spec", "controlPlane", "metadata"))...) + workerPath := field.NewPath("spec", "workers", "machineDeployments") + for idx, m := range clusterClass.Spec.Workers.MachineDeployments { + allErrs = append(allErrs, validateMachineDeploymentMetadata(m, workerPath.Index(idx))...) + } + return allErrs +} + +func validateMachineDeploymentMetadata(m clusterv1.MachineDeploymentClass, fldPath *field.Path) field.ErrorList { + return m.Template.Metadata.Validate(fldPath.Child("template", "metadata")) +}