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

✨ Adding WithContextFunc for webhook server to pass info to the handler #1334

Merged

Conversation

christopherhein
Copy link
Member

This closes #1333 and extends #549.

This non-breaking change introduces a new field on the webhook server registration, this allows you to add additional information to the request context from the ServeHTTP func. This is useful if you need to get information from the http.Request like parsing the request path for variables.

Controller Builder Implementation:

This example will write the request path back to

type pathKeyType int
const pathKey pathKeyType = iota
func setPath(ctx context.Context, r *http.Request) context.Context {
	return context.WithValue(ctx, pathKey, r.URL.Path)
}

// Get will return the new request details from the context
func getPath(ctx context.Context) string {
	return ctx.Value(pathKey).(string)
}

mgr.GetWebhookServer().Register("/webhook", &webhook.Admission{
	Handler: &fakeHandler{
		fn: func(ctx context.Context, req Request) Response {
			return Allowed(getPath(ctx))
		},
	},
	WithContextFunc: setPath,
})

Signed-off-by: Chris Hein me@chrishein.com

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jan 14, 2021
@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jan 14, 2021
@k8s-ci-robot
Copy link
Contributor

@christopherhein: The following test failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
pull-controller-runtime-apidiff-master 925c552 link /test pull-controller-runtime-apidiff-master

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@christopherhein
Copy link
Member Author

@vincepri re:apidiff can we ignore this? Seems like it's expected:

�[1;33minvoking: 'hack/tools/bin/go-apidiff 17b28b4c90d8736fa8ada8942235635191f85021 --print-compatible'�[0m

sigs.k8s.io/controller-runtime/pkg/webhook/admission
  Incompatible changes:
  - Webhook: old is comparable, new is not
  Compatible changes:
  - Webhook.WithContextFunc: added

@pwittrock
Copy link
Contributor

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: christopherhein, pwittrock

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 14, 2021
@pwittrock
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jan 14, 2021
@k8s-ci-robot k8s-ci-robot merged commit d1a29ee into kubernetes-sigs:master Jan 14, 2021
@christopherhein christopherhein deleted the add-request-context-func branch January 14, 2021 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

✨ Ability to modify Admissions Request context
3 participants