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

⚠️ support delete validation in validator interface #525

Merged
merged 1 commit into from
Jul 23, 2019
Merged

⚠️ support delete validation in validator interface #525

merged 1 commit into from
Jul 23, 2019

Conversation

awesomenix
Copy link
Contributor

  • Currently validator interface supports create and update
  • Add support for delete as well, which could be a breaking change
  • Users need to implement ValidateDelete since interface is updated

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 18, 2019
@k8s-ci-robot k8s-ci-robot requested review from droot and pwittrock July 18, 2019 01:44
@awesomenix
Copy link
Contributor Author

/assign @mengqiy

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jul 18, 2019
@mengqiy
Copy link
Member

mengqiy commented Jul 18, 2019

@awesomenix I'd like to hear your use case more before we decide to make change to the interface.

@mengqiy mengqiy changed the title ✨ ⚠️ support delete validation in validator interface ⚠️ support delete validation in validator interface Jul 18, 2019
@mengqiy
Copy link
Member

mengqiy commented Jul 18, 2019

For breaking change, we should use :warning: I have updated the title.

@awesomenix
Copy link
Contributor Author

awesomenix commented Jul 18, 2019

@mengqiy In our case we use annotations to validate deletes on certain resource so that none of the deletes are accidental. We used Validating Webhooks which support validating DELETE operations as well (this was easy to override in kubebuilder v1), hence requesting the same in v2 as well.

In short making deletes of resources explicit rather than implicit, since we cant protect deletes with finalizers (once the resource is marked for deletion, it can never be recovered)

BTW love the easy scaffolding of webhooks v2, way less code than the original

@mengqiy
Copy link
Member

mengqiy commented Jul 18, 2019

I'm thinking if we should support CONNECT operation. Trying to find if there is a common use case for it.

@awesomenix
Copy link
Contributor Author

was thinking if we should add support for CONNECT as well, but wasnt sure of any use case. Honestly i do not know when it will be used, during resource get?

@mengqiy
Copy link
Member

mengqiy commented Jul 18, 2019

@awesomenix
Re. DELETE operation, I saw this KEP.

It seems upstream doesn't support it yet. No objects will be in the AdmissionReview.

@awesomenix
Copy link
Contributor Author

KEP

Thanks for the reference. I see that 1.15 has the fix kubernetes/kubernetes#76346.

Updating PR to reflect the same.

@awesomenix awesomenix closed this Jul 19, 2019
@awesomenix awesomenix reopened this Jul 19, 2019
Copy link
Member

@mengqiy mengqiy left a comment

Choose a reason for hiding this comment

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

Mostly LG.
IMO we should add some logging showing if a type has implemented the Defaulter and Validator interface in builder package

// registerDefaultingWebhook registers a defaulting webhook if th
func (blder *WebhookBuilder) registerDefaultingWebhook() {
if defaulter, isDefaulter := blder.apiType.(admission.Defaulter); isDefaulter {
mwh := admission.DefaultingWebhookFor(defaulter)
if mwh != nil {
path := generateMutatePath(blder.gvk)
// Checking if the path is already registered.
// If so, just skip it.
if !blder.isAlreadyHandled(path) {
log.Info("Registering a mutating webhook",
"GVK", blder.gvk,
"path", path)
blder.mgr.GetWebhookServer().Register(path, mwh)
}
}
}
}
func (blder *WebhookBuilder) registerValidatingWebhook() {
if validator, isValidator := blder.apiType.(admission.Validator); isValidator {
vwh := admission.ValidatingWebhookFor(validator)
if vwh != nil {
path := generateValidatePath(blder.gvk)
// Checking if the path is already registered.
// If so, just skip it.
if !blder.isAlreadyHandled(path) {
log.Info("Registering a validating webhook",
"GVK", blder.gvk,
"path", path)
blder.mgr.GetWebhookServer().Register(path, vwh)
}
}
}
}
.
This can give people hints in debugging when they are using webhook builder, but the type doesn't implement the interfaces.

It probably looks like:

func (blder *WebhookBuilder) registerValidatingWebhook() {
	if validator, isValidator := blder.apiType.(admission.Validator); isValidator {
		log.Info("registering validating webhook, since Validator interface is implemented", "GVK", gvk)
		...
	} else {
		log.Info("webhook.Validator interface is not implemented", "GVK", gvk)
	}
}

Same for registerMutatingWebhook

pkg/builder/webhook_test.go Outdated Show resolved Hide resolved
pkg/builder/webhook_test.go Outdated Show resolved Hide resolved
- Currently validator interface supports create and update
- Add support for delete as well, which could be a breaking change
- Users need to implement ValidateDelete since interface is updated
@awesomenix
Copy link
Contributor Author

@mengqiy thanks for the feedback. Updated PR.

Copy link
Member

@mengqiy mengqiy left a comment

Choose a reason for hiding this comment

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

There is no action required in this PR.

if req.Operation == v1beta1.Delete {
// In reference to PR: https://github.com/kubernetes/kubernetes/pull/76346
// OldObject contains the object being deleted
err := h.decoder.DecodeRaw(req.OldObject, obj)
Copy link
Member

Choose a reason for hiding this comment

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

req.OldObject is an empty object when kube-apiserver is v1.14 or earlier.
obj will be a object with zero values everywhere.
DecodeRaw really should return an error when there is an empty object.
I created #529. We should merge that first.

Copy link
Member

@mengqiy mengqiy left a comment

Choose a reason for hiding this comment

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

/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 23, 2019
@mengqiy mengqiy added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 23, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by: awesomenix, mengqiy

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 merged commit 72ab3fe into kubernetes-sigs:master Jul 23, 2019
DirectXMan12 pushed a commit that referenced this pull request Jan 31, 2020
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/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants