mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-07-17 20:00:07 +00:00
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.
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user