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

✨ Pass HTTP request Context through to admission Webhook handlers #549

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
3 changes: 1 addition & 2 deletions pkg/webhook/admission/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package admission

import (
"context"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -84,7 +83,7 @@ func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

// TODO: add panic-recovery for Handle
reviewResponse = wh.Handle(context.Background(), req)
reviewResponse = wh.Handle(r.Context(), req)
wh.writeResponse(w, reviewResponse)
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/webhook/admission/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package admission
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -94,6 +95,32 @@ var _ = Describe("Admission Webhooks", func() {
webhook.ServeHTTP(respRecorder, req)
Expect(respRecorder.Body.Bytes()).To(Equal(expected))
})

It("should present the Context from the HTTP request, if any", func() {
req := &http.Request{
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: nopCloser{Reader: bytes.NewBufferString(`{"request":{}}`)},
}
type ctxkey int
const key ctxkey = 1
const value = "from-ctx"
webhook := &Webhook{
Handler: &fakeHandler{
fn: func(ctx context.Context, req Request) Response {
<-ctx.Done()
return Allowed(ctx.Value(key).(string))
},
},
}

expected := []byte(fmt.Sprintf(`{"response":{"uid":"","allowed":true,"status":{"metadata":{},"reason":%q,"code":200}}}
`, value))

ctx, cancel := context.WithCancel(context.WithValue(context.Background(), key, value))
cancel()
webhook.ServeHTTP(respRecorder, req.WithContext(ctx))
Expect(respRecorder.Body.Bytes()).To(Equal(expected))
})
})
})

Expand Down
4 changes: 4 additions & 0 deletions pkg/webhook/admission/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (r *Response) Complete(req Request) error {

// Handler can handle an AdmissionRequest.
type Handler interface {
// Handle yields a response to an AdmissionRequest.
//
// The supplied context is extracted from the received http.Request, allowing wrapping
// http.Handlers to inject values into and control cancelation of downstream request processing.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In proxy server terms, the inner handlers are analogous to "upstream" servers. Given that, if my use of "downstream" is confusing, please let me know and I can try again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh, I think it's clear enough if you're vaguely familiar with context. We'll see if anyone complains :-)

Handle(context.Context, Request) Response
}

Expand Down