Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow admission responses to send warnings #1157

Merged
merged 1 commit into from
Sep 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/webhook/admission/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,10 @@ func validationResponseFromStatus(allowed bool, status metav1.Status) Response {
}
return resp
}

// WithWarnings adds the given warnings to the Response.
// If any warnings were already given, they will not be overwritten.
func (r Response) WithWarnings(warnings ...string) Response {
r.AdmissionResponse.Warnings = append(r.AdmissionResponse.Warnings, warnings...)
return r
}
26 changes: 26 additions & 0 deletions pkg/webhook/admission/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,30 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
Expect(resp).To(Equal(expected))
})
})

Describe("WithWarnings", func() {
It("should add the warnings to the existing response without removing any existing warnings", func() {
initialResponse := Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
},
Warnings: []string{"existing-warning"},
},
}
warnings := []string{"additional-warning-1", "additional-warning-2"}
expectedResponse := Response{
AdmissionResponse: admissionv1beta1.AdmissionResponse{
Allowed: true,
Result: &metav1.Status{
Code: http.StatusOK,
},
Warnings: []string{"existing-warning", "additional-warning-1", "additional-warning-2"},
},
}

Expect(initialResponse.WithWarnings(warnings...)).To(Equal(expectedResponse))
})
})
})