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

✨ Allow customizing generated webhook configuration's name #1002

Merged
merged 1 commit into from
Sep 12, 2024

Conversation

davidxia
Copy link
Contributor

@davidxia davidxia commented Jul 10, 2024

example usage

❯ GOBIN=(pwd)/bin go install ./cmd/*

❯ ./bin/controller-gen webhook -w

Webhook

+kubebuilder:webhook:admissionReviewVersions=<[]string>,failurePolicy=<string>,groups=<[]string>[,matchPolicy=<string>],mutating=<bool>,name=<string>[,path=<string>][,reinvocationPolicy=<string>],resources=<[]string>[,sideEffects=<string>][,timeoutSeconds=<int>][,url=<string>],verbs=<[]string>,versions=<[]string>[,webhookVersions=<[]string>]  package  specifies how a webhook should be served.  
+kubebuilder:webhookconfiguration:mutating=<bool>[,name=<string>]                                                                                                                                                                                                                                                                                        package  specifies how a webhook should be served.  

contributes to #865

@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. do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jul 10, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @davidxia!

It looks like this is your first PR to kubernetes-sigs/controller-tools 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-tools has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot
Copy link
Contributor

Hi @davidxia. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 10, 2024
@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Jul 10, 2024
@davidxia
Copy link
Contributor Author

@sbueringer thanks for your tip. How does this look? If this looks good overall, I can write a test for it. Should the test be similar to this?

Copy link
Contributor Author

@davidxia davidxia left a comment

Choose a reason for hiding this comment

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

tested manually by

  1. GOBIN=(pwd)/bin go install ./cmd/*
  2. copy the ./bin/controller-gen executable into a kubebuilder-scaffolded project's bin/controller-gen-vX.Y.Z path
  3. remove the kubectl apply from the kubebuilder project's Makefile's deploy target

case 1: backwards compatibility

  1. run make deploy and check that kind: MutatingWebhookConfiguration and kind: ValidatingWebhookConfiguration have the same names as before.

    apiVersion: admissionregistration.k8s.io/v1
    kind: MutatingWebhookConfiguration
    metadata:
      name: project-mutating-webhook-configuration
    
    apiVersion: admissionregistration.k8s.io/v1
    kind: ValidatingWebhookConfiguration
    metadata:
      name: project-validating-webhook-configuration
    

case 2: specify marker only for MutatingWebhookConfiguration

  1. add // +kubebuilder:webhookconfiguration:name=DEADBEEF,mutating=true to api/v1/cronjob_webhook.go

  2. run make deploy and check that kind: MutatingWebhookConfiguration has the following.

    apiVersion: admissionregistration.k8s.io/v1
    kind: MutatingWebhookConfiguration
    metadata:
      name: project-DEADBEEF
    

    instead of

    apiVersion: admissionregistration.k8s.io/v1
    kind: MutatingWebhookConfiguration
    metadata:
      name: project-mutating-webhook-configuration
    
  3. check that kind: ValidatingWebhookConfiguration has the same name as before: project-validating-webhook-configuration

case 2: specify markers for both MutatingWebhookConfiguration and ValidatingWebhookConfiguration

  1. add // +kubebuilder:webhookconfiguration:name=DEADBEEF,mutating=true and // +kubebuilder:webhookconfiguration:name=FOOBAR,mutating=false to api/v1/cronjob_webhook.go
  2. run make deploy and check for the following.
    apiVersion: admissionregistration.k8s.io/v1
    kind: MutatingWebhookConfiguration
    metadata:
      name: project-DEADBEEF
    
    ...
    
    apiVersion: admissionregistration.k8s.io/v1
    kind: ValidatingWebhookConfiguration
    metadata:
      name: project-FOOBAR
    

case 3: add multiple kubebuilder:webhookconfiguration:mutating=true markers

  1. add both // +kubebuilder:webhookconfiguration:name=A,mutating=true and // +kubebuilder:webhookconfiguration:name=a,mutating=true (in that order) to api/v1/cronjob_webhook.go
  2. run make deploy and check for the following. controller-gen sorts and uses the last one. a sorts to a later position than A.
    apiVersion: admissionregistration.k8s.io/v1
    kind: MutatingWebhookConfiguration
    metadata:
      name: project-a
    

pkg/webhook/parser.go Outdated Show resolved Hide resolved
@sbueringer
Copy link
Member

@sbueringer thanks for your tip. How does this look? If this looks good overall, I can write a test for it. Should the test be similar to this?

Yup. I think something similar to the test coverage in this PR would be good

Oh I think I missed something before. The existing kubebuilder:webhook marker is used to define individual webhooks in the object. While the name is global for the entire MutatingWebhookConfiguration / ValidatingWebhookConfiguration configuration. Might be a bit awkward to define the name of the overall obect on the marker that defines a single element in the webhook array.

WDYT about introducing a separate marker called kubebuilder:webhookconfiguration with a name field instead?

Looking at the issue, the service namespace (& maybe also name?) can be added to the Config struct though (just like Path)

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jul 11, 2024
pkg/webhook/parser.go Outdated Show resolved Hide resolved
pkg/webhook/parser.go Outdated Show resolved Hide resolved
pkg/webhook/parser.go Outdated Show resolved Hide resolved
Comment on lines +492 to +498
} else {
// The only possible version in supportedWebhookVersions is v1,
// so use it for all versioned types in this context.
objRaw = &admissionregv1.MutatingWebhookConfiguration{}
objRaw.SetName("mutating-webhook-configuration")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

for backwards-compat

Comment on lines +529 to +535
} else {
// The only possible version in supportedWebhookVersions is v1,
// so use it for all versioned types in this context.
objRaw = &admissionregv1.ValidatingWebhookConfiguration{}
objRaw.SetName("validating-webhook-configuration")
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

for backwards-compat

@davidxia
Copy link
Contributor Author

WDYT about introducing a separate marker called kubebuilder:webhookconfiguration with a name field instead?

done, PTAL and let me know if that looks good

Looking at the issue, the service namespace (& maybe also name?) can be added to the Config struct though (just like Path)

Good idea. I can make another PR for that once we get something merged for this.

@davidxia
Copy link
Contributor Author

@joelanford @vincepri do you have time to give this a quick initial review?

@sbueringer
Copy link
Member

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 9, 2024
Copy link
Member

@sbueringer sbueringer left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, looks good so far!

pkg/webhook/parser.go Show resolved Hide resolved
pkg/webhook/parser.go Outdated Show resolved Hide resolved
pkg/webhook/parser.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 2, 2024
@davidxia
Copy link
Contributor Author

davidxia commented Sep 2, 2024

@sbueringer updated. Lmk if I'm missing anything!

@davidxia davidxia marked this pull request as ready for review September 2, 2024 22:57
@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 Sep 2, 2024
@sbueringer sbueringer changed the title ✨ feat: allow customizing generated webhook's name ✨ Allow customizing generated webhook configuration's name Sep 10, 2024
@sbueringer
Copy link
Member

@davidxia Looks good. PTAL at #1002 (comment)

Otherwise happy to merge

@davidxia davidxia force-pushed the webhook-name branch 2 times, most recently from 521a807 to 8b7192e Compare September 10, 2024 20:42
@davidxia
Copy link
Contributor Author

@sbueringer thanks, but the bot message doesn't say why the commit message is invalid or how to fix. Are there instructions?

@sbueringer
Copy link
Member

sbueringer commented Sep 11, 2024

Keywords which can automatically close issues and at(@) or hashtag(#) mentions are not allowed in commit messages.

I would just drop everything that contains "closes" or an issue number in the entire commit message

## example usage

```
❯ GOBIN=(pwd)/bin go install ./cmd/*

❯ ./bin/controller-gen webhook -w

Webhook

+kubebuilder:webhook:admissionReviewVersions=<[]string>,failurePolicy=<string>,groups=<[]string>[,matchPolicy=<string>],mutating=<bool>,name=<string>[,path=<string>][,reinvocationPolicy=<string>],resources=<[]string>[,sideEffects=<string>][,timeoutSeconds=<int>][,url=<string>],verbs=<[]string>,versions=<[]string>[,webhookVersions=<[]string>]  package  specifies how a webhook should be served.
+kubebuilder:webhookconfiguration:mutating=<bool>[,name=<string>]                                                                                                                                                                                                                                                                                        package  specifies how a webhook should be served.
```

Signed-off-by: David Xia <[email protected]>
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Sep 11, 2024
@davidxia
Copy link
Contributor Author

@sbueringer that did the trick. Ready for review and/or merge now.

@sbueringer
Copy link
Member

Thank you!

/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 Sep 12, 2024
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: 44d35f3ef0371f7cee392d80cd6259f75063bf7f

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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 Sep 12, 2024
@k8s-ci-robot k8s-ci-robot merged commit c69110a into kubernetes-sigs:main Sep 12, 2024
13 checks passed
@davidxia davidxia deleted the webhook-name branch September 12, 2024 16:42
shanduur pushed a commit to registry-operator/registry-operator that referenced this pull request Sep 21, 2024
This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
|
[sigs.k8s.io/controller-tools](https://redirect.github.com/kubernetes-sigs/controller-tools)
| `v0.16.2` -> `v0.16.3` |
[![age](https://developer.mend.io/api/mc/badges/age/go/sigs.k8s.io%2fcontroller-tools/v0.16.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/sigs.k8s.io%2fcontroller-tools/v0.16.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/sigs.k8s.io%2fcontroller-tools/v0.16.2/v0.16.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/sigs.k8s.io%2fcontroller-tools/v0.16.2/v0.16.3?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>kubernetes-sigs/controller-tools
(sigs.k8s.io/controller-tools)</summary>

###
[`v0.16.3`](https://redirect.github.com/kubernetes-sigs/controller-tools/releases/tag/v0.16.3)

[Compare
Source](https://redirect.github.com/kubernetes-sigs/controller-tools/compare/v0.16.2...v0.16.3)

Published binaries on previous v0.16.x releases were reporting an
incorrect version.

#### What's Changed

- 🐛 Allow CLI binaries to set a version by
[@&#8203;josvazg](https://redirect.github.com/josvazg) in
[kubernetes-sigs/controller-tools#1049
- ✨ Allow customizing generated webhook configuration's name by
[@&#8203;davidxia](https://redirect.github.com/davidxia) in
[kubernetes-sigs/controller-tools#1002

#### Dependencies

- 🌱 Bump github.com/onsi/gomega from 1.34.1 to 1.34.2 in the
all-go-mod-patch-and-minor group by
[@&#8203;dependabot](https://redirect.github.com/dependabot) in
[kubernetes-sigs/controller-tools#1047
- 🌱 Bump tj-actions/changed-files from 45.0.0 to 45.0.1 in the
all-github-actions group by
[@&#8203;dependabot](https://redirect.github.com/dependabot) in
[kubernetes-sigs/controller-tools#1048
- 🌱 Bump peter-evans/create-pull-request from 6.1.0 to 7.0.1 in
the all-github-actions group by
[@&#8203;dependabot](https://redirect.github.com/dependabot) in
[kubernetes-sigs/controller-tools#1052

#### New Contributors

- [@&#8203;josvazg](https://redirect.github.com/josvazg) made their
first contribution in
[kubernetes-sigs/controller-tools#1049
- [@&#8203;davidxia](https://redirect.github.com/davidxia) made their
first contribution in
[kubernetes-sigs/controller-tools#1002

**Full Changelog**:
kubernetes-sigs/controller-tools@v0.16.2...v0.16.3

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend Renovate](https://mend.io/renovate/).
View the [repository job
log](https://developer.mend.io/github/registry-operator/registry-operator).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC44MC4wIiwidXBkYXRlZEluVmVyIjoiMzguODAuMCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiYXJlYS9kZXBlbmRlbmN5Il19-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
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. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants