Skip to content

Commit 5e210ca

Browse files
committed
Add validation for nested ObjectMeta fields in webhook
1 parent da10c33 commit 5e210ca

File tree

9 files changed

+57
-1
lines changed

9 files changed

+57
-1
lines changed

api/v1beta1/common_validate.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package v1beta1
2+
3+
import (
4+
metavalidation "k8s.io/apimachinery/pkg/api/validation"
5+
metav1validation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
6+
"k8s.io/apimachinery/pkg/util/validation/field"
7+
)
8+
9+
func (metadata *ObjectMeta) Validate(parent *field.Path) field.ErrorList {
10+
allErrs := metav1validation.ValidateLabels(
11+
metadata.Labels,
12+
parent.Child("labels"),
13+
)
14+
allErrs = append(allErrs, metavalidation.ValidateAnnotations(
15+
metadata.Annotations,
16+
parent.Child("annotations"),
17+
)...)
18+
return allErrs
19+
}

api/v1beta1/machinedeployment_webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ func (m *MachineDeployment) validate(old *MachineDeployment) error {
266266
}
267267
}
268268

269+
// Validate the metadata of the template.
270+
allErrs = append(allErrs, m.Spec.Template.ObjectMeta.Validate(specPath.Child("template", "metadata"))...)
271+
269272
if len(allErrs) == 0 {
270273
return nil
271274
}

api/v1beta1/machineset_webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ func (m *MachineSet) validate(old *MachineSet) error {
152152
}
153153
}
154154

155+
// Validate the metadata of the template.
156+
allErrs = append(allErrs, m.Spec.Template.ObjectMeta.Validate(specPath.Child("template", "metadata"))...)
157+
155158
if len(allErrs) == 0 {
156159
return nil
157160
}

bootstrap/kubeadm/api/v1beta1/kubeadmconfigtemplate_webhook.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func (r *KubeadmConfigTemplateSpec) validate(name string) error {
6363
var allErrs field.ErrorList
6464

6565
allErrs = append(allErrs, r.Template.Spec.Validate(field.NewPath("spec", "template", "spec"))...)
66+
// Validate the metadata of the template.
67+
allErrs = append(allErrs, r.Template.ObjectMeta.Validate(field.NewPath("spec", "template", "metadata"))...)
6668

6769
if len(allErrs) == 0 {
6870
return nil

controlplane/kubeadm/api/v1beta1/kubeadm_control_plane_webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ func validateKubeadmControlPlaneSpec(s KubeadmControlPlaneSpec, namespace string
330330
)
331331
}
332332

333+
// Validate the metadata of the MachineTemplate
334+
allErrs = append(allErrs, s.MachineTemplate.ObjectMeta.Validate(pathPrefix.Child("machineTemplate", "metadata"))...)
335+
333336
if !version.KubeSemver.MatchString(s.Version) {
334337
allErrs = append(allErrs, field.Invalid(pathPrefix.Child("version"), s.Version, "must be a valid semantic version"))
335338
}

controlplane/kubeadm/api/v1beta1/kubeadmcontrolplanetemplate_webhook.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ func (r *KubeadmControlPlaneTemplate) ValidateCreate() (admission.Warnings, erro
6969
allErrs := validateKubeadmControlPlaneTemplateResourceSpec(spec, field.NewPath("spec", "template", "spec"))
7070
allErrs = append(allErrs, validateClusterConfiguration(spec.KubeadmConfigSpec.ClusterConfiguration, nil, field.NewPath("spec", "template", "spec", "kubeadmConfigSpec", "clusterConfiguration"))...)
7171
allErrs = append(allErrs, spec.KubeadmConfigSpec.Validate(field.NewPath("spec", "template", "spec", "kubeadmConfigSpec"))...)
72+
// Validate the metadata of the KubeadmControlPlaneTemplateResource
73+
allErrs = append(allErrs, r.Spec.Template.ObjectMeta.Validate(field.NewPath("spec", "template", "metadata"))...)
7274
if len(allErrs) > 0 {
7375
return nil, apierrors.NewInvalid(GroupVersion.WithKind("KubeadmControlPlaneTemplate").GroupKind(), r.Name, allErrs)
7476
}
@@ -108,5 +110,10 @@ func validateKubeadmControlPlaneTemplateResourceSpec(s KubeadmControlPlaneTempla
108110
allErrs = append(allErrs, validateRolloutBefore(s.RolloutBefore, pathPrefix.Child("rolloutBefore"))...)
109111
allErrs = append(allErrs, validateRolloutStrategy(s.RolloutStrategy, nil, pathPrefix.Child("rolloutStrategy"))...)
110112

113+
if s.MachineTemplate != nil {
114+
// Validate the metadata of the MachineTemplate
115+
allErrs = append(allErrs, s.MachineTemplate.ObjectMeta.Validate(pathPrefix.Child("machineTemplate", "metadata"))...)
116+
}
117+
111118
return allErrs
112119
}

exp/api/v1beta1/machinepool_webhook.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ func (m *MachinePool) validate(old *MachinePool) error {
152152
}
153153
}
154154

155+
// Validate the metadata of the MachinePool template.
156+
allErrs = append(allErrs, m.Spec.Template.ObjectMeta.Validate(specPath.Child("template", "metadata"))...)
157+
155158
if len(allErrs) == 0 {
156159
return nil
157160
}

test/infrastructure/docker/api/v1beta1/dockerclustertemplate_webhook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ func (r *DockerClusterTemplate) ValidateCreate() (admission.Warnings, error) {
6363
}
6464

6565
allErrs := validateDockerClusterSpec(r.Spec.Template.Spec)
66+
67+
// Validate the metadata of the template.
68+
allErrs = append(allErrs, r.Spec.Template.ObjectMeta.Validate(field.NewPath("spec", "template", "metadata"))...)
69+
6670
if len(allErrs) > 0 {
6771
return nil, apierrors.NewInvalid(GroupVersion.WithKind("DockerClusterTemplate").GroupKind(), r.Name, allErrs)
6872
}

test/infrastructure/docker/api/v1beta1/dockermachinetemplate_webhook.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,16 @@ type DockerMachineTemplateWebhook struct{}
4949
var _ webhook.CustomValidator = &DockerMachineTemplateWebhook{}
5050

5151
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type.
52-
func (*DockerMachineTemplateWebhook) ValidateCreate(_ context.Context, _ runtime.Object) (admission.Warnings, error) {
52+
func (*DockerMachineTemplateWebhook) ValidateCreate(_ context.Context, raw runtime.Object) (admission.Warnings, error) {
53+
obj, ok := raw.(*DockerMachineTemplate)
54+
if !ok {
55+
return nil, apierrors.NewBadRequest(fmt.Sprintf("expected a DockerMachineTemplate but got a %T", raw))
56+
}
57+
// Validate the metadata of the template.
58+
allErrs := obj.Spec.Template.ObjectMeta.Validate(field.NewPath("spec", "template", "metadata"))
59+
if len(allErrs) > 0 {
60+
return nil, apierrors.NewInvalid(GroupVersion.WithKind("DockerClusterTemplate").GroupKind(), obj.Name, allErrs)
61+
}
5362
return nil, nil
5463
}
5564

@@ -74,6 +83,9 @@ func (*DockerMachineTemplateWebhook) ValidateUpdate(ctx context.Context, oldRaw
7483
!reflect.DeepEqual(newObj.Spec.Template.Spec, oldObj.Spec.Template.Spec) {
7584
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "template", "spec"), newObj, dockerMachineTemplateImmutableMsg))
7685
}
86+
// Validate the metadata of the template.
87+
allErrs = append(allErrs, newObj.Spec.Template.ObjectMeta.Validate(field.NewPath("spec", "template", "metadata"))...)
88+
7789
if len(allErrs) == 0 {
7890
return nil, nil
7991
}

0 commit comments

Comments
 (0)