Skip to content

Commit 0874508

Browse files
committed
chore: change Info->Error log level related to declarative validation runtime tests and refactor panic wrapper names
1 parent 21f7eaa commit 0874508

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

staging/src/k8s.io/apiserver/pkg/registry/rest/validate.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func CompareDeclarativeErrorsAndEmitMismatches(ctx context.Context, imperativeEr
116116
mismatchDetails := gatherDeclarativeValidationMismatches(imperativeErrs, declarativeErrs, takeover)
117117
for _, detail := range mismatchDetails {
118118
// Log information about the mismatch using contextual logger
119-
logger.Info(detail)
119+
logger.Error(nil, detail)
120120

121121
// Increment the metric for the mismatch
122122
validationmetrics.Metrics.IncDeclarativeValidationMismatchMetric()
@@ -231,20 +231,20 @@ func createDeclarativeValidationPanicHandler(ctx context.Context, errs *field.Er
231231
// If takeover is enabled, output as a validation error as authoritative validator panicked and validation should error
232232
*errs = append(*errs, field.InternalError(nil, fmt.Errorf(errorFmt, r)))
233233
} else {
234-
// if takeover not enabled, log the panic as an info message
235-
logger.Info(fmt.Sprintf(errorFmt, r))
234+
// if takeover not enabled, log the panic as an error message
235+
logger.Error(nil, fmt.Sprintf(errorFmt, r))
236236
}
237237
}
238238
}
239239
}
240240

241-
// withRecover wraps a validation function with panic recovery logic.
241+
// panicSafeValidateFunc wraps a validation function with panic recovery logic.
242242
// It takes a validation function with the ValidateDeclaratively signature
243243
// and returns a function with the same signature.
244244
// The returned function will execute the wrapped function and handle any panics by
245245
// incrementing the panic metric, and logging an error message
246246
// if takeover=false, and adding a validation error if takeover=true.
247-
func withRecover(
247+
func panicSafeValidateFunc(
248248
validateFunc func(ctx context.Context, options sets.Set[string], scheme *runtime.Scheme, obj runtime.Object) field.ErrorList,
249249
takeover bool,
250250
) func(ctx context.Context, options sets.Set[string], scheme *runtime.Scheme, obj runtime.Object) field.ErrorList {
@@ -255,13 +255,13 @@ func withRecover(
255255
}
256256
}
257257

258-
// withRecoverUpdate wraps an update validation function with panic recovery logic.
258+
// panicSafeValidateUpdateFunc wraps an update validation function with panic recovery logic.
259259
// It takes a validation function with the ValidateUpdateDeclaratively signature
260260
// and returns a function with the same signature.
261261
// The returned function will execute the wrapped function and handle any panics by
262262
// incrementing the panic metric, and logging an error message
263263
// if takeover=false, and adding a validation error if takeover=true.
264-
func withRecoverUpdate(
264+
func panicSafeValidateUpdateFunc(
265265
validateUpdateFunc func(ctx context.Context, options sets.Set[string], scheme *runtime.Scheme, obj, oldObj runtime.Object) field.ErrorList,
266266
takeover bool,
267267
) func(ctx context.Context, options sets.Set[string], scheme *runtime.Scheme, obj, oldObj runtime.Object) field.ErrorList {
@@ -292,7 +292,7 @@ func withRecoverUpdate(
292292
// conversion fails, or if a panic occurs during validation when
293293
// takeover is true.
294294
func ValidateDeclarativelyWithRecovery(ctx context.Context, options sets.Set[string], scheme *runtime.Scheme, obj runtime.Object, takeover bool) field.ErrorList {
295-
return withRecover(ValidateDeclaratively, takeover)(ctx, options, scheme, obj)
295+
return panicSafeValidateFunc(ValidateDeclaratively, takeover)(ctx, options, scheme, obj)
296296
}
297297

298298
// ValidateUpdateDeclarativelyWithRecovery validates obj and oldObj against declarative
@@ -315,5 +315,5 @@ func ValidateDeclarativelyWithRecovery(ctx context.Context, options sets.Set[str
315315
// conversion fails, or if a panic occurs during validation when
316316
// takeover is true.
317317
func ValidateUpdateDeclarativelyWithRecovery(ctx context.Context, options sets.Set[string], scheme *runtime.Scheme, obj, oldObj runtime.Object, takeover bool) field.ErrorList {
318-
return withRecoverUpdate(ValidateUpdateDeclaratively, takeover)(ctx, options, scheme, obj, oldObj)
318+
return panicSafeValidateUpdateFunc(ValidateUpdateDeclaratively, takeover)(ctx, options, scheme, obj, oldObj)
319319
}

staging/src/k8s.io/apiserver/pkg/registry/rest/validate_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,8 @@ func TestCompareDeclarativeErrorsAndEmitMismatches(t *testing.T) {
394394
declarativeErrs: field.ErrorList{errA},
395395
takeover: true,
396396
expectLogs: true,
397-
// logs have a prefix of the form - I0309 21:05:33.865030 1926106 validate.go:199]
398-
expectedRegex: "I.*Unexpected difference between hand written validation and declarative validation error results.*Consider disabling the DeclarativeValidationTakeover feature gate to keep data persisted in etcd consistent with prior versions of Kubernetes",
397+
// logs have a prefix of the form - E0309 21:05:33.865030 1926106 validate.go:199]
398+
expectedRegex: "E.*Unexpected difference between hand written validation and declarative validation error results.*Consider disabling the DeclarativeValidationTakeover feature gate to keep data persisted in etcd consistent with prior versions of Kubernetes",
399399
},
400400
{
401401
name: "matching errors, don't log info",
@@ -468,8 +468,8 @@ func TestWithRecover(t *testing.T) {
468468
},
469469
takeoverEnabled: false,
470470
wantErrs: nil,
471-
// logs have a prefix of the form - I0309 21:05:33.865030 1926106 validate.go:199]
472-
expectLogRegex: "I.*panic during declarative validation: test panic",
471+
// logs have a prefix of the form - E0309 21:05:33.865030 1926106 validate.go:199]
472+
expectLogRegex: "E.*panic during declarative validation: test panic",
473473
},
474474
{
475475
name: "panic with takeover enabled",
@@ -500,16 +500,16 @@ func TestWithRecover(t *testing.T) {
500500
klog.LogToStderr(false)
501501
defer klog.LogToStderr(true)
502502

503-
// Pass the takeover flag to withRecover instead of relying on the feature gate
504-
wrapped := withRecover(tc.validateFn, tc.takeoverEnabled)
503+
// Pass the takeover flag to panicSafeValidateFunc instead of relying on the feature gate
504+
wrapped := panicSafeValidateFunc(tc.validateFn, tc.takeoverEnabled)
505505
gotErrs := wrapped(ctx, options, scheme, obj)
506506

507507
klog.Flush()
508508
logOutput := buf.String()
509509

510510
// Compare gotErrs vs. tc.wantErrs
511511
if !equalErrorLists(gotErrs, tc.wantErrs) {
512-
t.Errorf("withRecover() gotErrs = %#v, want %#v", gotErrs, tc.wantErrs)
512+
t.Errorf("panicSafeValidateFunc() gotErrs = %#v, want %#v", gotErrs, tc.wantErrs)
513513
}
514514

515515
// Check logs if needed
@@ -562,8 +562,8 @@ func TestWithRecoverUpdate(t *testing.T) {
562562
},
563563
takeoverEnabled: false,
564564
wantErrs: nil,
565-
// logs have a prefix of the form - I0309 21:05:33.865030 1926106 validate.go:199]
566-
expectLogRegex: "I.*panic during declarative validation: test update panic",
565+
// logs have a prefix of the form - E0309 21:05:33.865030 1926106 validate.go:199]
566+
expectLogRegex: "E.*panic during declarative validation: test update panic",
567567
},
568568
{
569569
name: "panic with takeover enabled",
@@ -594,16 +594,16 @@ func TestWithRecoverUpdate(t *testing.T) {
594594
klog.LogToStderr(false)
595595
defer klog.LogToStderr(true)
596596

597-
// Pass the takeover flag to withRecoverUpdate instead of relying on the feature gate
598-
wrapped := withRecoverUpdate(tc.validateFn, tc.takeoverEnabled)
597+
// Pass the takeover flag to panicSafeValidateUpdateFunc instead of relying on the feature gate
598+
wrapped := panicSafeValidateUpdateFunc(tc.validateFn, tc.takeoverEnabled)
599599
gotErrs := wrapped(ctx, options, scheme, obj, oldObj)
600600

601601
klog.Flush()
602602
logOutput := buf.String()
603603

604604
// Compare gotErrs with wantErrs
605605
if !equalErrorLists(gotErrs, tc.wantErrs) {
606-
t.Errorf("withRecoverUpdate() gotErrs = %#v, want %#v", gotErrs, tc.wantErrs)
606+
t.Errorf("panicSafeValidateUpdateFunc() gotErrs = %#v, want %#v", gotErrs, tc.wantErrs)
607607
}
608608

609609
// Verify log output

0 commit comments

Comments
 (0)