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

✨ Add validating webhook for AWSMachineTemplate #1116

Merged
merged 2 commits into from
Oct 15, 2019

Conversation

tahsinrahman
Copy link
Contributor

What this PR does / why we need it:

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #886

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Sep 10, 2019
Dockerfile Outdated Show resolved Hide resolved
api/v1alpha2/awsmachinetemplate_webhook.go Outdated Show resolved Hide resolved
api/v1alpha2/awsmachinetemplate_webhook.go Outdated Show resolved Hide resolved
api/v1alpha2/awsmachinetemplate_webhook.go Outdated Show resolved Hide resolved
api/v1alpha2/awsmachinetemplate_webhook.go Outdated Show resolved Hide resolved
api/v1alpha2/awsmachinetemplate_webhook.go Outdated Show resolved Hide resolved
api/v1alpha2/zz_generated.deepcopy.go Outdated Show resolved Hide resolved
config/certmanager/certificate.yaml Show resolved Hide resolved
config/default/kustomization.yaml Show resolved Hide resolved
config/webhook/kustomizeconfig.yaml Show resolved Hide resolved
@tahsinrahman tahsinrahman force-pushed the webhook branch 3 times, most recently from 7461e79 to 690d2ec Compare September 11, 2019 12:07
}

// validateUpdate checks that no immutable fields have been updated
func validateUpdate(spec *AWSMachineSpec, oldSpec *AWSMachineSpec) error {
Copy link
Member

Choose a reason for hiding this comment

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

Are there any fields that we want to be mutable in the template types? @ncdc @vincepri I thought we had wanted to avoid allowing mutability of the template types and instead wanted to require users to update the reference in the MachineDeployment/MachineSet?

If we do want to force full immutability, then we can likely create a generic webhook in CAPI that blocks any update, and just register the webhook for the AWSMachineTemplate type here in CAPA.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can allow updates to metadata, but probably all other updates should be rejected. I think we could probably write some generic validation helpers in CAPI, but afaik we still need to do the full webhook code generation & registration for each type.

Copy link
Member

Choose a reason for hiding this comment

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

According to the docs, we can configure which resources are subject to which admission webhooks at runtime: https://kubernetes.io/docs/reference/access-authn-authz/extensible-admission-controllers/#configure-admission-webhooks-on-the-fly

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I didn't realize you were thinking about doing it that way. I was thinking of a kubebuilder-ism with its way of registering webhooks.

Copy link
Member

Choose a reason for hiding this comment

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

We should avoid naming changes to resources given that they're references elsewhere, unless the OwnerReferences get updated automagically?

Copy link
Member

Choose a reason for hiding this comment

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

As long as it's documented, I think that should be fine. Technically, the same applies for today's default RBAC permissions in CAPI, where we assume bootstrap.<> and infrastructure.<> as default

Copy link
Member

Choose a reason for hiding this comment

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

@ncdc agreed that clusteradm would be the best place long term to handle this

Copy link
Member

Choose a reason for hiding this comment

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

@vincepri @ncdc the validating webhook requests are initiated by the API Server, I'm not sure there is a way to use API discovery to register types to watch at runtime as we do with the controllers.

Copy link
Contributor

Choose a reason for hiding this comment

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

@detiber that's right. So the only way to handle this is with the details in the ValidatingWebhookConfiguration.

What do you all think about putting the generic *Template validation as a helper function in cluster-api, but at least for now, having each provider implement their own webhooks for their own templates (using the generic function from cluster-api)?

Copy link
Member

Choose a reason for hiding this comment

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

@ncdc I think that's fine as long as we track improving that experience later when clusteradm is available.

@ncdc ncdc added this to the v0.4.x milestone Sep 23, 2019
@vincepri vincepri self-assigned this Sep 23, 2019
// spec.Subnet is a *AWSResourceReference and could technically be
// a *string, ARN or Filter. However, elsewhere in the code it is only used
// as a *string, so do the same here.
if aws.StringValue(spec.Subnet.ID) != aws.StringValue(oldSpec.Subnet.ID) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably check that spec.Subnet isn't nil

name: validating-webhook-configuration
webhooks:
- clientConfig:
caBundle: Cg==
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this do?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is generated by kubebuilder. I guess it's a required field. Maybe cert-manager or something else updates it? It decodes to \n.

Copy link
Member

@detiber detiber left a comment

Choose a reason for hiding this comment

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

A few nits, but otherwise lgtm

main.go Outdated Show resolved Hide resolved
config/webhook/service.yaml Outdated Show resolved Hide resolved
config/webhook/service.yaml Show resolved Hide resolved
Copy link
Member

@vincepri vincepri left a comment

Choose a reason for hiding this comment

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

/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: tahsinrahman, 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 Oct 8, 2019
@tahsinrahman
Copy link
Contributor Author

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 8, 2019
@@ -282,13 +280,6 @@ func (r *AWSMachineReconciler) reconcileNormal(ctx context.Context, machineScope
return reconcile.Result{}, nil
}

// TODO(ncdc): move this validation logic into a validating webhook
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see this logic moved anywhere, am i misunderstanding this Webhook?

Copy link
Contributor

Choose a reason for hiding this comment

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

It was in an earlier version of this PR. Not sure why it got deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as entire spec is immutable, instead of checking every field of spec, i did this

if !reflect.DeepEqual(newAWSMachineTemplate.Spec, oldAWSMachineTemplate.Spec) {
	return errors.New("awsMachineTemplateSpec is immutable")
}

Copy link
Member

Choose a reason for hiding this comment

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

This validation was being used by the AWSMachine controller, where the current webhook is targeting AWSMachineTemplate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it!
that means, we need to deploy another validating webhook for AWSMachine types, to move these logic, right?

Copy link
Member

Choose a reason for hiding this comment

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

correct, thanks :)

Copy link
Contributor

Choose a reason for hiding this comment

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

@tahsinrahman are you going to add the AWSMachine validation webhook in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I can add it here, or if it becomes too big for review, i can do it in separate pr, which one works best for you? 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Either way is fine. If you keep them separate, you can retitle this PR to "Add validating webhook for AWSMachineTemplate"?

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Oct 9, 2019
@tahsinrahman tahsinrahman force-pushed the webhook branch 2 times, most recently from f3bb900 to 35a1361 Compare October 9, 2019 12:32
@tahsinrahman tahsinrahman changed the title ✨ Add validating webhooks ✨ Add validating webhook for AWSMachineTemplate Oct 10, 2019
namespace: system
path: /validate-infrastructure-cluster-x-k8s-io-v1alpha2-awsmachinetemplate
failurePolicy: Fail
name: vawsmachinetemplate.kb.io
Copy link
Member

Choose a reason for hiding this comment

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

How was this name generated?

Copy link
Contributor

Choose a reason for hiding this comment

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

kubebuilder does it this way

Copy link
Member

Choose a reason for hiding this comment

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

Got it, was mostly curious as it reads funny

@detiber
Copy link
Member

detiber commented Oct 10, 2019

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 10, 2019
@vincepri
Copy link
Member

/hold

@vincepri
Copy link
Member

Do we need to cut release-0.4 first?

@ncdc
Copy link
Contributor

ncdc commented Oct 10, 2019

Do we need to cut release-0.4 first?

No, we can branch from any commit at any time.

@tahsinrahman
Copy link
Contributor Author

Do we want to merge this now? It'll break clusterctl

@vincepri
Copy link
Member

Let's hold on to this until we have all the others open PR merged in 0.4 and we cut the release branch

@ncdc
Copy link
Contributor

ncdc commented Oct 10, 2019

We also don't have to rush this in. Let's think more about the "clusterctl on master" impact.

@detiber
Copy link
Member

detiber commented Oct 10, 2019

/lgtm cancel

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 10, 2019
@ncdc ncdc modified the milestones: v0.4.x, v0.5.0 Oct 10, 2019
@ncdc
Copy link
Contributor

ncdc commented Oct 11, 2019

I'm thinking this will need to move into the v1alpha3 api package (#1202), right?

@tahsinrahman
Copy link
Contributor Author

I updated the pr with v1alpha3 package!

@detiber
Copy link
Member

detiber commented Oct 15, 2019

/lgtm
I believe the hold can be lifted now that we have the release-0.4 branch as well. @ncdc?

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 15, 2019
@ncdc
Copy link
Contributor

ncdc commented Oct 15, 2019

Yes, master is open for v1alpha3.
/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 15, 2019
@k8s-ci-robot k8s-ci-robot merged commit ccf11ac into kubernetes-sigs:master Oct 15, 2019
@tahsinrahman tahsinrahman deleted the webhook branch October 15, 2019 13:30
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.

Add validating webhooks
8 participants