Skip to content

Commit

Permalink
Add validation for the nested ObjectMeta fields of cluster class
Browse files Browse the repository at this point in the history
  • Loading branch information
LuBingtan committed Mar 31, 2023
1 parent 87784c2 commit e32f8f3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/v1beta1/common_validate.go
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions internal/webhooks/clusterclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"))
}

0 comments on commit e32f8f3

Please sign in to comment.