Skip to content

Commit

Permalink
Merge pull request #9 from appuio/fix/newline-in-warning
Browse files Browse the repository at this point in the history
Fix invalid warning with newline
  • Loading branch information
glrf authored Apr 6, 2022
2 parents 9216e2a + c1a85e3 commit 70599c2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions webhooks/ratio_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions webhooks/ratio_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 70599c2

Please sign in to comment.