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

✨ Provide access to admission.Request in custom validator/defaulter #1950

Merged

Conversation

sbueringer
Copy link
Member

@sbueringer sbueringer commented Jul 6, 2022

This PR provides access to the admisison.Request in custom validator and defaulter.

Fixes #1949

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jul 6, 2022
type requestContextKey struct{}

// RequestFromContext returns an admission.Request from ctx.
func RequestFromContext(ctx context.Context) (Request, error) {
Copy link
Member Author

@sbueringer sbueringer Jul 6, 2022

Choose a reason for hiding this comment

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

It would potentially (?) be a nicer UX if we could omit the error return parameter. But returning an empty Request if it is not found feels wrong

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, an error return parameter will be nicer, even if that should not happen for custom validator/defaulter.

Copy link
Member

Choose a reason for hiding this comment

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

The error return message seems ok, better to have the clarity rather than checking nils?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup and a lot better then checking empty fields of an empty struct (as it's not a pointer)

pkg/webhook/admission/webhook.go Outdated Show resolved Hide resolved
@@ -253,3 +253,21 @@ func StandaloneWebhook(hook *Webhook, opts StandaloneOptions) (http.Handler, err
}
return metrics.InstrumentedHook(opts.MetricsPath, hook), nil
}

// requestContextKey is how we find the admission.Request in a context.Context.
type requestContextKey struct{}
Copy link
Contributor

Choose a reason for hiding this comment

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

How about a const string like "webhookRequest"?

Copy link
Member Author

@sbueringer sbueringer Jul 6, 2022

Choose a reason for hiding this comment

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

I think the idea behind using an unexported struct is to only make it possible to retrieve it with the exported RequestFromContext func.

(I took this pattern from how go-logr stores a Logger in a context)

type requestContextKey struct{}

// RequestFromContext returns an admission.Request from ctx.
func RequestFromContext(ctx context.Context) (Request, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1, an error return parameter will be nicer, even if that should not happen for custom validator/defaulter.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 6, 2022
@sbueringer sbueringer changed the title [WIP] Provide access to admission.Request in custom validator/defaulter Provide access to admission.Request in custom validator/defaulter Jul 6, 2022
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 6, 2022
@sbueringer sbueringer changed the title Provide access to admission.Request in custom validator/defaulter ✨ Provide access to admission.Request in custom validator/defaulter Jul 6, 2022
@vincepri
Copy link
Member

vincepri commented Jul 6, 2022

LGTM pending tests

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jul 6, 2022
@sbueringer
Copy link
Member Author

@vincepri PTAL

Added a test for the ctx utils and extended the tests which were added when we introduced the custom validator/defaulter

@sbueringer
Copy link
Member Author

sbueringer commented Jul 6, 2022

@vincepri
Copy link
Member

vincepri commented Jul 6, 2022

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 6, 2022
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: sbueringer, vincepri

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 Jul 6, 2022
@k8s-ci-robot k8s-ci-robot merged commit 15154aa into kubernetes-sigs:master Jul 6, 2022
@k8s-ci-robot k8s-ci-robot added this to the v0.10.x milestone Jul 6, 2022
@sbueringer sbueringer deleted the pr-webhook-adm-request branch July 6, 2022 15:34
@sbueringer
Copy link
Member Author

Would be nice if we can cherry-pick this to v0.12.x

/cherry-pick release-0.12

@k8s-infra-cherrypick-robot

@sbueringer: new pull request created: #1952

In response to this:

Would be nice if we can cherry-pick this to v0.12.x

/cherry-pick release-0.12

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.

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.

Provide access to the admission.Request in validating & defaulting webhooks
6 participants