Skip to content

Commit cf43bf7

Browse files
committed
Add validation for the nested ObjectMeta fields of cluster class
1 parent 5e210ca commit cf43bf7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

api/v1beta1/common_validate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package v1beta1
218

319
import (
@@ -6,6 +22,7 @@ import (
622
"k8s.io/apimachinery/pkg/util/validation/field"
723
)
824

25+
// Validate validates the labels and annotations in ObjectMeta.
926
func (metadata *ObjectMeta) Validate(parent *field.Path) field.ErrorList {
1027
allErrs := metav1validation.ValidateLabels(
1128
metadata.Labels,

internal/webhooks/clusterclass.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ func (webhook *ClusterClass) validate(ctx context.Context, oldClusterClass, newC
156156
// Validate patches.
157157
allErrs = append(allErrs, validatePatches(newClusterClass)...)
158158

159+
// Validate metadata
160+
allErrs = append(allErrs, validateClusterClassMetadata(newClusterClass)...)
161+
159162
// If this is an update run additional validation.
160163
if oldClusterClass != nil {
161164
// Ensure spec changes are compatible.
@@ -365,3 +368,17 @@ func validateMachineHealthCheckClass(fldPath *field.Path, namepace string, m *cl
365368

366369
return mhc.ValidateCommonFields(fldPath)
367370
}
371+
372+
func validateClusterClassMetadata(clusterClass *clusterv1.ClusterClass) field.ErrorList {
373+
var allErrs field.ErrorList
374+
allErrs = append(allErrs, clusterClass.Spec.ControlPlane.Metadata.Validate(field.NewPath("spec", "controlPlane", "metadata"))...)
375+
workerPath := field.NewPath("spec", "workers", "machineDeployments")
376+
for idx, m := range clusterClass.Spec.Workers.MachineDeployments {
377+
allErrs = append(allErrs, validateMachineDeploymentMetadata(m, workerPath.Index(idx))...)
378+
}
379+
return allErrs
380+
}
381+
382+
func validateMachineDeploymentMetadata(m clusterv1.MachineDeploymentClass, fldPath *field.Path) field.ErrorList {
383+
return m.Template.Metadata.Validate(fldPath.Child("template", "metadata"))
384+
}

0 commit comments

Comments
 (0)