Skip to content

Commit

Permalink
Allow admission responses to send warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelSpeed committed Sep 16, 2020
1 parent d6829e9 commit 786a866
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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))
})
})
})

0 comments on commit 786a866

Please sign in to comment.