Code Diff
diff --git a/staging/src/k8s.io/apiserver/pkg/registry/rest/create.go b/staging/src/k8s.io/apiserver/pkg/registry/rest/create.go
index 8997fdd832369..faa8d77bc5b3c 100644
--- a/staging/src/k8s.io/apiserver/pkg/registry/rest/create.go
+++ b/staging/src/k8s.io/apiserver/pkg/registry/rest/create.go
@@ -144,25 +144,26 @@ func BeforeCreate(strategy RESTCreateStrategy, ctx context.Context, obj runtime.
// ValidateCreate performs common and strategy-specific validation for a create operation.
func ValidateCreate(ctx context.Context, obj runtime.Object, strategy RESTCreateStrategy) field.ErrorList {
errs := strategy.Validate(ctx, obj)
- if dv, ok := strategy.(DeclarativeValidationStrategy); ok {
- errs = dv.ValidateDeclaratively(ctx, obj, nil, errs, operation.Create, dv.DeclarativeValidationConfig(ctx, obj, nil))
- }
- if len(errs) > 0 {
- return errs
- }
+ if len(errs) == 0 {
+ objectMeta, err := meta.Accessor(obj)
+ if err != nil {
+ return append(errs, field.InternalError(field.NewPath("metadata"), fmt.Errorf("failed to get object metadata: %v", err)))
+ }
- objectMeta, err := meta.Accessor(obj)
- if err != nil {
- return append(errs, field.InternalError(field.NewPath("metadata"), fmt.Errorf("failed to get object metadata: %w", err)))
+ // TODO: Replace this check with the ObjectMeta name validation (validatePathSegment) once we are sure that all other validations are covered by strategy.Validate and strategy.ValidateDeclaratively.
+
+ // Custom validation (including name validation) passed
+ // Now run common validation on object meta
+ // Do this *after* custom validation so that specific error messages are shown whenever possible
+ errs = append(errs, genericvalidation.ValidateObjectMetaAccessor(objectMeta, strategy.NamespaceScoped(), validatePathSegment, field.NewPath("metadata"))...)
}
- // TODO: Replace this check with the ObjectMeta name validation (validatePathSegment) once we are sure that all other validations are covered by strategy.Validate and strategy.ValidateDeclaratively.
+ if dv, ok := strategy.(DeclarativeValidationStrategy); ok {
+ errs = dv.ValidateDeclaratively(ctx, obj, nil, errs, operation.Create, dv.DeclarativeValidationConfig(ctx, obj, nil))
+ }
- // Custom validation (including name validation) passed
- // Now run common validation on object meta
- // Do this *after* custom validation so that specific error messages are shown whenever possible
- return genericvalidation.ValidateObjectMetaAccessor(objectMeta, strategy.NamespaceScoped(), validatePathSegment, field.NewPath("metadata"))
+ return errs
}
// CheckGeneratedNameError checks whether an error that occurred creating a resource is due
diff --git a/staging/src/k8s.io/apiserver/pkg/registry/rest/update.go b/staging/src/k8s.io/apiserver/pkg/registry/rest/update.go
index 80717f79c1965..b66978ca14fc7 100644
--- a/staging/src/k8s.io/apiserver/pkg/registry/rest/update.go
+++ b/staging/src/k8s.io/apiserver/pkg/registry/rest/update.go
@@ -151,7 +151,6 @@ func BeforeUpdate(strategy RESTUpdateStrategy, ctx context.Context, obj, old run
if oldMeta.GetDeletionGracePeriodSeconds() != nil && objectMeta.GetDeletionGracePeriodSeconds() == nil {
objectMeta.SetDeletionGracePeriodSeconds(oldMeta.GetDeletionGracePeriodSeconds())
}
- // Ensure some common fields, like UID, are validated for all resources.
errs := ValidateUpdate(ctx, obj, old, strategy)
if len(errs) > 0 {
RecordDuplicateValidationErrors(ctx, kind.GroupKind(), errs)
@@ -171,6 +170,7 @@ func BeforeUpdate(strategy RESTUpdateStrategy, ctx context.Context, obj, old run
func ValidateUpdate(ctx context.Context, obj runtime.Object, old runtime.Object, strategy RESTUpdateStrategy) field.ErrorList {
// TODO: Replace this check with the ObjectMeta name validation (validatePathSegment) once we are sure that all other validations are covered by strategy.Validate and strategy.ValidateDeclaratively.
+ // Ensure some common fields, like UID, are validated for all resources.
errs := validateCommonFields(obj, old, strategy)
errs = append(errs, strategy.ValidateUpdate(ctx, obj, old)...)