Repository: kubernetes/kubernetes
File: staging/src/k8s.io/apiserver/pkg/registry/rest/validate.go (Go)

Code before:

	// Call the panic-safe wrapper with the real validation function.
	declarativeErrs := panicSafeValidateFunc(validateDeclaratively, cfg.takeover || cfg.containsDeclarativeNative, cfg.validationIdentifier)(ctx, scheme, obj, oldObj, cfg)

	mirroredDVErrors := field.ErrorList{}
	dvNativeErrors := field.ErrorList{}
	if cfg.containsDeclarativeNative {
		for _, err := range declarativeErrs {
			if err.DeclarativeNative {
				dvNativeErrors = append(dvNativeErrors, err)
				// Internal Error is very unlikely to happen, if that happen it should be fail the validations
				// for both of types of validations.
			} else if err.Type == field.ErrorTypeInternal {
				dvNativeErrors = append(dvNativeErrors, err)
				mirroredDVErrors = append(mirroredDVErrors, err)
			} else {
				mirroredDVErrors = append(mirroredDVErrors, err)
			}
		}
	} else {
		mirroredDVErrors = declarativeErrs
	}

	if declarativeValidationEnabled {
		compareDeclarativeErrorsAndEmitMismatches(ctx, errs, mirroredDVErrors, takeover, validationIdentifier, cfg.normalizationRules)
		if takeover {
			errs = append(errs.RemoveCoveredByDeclarative(), mirroredDVErrors...)
		}
	}
	errs = append(errs, dvNativeErrors...)
	return errs
}

// RecordDuplicateValidationErrors increments a metric and log the error when duplicate validation errors are found.
func RecordDuplicateValidationErrors(ctx context.Context, qualifiedKind schema.GroupKind, errs field.ErrorList) {
	logger := klog.FromContext(ctx)
	seenErrs := make([]string, 0, len(errs))

	for _, err := range errs {
		errStr := fmt.Sprintf("%v", err)

		if slices.Contains(seenErrs, errStr) {
			logger.Info("Found duplicate validation error", "kind", qualifiedKind.String(), "error", errStr)
			validationmetrics.Metrics.IncDuplicateValidationErrorMetric()
		} else {
			seenErrs = append(seenErrs, errStr)
		}
	}
}


Review comment (suggestion):
Add comment somehwere explaining how declarative native validation is skipped in block?  My understanding is that the reason we maintain separate `mirroredDVErrors` and `dvNativeErrors` lists is so that we don't compare declarative native errors with handwritten here, but I don't see that stated in any of the comments and it feels like a somewhat subtle but critical property of this logic.

Code after:
	// Call the panic-safe wrapper with the real validation function.
	declarativeErrs := panicSafeValidateFunc(validateDeclaratively, cfg.takeover || cfg.containsDeclarativeNative, cfg.validationIdentifier)(ctx, scheme, obj, oldObj, cfg)

	mirroredDVErrors := field.ErrorList{}
	dvNativeErrors := field.ErrorList{}

	// When declarative native validation is present, we need to separate declarative native errors
	// from mirrored declarative errors. This is to avoid comparing declarative native errors (which
	// have no imperative equivalent) with handwritten imperative errors.
	if cfg.containsDeclarativeNative {
		for _, err := range declarativeErrs {
			if err.DeclarativeNative {
				dvNativeErrors = append(dvNativeErrors, err)
			} else if err.Type == field.ErrorTypeInternal {
				// Internal errors should fail both types of validation.
				dvNativeErrors = append(dvNativeErrors, err)
				mirroredDVErrors = append(mirroredDVErrors, err)
			} else {
				mirroredDVErrors = append(mirroredDVErrors, err)
			}
		}
	} else {
		mirroredDVErrors = declarativeErrs
	}

	if declarativeValidationEnabled {
		compareDeclarativeErrorsAndEmitMismatches(ctx, errs, mirroredDVErrors, takeover, validationIdentifier, cfg.normalizationRules)
		if takeover {
			errs = append(errs.RemoveCoveredByDeclarative(), mirroredDVErrors...)
		}
	}
	errs = append(errs, dvNativeErrors...)
	return errs
}

// RecordDuplicateValidationErrors increments a metric and log the error when duplicate validation errors are found.
func RecordDuplicateValidationErrors(ctx context.Context, qualifiedKind schema.GroupKind, errs field.ErrorList) {
	logger := klog.FromContext(ctx)
	seenErrs := make([]string, 0, len(errs))

	for _, err := range errs {
		errStr := fmt.Sprintf("%v", err)

		if slices.Contains(seenErrs, errStr) {
			logger.Info("Found duplicate validation error", "kind", qualifiedKind.String(), "error", errStr)
			validationmetrics.Metrics.IncDuplicateValidationErrorMetric()
		} else {
			seenErrs = append(seenErrs, errStr)
		}
	}
}