Skip to content

Commit d1a29ee

Browse files
authored
Merge pull request #1334 from christopherhein/add-request-context-func
✨ Adding WithContextFunc for webhook server to pass info to the handler
2 parents 17b28b4 + 925c552 commit d1a29ee

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

pkg/webhook/admission/http.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ var _ http.Handler = &Webhook{}
4545
func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4646
var body []byte
4747
var err error
48+
ctx := r.Context()
49+
if wh.WithContextFunc != nil {
50+
ctx = wh.WithContextFunc(ctx, r)
51+
}
4852

4953
var reviewResponse Response
5054
if r.Body != nil {
@@ -93,7 +97,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9397
wh.log.V(1).Info("received request", "UID", req.UID, "kind", req.Kind, "resource", req.Resource)
9498

9599
// TODO: add panic-recovery for Handle
96-
reviewResponse = wh.Handle(r.Context(), req)
100+
reviewResponse = wh.Handle(ctx, req)
97101
wh.writeResponseTyped(w, reviewResponse, actualAdmRevGVK)
98102
}
99103

pkg/webhook/admission/http_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,34 @@ var _ = Describe("Admission Webhooks", func() {
162162
webhook.ServeHTTP(respRecorder, req.WithContext(ctx))
163163
Expect(respRecorder.Body.String()).To(Equal(expected))
164164
})
165+
166+
It("should mutate the Context from the HTTP request, if func supplied", func() {
167+
req := &http.Request{
168+
Header: http.Header{"Content-Type": []string{"application/json"}},
169+
Body: nopCloser{Reader: bytes.NewBufferString(`{"request":{}}`)},
170+
}
171+
type ctxkey int
172+
const key ctxkey = 1
173+
webhook := &Webhook{
174+
Handler: &fakeHandler{
175+
fn: func(ctx context.Context, req Request) Response {
176+
return Allowed(ctx.Value(key).(string))
177+
},
178+
},
179+
WithContextFunc: func(ctx context.Context, r *http.Request) context.Context {
180+
return context.WithValue(ctx, key, r.Header["Content-Type"][0])
181+
},
182+
log: logf.RuntimeLog.WithName("webhook"),
183+
}
184+
185+
expected := fmt.Sprintf(`{%s,"response":{"uid":"","allowed":true,"status":{"metadata":{},"reason":%q,"code":200}}}
186+
`, gvkJSONv1, "application/json")
187+
188+
ctx, cancel := context.WithCancel(context.Background())
189+
cancel()
190+
webhook.ServeHTTP(respRecorder, req.WithContext(ctx))
191+
Expect(respRecorder.Body.String()).To(Equal(expected))
192+
})
165193
})
166194
})
167195

pkg/webhook/admission/webhook.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ type Webhook struct {
115115
// and potentially patches to apply to the handler.
116116
Handler Handler
117117

118+
// WithContextFunc will allow you to take the http.Request.Context() and
119+
// add any additional information such as passing the request path or
120+
// headers thus allowing you to read them from within the handler
121+
WithContextFunc func(context.Context, *http.Request) context.Context
122+
118123
// decoder is constructed on receiving a scheme and passed down to then handler
119124
decoder *Decoder
120125

0 commit comments

Comments
 (0)