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

✨ Ability to modify Admissions Request context #1333

Closed
christopherhein opened this issue Jan 13, 2021 · 2 comments · Fixed by #1334
Closed

✨ Ability to modify Admissions Request context #1333

christopherhein opened this issue Jan 13, 2021 · 2 comments · Fixed by #1334

Comments

@christopherhein
Copy link
Member

I would like to be able to get access to all or some of the http.Request.Context from within an Admission webhook handler. This could be for getting the path, parameters, adding tracing information, etc. This should be something you can opt into since it could bloat the context if we just added the entire request object.

Proposal

Modify admission.Webhook struct to include optional WithContextFunc which allows the controller builder to implement their own context mutation:

type Webhook struct {
	// Handler actually processes an admission request returning whether it was allowed or denied,
	// and potentially patches to apply to the handler.
	Handler Handler

	// WithContextFunc will allow you to take the http.Request.Context() and
	// add any additional information such as passing the request path or
	// headers thus allowing you to read them from within the handler
	WithContextFunc func(context.Context, *http.Request) context.Context

	// decoder is constructed on receiving a scheme and passed down to then handler
	decoder *Decoder

	log logr.Logger
}

With that defined we can then change the ServeHTTP func to call this func before passing the context to the Handler.

func (wh *Webhook) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	if wh.WithContextFunc != nil {
		ctx = wh.WithContextFunc(ctx, r)
	}
        // ...
        reviewResponse = wh.Handle(ctx, req)
}

Usage

Typically in main.go this would setup the webhook server and add the path to the context, we can then return this value and take any action we need.

	mgr.GetWebhookServer().Register("/webhook", &webhook.Admission{
		Handler: &wh.Service{
			Client: mgr.GetClient(),
		},
		WithContextFunc: ctx context.Context, r *http.Request) context.Context {
	        	return context.WithValue(ctx,"path", r.URL.Path)
		},
	})

Webhook implementation:

func (a *Service) Handle(ctx context.Context, req admission.Request) admission.Response {
	path := ctx.Value("path").(string)
	fmt.Printf("path: %s", path)
	// ...
}
@christopherhein
Copy link
Member Author

This extends the functionality added #549

@vincepri
Copy link
Member

+1 on the proposal from my side, it seems an iterative improvement that can be useful to lots of folks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants