From c1a85e351b045ca11df90da1d41fa55d9e7c4ecb Mon Sep 17 00:00:00 2001 From: Fabian Fischer Date: Wed, 6 Apr 2022 14:57:52 +0200 Subject: [PATCH] Fix invalid warning with newline --- webhooks/ratio_validator.go | 7 +++++-- webhooks/ratio_validator_test.go | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/webhooks/ratio_validator.go b/webhooks/ratio_validator.go index 6e84f70..caeb56e 100644 --- a/webhooks/ratio_validator.go +++ b/webhooks/ratio_validator.go @@ -70,8 +70,11 @@ func (v *RatioValidator) Handle(ctx context.Context, req admission.Request) admi if r.Below(*v.RatioLimit) { return admission.Response{ AdmissionResponse: admissionv1.AdmissionResponse{ - Allowed: true, - Warnings: []string{fmt.Sprintf("Current memory to CPU ratio of %s/core in this namespace is below the fair use ratio of %s/core\n This might lead to additional cost.", r, v.RatioLimit)}, + Allowed: true, + // WARNING(glrf) Warnings MUST NOT contain newlines. K8s will simply drop your warning if you add newlines. + Warnings: []string{ + fmt.Sprintf("Current memory to CPU ratio of %s/core in this namespace is below the fair use ratio of %s/core. This might lead to additional costs.", r, v.RatioLimit), + }, }} } return admission.Allowed("ok") diff --git a/webhooks/ratio_validator_test.go b/webhooks/ratio_validator_test.go index f71727a..c0b31b2 100644 --- a/webhooks/ratio_validator_test.go +++ b/webhooks/ratio_validator_test.go @@ -226,6 +226,9 @@ func TestRatioValidator_Handle(t *testing.T) { assert.True(t, resp.Allowed) if tc.warn { assert.NotEmpty(t, resp.AdmissionResponse.Warnings) + for _, w := range resp.AdmissionResponse.Warnings { + assert.NotContainsf(t, w, "\n", "Warning are not allowed to contain newlines") + } } else { assert.Empty(t, resp.AdmissionResponse.Warnings) }