From 3470dbd96e18a940c1eeabb6d7575642ba35c361 Mon Sep 17 00:00:00 2001 From: yongruilin Date: Tue, 10 Feb 2026 20:43:31 +0000 Subject: [PATCH] address feedback: add IsAlpha/IsBeta helpers for ValidationStabilityLevel Introduce IsAlpha() and IsBeta() helper methods on field.Error to encapsulate stability level checks. Unexport the underlying ValidationStabilityLevel constants to keep the API surface minimal. Update the apiserver's validation logic to use these helpers. --- .../pkg/util/validation/field/errors.go | 20 ++++++++++---- .../apiserver/pkg/registry/rest/validate.go | 27 +++++++------------ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go index 5d3a39141fd..d44bb0e6088 100644 --- a/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go +++ b/staging/src/k8s.io/apimachinery/pkg/util/validation/field/errors.go @@ -61,19 +61,19 @@ type Error struct { DeclarativeNative bool // ValidationStabilityLevel denotes the validation stability level of the declarative validation from this error is returned. This should be used in the declarative validations only. - ValidationStabilityLevel validationStabilityLevel + ValidationStabilityLevel ValidationStabilityLevel } -// ValidationLevel denotes the stability level of a validation. -type validationStabilityLevel int +// ValidationStabilityLevel denotes the stability level of a validation. +type ValidationStabilityLevel int const ( - unknown validationStabilityLevel = iota + stabilityLevelUnknown ValidationStabilityLevel = iota stabilityLevelAlpha stabilityLevelBeta ) -func (v validationStabilityLevel) String() string { +func (v ValidationStabilityLevel) String() string { switch v { case stabilityLevelAlpha: return "alpha" @@ -86,6 +86,16 @@ func (v validationStabilityLevel) String() string { var _ error = &Error{} +// IsAlpha returns true if the error is an alpha validation error. +func (e *Error) IsAlpha() bool { + return e.ValidationStabilityLevel == stabilityLevelAlpha +} + +// IsBeta returns true if the error is a beta validation error. +func (e *Error) IsBeta() bool { + return e.ValidationStabilityLevel == stabilityLevelBeta +} + // Error implements the error interface. func (e *Error) Error() string { return fmt.Sprintf("%s: %s", e.Field, e.ErrorBody()) diff --git a/staging/src/k8s.io/apiserver/pkg/registry/rest/validate.go b/staging/src/k8s.io/apiserver/pkg/registry/rest/validate.go index fe2df5c4eac..0eadc6a00f9 100644 --- a/staging/src/k8s.io/apiserver/pkg/registry/rest/validate.go +++ b/staging/src/k8s.io/apiserver/pkg/registry/rest/validate.go @@ -251,7 +251,7 @@ func gatherDeclarativeValidationMismatches(imperativeErrs, declarativeErrs field if matchCount == 0 { rec := defaultRecommendation // If the imperative error is explicitly Alpha, it is never enforced, so HV is authoritative. - if iErr.ValidationStabilityLevel.String() == "alpha" { + if iErr.IsAlpha() { rec = authoritativeMsg } @@ -272,7 +272,7 @@ func gatherDeclarativeValidationMismatches(imperativeErrs, declarativeErrs field for _, dErr := range remaining { rec := defaultRecommendation // If the declarative error is Alpha, it is never enforced (shadowed), so HV is authoritative. - if dErr.ValidationStabilityLevel.String() == "alpha" { + if dErr.IsAlpha() { rec = authoritativeMsg } @@ -408,8 +408,7 @@ func ValidateDeclarativelyWithMigrationChecks(ctx context.Context, scheme *runti if cfg.declarativeEnforcement { mismatchCandidateErrs = nil for _, err := range declarativeErrs { - level := err.ValidationStabilityLevel.String() - if level == "alpha" || level == "beta" { + if err.IsAlpha() || err.IsBeta() { mismatchCandidateErrs = append(mismatchCandidateErrs, err) } } @@ -433,8 +432,7 @@ func ValidateDeclarativelyWithMigrationChecks(ctx context.Context, scheme *runti } // Explicit Strategy - level := fe.ValidationStabilityLevel.String() - if level == "beta" { + if fe.IsBeta() { // Beta validations are enforced only if the Beta feature gate is enabled. return betaEnabled } @@ -445,23 +443,16 @@ func ValidateDeclarativelyWithMigrationChecks(ctx context.Context, scheme *runti // Append Enforced DV errors for _, dvErr := range declarativeErrs { - // Internal errors should always fail validation. - if dvErr.Type == field.ErrorTypeInternal { + switch { + case dvErr.Type == field.ErrorTypeInternal: errs = append(errs, dvErr) - continue - } - - level := dvErr.ValidationStabilityLevel.String() - if level == "beta" { + case dvErr.IsBeta(): if betaEnabled { errs = append(errs, dvErr) } - continue + case !dvErr.IsAlpha(): + errs = append(errs, dvErr) // Standard } - if level == "alpha" { - continue // Always shadowed - } - errs = append(errs, dvErr) // Standard } return errs