rs: API warn when name is not DNS label

This commit is contained in:
Tim Hockin 2022-12-11 13:32:37 -08:00
parent e27cf75094
commit 820e2fff0d
No known key found for this signature in database

View File

@ -30,6 +30,7 @@ import (
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/registry/generic"
@ -39,7 +40,7 @@ import (
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/api/pod"
"k8s.io/kubernetes/pkg/apis/apps"
"k8s.io/kubernetes/pkg/apis/apps/validation"
appsvalidation "k8s.io/kubernetes/pkg/apis/apps/validation"
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
)
@ -113,13 +114,18 @@ func (rsStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object)
func (rsStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
rs := obj.(*apps.ReplicaSet)
opts := pod.GetValidationOptionsFromPodTemplate(&rs.Spec.Template, nil)
return validation.ValidateReplicaSet(rs, opts)
return appsvalidation.ValidateReplicaSet(rs, opts)
}
// WarningsOnCreate returns warnings for the creation of the given object.
func (rsStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
newRS := obj.(*apps.ReplicaSet)
return pod.GetWarningsForPodTemplate(ctx, field.NewPath("spec", "template"), &newRS.Spec.Template, nil)
var warnings []string
if msgs := utilvalidation.IsDNS1123Label(newRS.Name); len(msgs) != 0 {
warnings = append(warnings, fmt.Sprintf("metadata.name: this is used in Pod names and hostnames, which can result in surprising behavior; a DNS label is recommended: %v", msgs))
}
warnings = append(warnings, pod.GetWarningsForPodTemplate(ctx, field.NewPath("spec", "template"), &newRS.Spec.Template, nil)...)
return warnings
}
// Canonicalize normalizes the object after validation.
@ -138,8 +144,8 @@ func (rsStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) f
oldReplicaSet := old.(*apps.ReplicaSet)
opts := pod.GetValidationOptionsFromPodTemplate(&newReplicaSet.Spec.Template, &oldReplicaSet.Spec.Template)
allErrs := validation.ValidateReplicaSet(obj.(*apps.ReplicaSet), opts)
allErrs = append(allErrs, validation.ValidateReplicaSetUpdate(newReplicaSet, oldReplicaSet, opts)...)
allErrs := appsvalidation.ValidateReplicaSet(obj.(*apps.ReplicaSet), opts)
allErrs = append(allErrs, appsvalidation.ValidateReplicaSetUpdate(newReplicaSet, oldReplicaSet, opts)...)
// Update is not allowed to set Spec.Selector for all groups/versions except extensions/v1beta1.
// If RequestInfo is nil, it is better to revert to old behavior (i.e. allow update to set Spec.Selector)
@ -228,7 +234,7 @@ func (rsStatusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.O
}
func (rsStatusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return validation.ValidateReplicaSetStatusUpdate(obj.(*apps.ReplicaSet), old.(*apps.ReplicaSet))
return appsvalidation.ValidateReplicaSetStatusUpdate(obj.(*apps.ReplicaSet), old.(*apps.ReplicaSet))
}
// WarningsOnUpdate returns warnings for the given update.