Skip to content

Commit e36efe3

Browse files
authored
Merge pull request #1157 from JoelSpeed/admission-warnings
✨ Allow admission responses to send warnings
2 parents aee0ef1 + 786a866 commit e36efe3

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pkg/webhook/admission/response.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,10 @@ func validationResponseFromStatus(allowed bool, status metav1.Status) Response {
107107
}
108108
return resp
109109
}
110+
111+
// WithWarnings adds the given warnings to the Response.
112+
// If any warnings were already given, they will not be overwritten.
113+
func (r Response) WithWarnings(warnings ...string) Response {
114+
r.AdmissionResponse.Warnings = append(r.AdmissionResponse.Warnings, warnings...)
115+
return r
116+
}

pkg/webhook/admission/response_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,30 @@ var _ = Describe("Admission Webhook Response Helpers", func() {
216216
Expect(resp).To(Equal(expected))
217217
})
218218
})
219+
220+
Describe("WithWarnings", func() {
221+
It("should add the warnings to the existing response without removing any existing warnings", func() {
222+
initialResponse := Response{
223+
AdmissionResponse: admissionv1beta1.AdmissionResponse{
224+
Allowed: true,
225+
Result: &metav1.Status{
226+
Code: http.StatusOK,
227+
},
228+
Warnings: []string{"existing-warning"},
229+
},
230+
}
231+
warnings := []string{"additional-warning-1", "additional-warning-2"}
232+
expectedResponse := Response{
233+
AdmissionResponse: admissionv1beta1.AdmissionResponse{
234+
Allowed: true,
235+
Result: &metav1.Status{
236+
Code: http.StatusOK,
237+
},
238+
Warnings: []string{"existing-warning", "additional-warning-1", "additional-warning-2"},
239+
},
240+
}
241+
242+
Expect(initialResponse.WithWarnings(warnings...)).To(Equal(expectedResponse))
243+
})
244+
})
219245
})

0 commit comments

Comments
 (0)