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

🐛 Fail when creating api webhook for a core type #2152

Closed
wants to merge 1 commit into from

Conversation

kopiczko
Copy link

This change makes:

  • create api fail when --resource is set and the API is not already scaffolded for a core type
  • create webhook fail for a core type

Fixes #2141

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Apr 23, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @kopiczko!

It looks like this is your first PR to kubernetes-sigs/kubebuilder 🎉. 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/kubebuilder 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 @kopiczko. 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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 23, 2021
@kopiczko
Copy link
Author

/cc @estroz

@k8s-ci-robot k8s-ci-robot requested a review from estroz April 23, 2021 08:14
pkg/plugins/golang/options.go Outdated Show resolved Hide resolved
@@ -40,6 +40,9 @@ type Resource struct {
// Controller specifies if a controller has been scaffolded.
Controller bool `json:"controller,omitempty"`

// Core indicates if this is a core resource.
Core bool `json:"core,omitempty"`
Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure why there are those json tags. Is it marshalled somewhere?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, in the config. I think we can get the same functionality as setting a bool if we check strings.HasPrefix(Resource.Path, modulePath) where modulePath is from go.mod. This might break for submodules though.

If we do decide to go with an extra field, I vote for

Suggested change
Core bool `json:"core,omitempty"`
External bool `json:"external,omitempty"`

Since it covers both cases. If the path starts with k8s.io/ then we can be pretty sure it is core.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, thinking of that there is no point of introducing .Core because we can basically check strings.HasPrefix(res.Path, "k8s.io").

Checking against module is also not hard. There is golang.findGoModulePath function already defined. It could be exported.

Copy link
Author

Choose a reason for hiding this comment

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

@estroz I went with simple prefix check. That can be changed if for any reason .External has to be introduced, but I'd prefer to avoid changing PROJECT because something may be needed.

Copy link
Member

Choose a reason for hiding this comment

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

We cannot change core to external.

  1. it is a breaking change
  2. Core type means types that are defined in the K8s API and External means we scaffold an API that is defined in another project. For example, I have an Operator A which defines APIs that will also be used by my Operator B.

@kopiczko kopiczko force-pushed the error-core-webhook branch from 9eefee0 to 121cc3b Compare April 25, 2021 11:07
pkg/plugins/golang/v2/webhook.go Outdated Show resolved Hide resolved
pkg/plugins/golang/v3/webhook.go Outdated Show resolved Hide resolved
@kopiczko kopiczko force-pushed the error-core-webhook branch 3 times, most recently from 068ba61 to a38e802 Compare April 28, 2021 06:39
Comment on lines 105 to 110
// check if resource exist to create webhook
if r, err := p.config.GetResource(p.resource.GVK); err != nil {
loadedResource, err := p.config.GetResource(p.resource.GVK)
if err != nil || !loadedResource.HasAPI() {
return fmt.Errorf("%s create webhook requires a previously created API ", p.commandName)
} else if r.Webhooks != nil && !r.Webhooks.IsEmpty() && !p.force {
}
if loadedResource.Webhooks != nil && !loadedResource.Webhooks.IsEmpty() && !p.force {
return fmt.Errorf("webhook resource already exists")
}
Copy link
Author

Choose a reason for hiding this comment

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

After rereading it again I found that the check was already there but it had a bug: if the err != nil that doesn't guarantee the API is created. We need to check if the loaded resource HasAPI() as well.

So it now all works like this:

  • Path is always set for core resources. create api fails if the Path starts with k8s.io
  • create webhook fails if there is no scaffolded API

@Adirio @estroz PTALA

@kopiczko kopiczko force-pushed the error-core-webhook branch from a38e802 to 9252e90 Compare April 28, 2021 10:34
// Generating the API for a core resource is forbidden.
if strings.HasPrefix(p.resource.Path, "k8s.io") {
return errors.New("scaffolding API for core resources is not supported")
}
Copy link
Member

@camilamacedo86 camilamacedo86 Apr 28, 2021

Choose a reason for hiding this comment

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

WDYT about if is core-type then we allow to do the default scaffold and just update the PROJECT file?
What would be the bad side impacts one that?

c/c @estroz @Adirio

Copy link
Author

Choose a reason for hiding this comment

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

Do I understand correctly you'd like to ignore the --resource flag completely for the core types and silently set it to false?

Copy link
Author

Choose a reason for hiding this comment

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

@camilamacedo86 I believe as a first iteration create api may fail when for a core resource. If the handling of the --resource flag changes in the future it will be a non-breaking change anyway if it fails now.

Copy link
Member

Choose a reason for hiding this comment

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

We can create api for a core type however that will be added to the project domain and is required to change manually. So, the correct approach IMO is:

  • create API: check if is a core-type and do not scaffold the resource and populate accordingly the PROJECT file
  • webhook API: check if we are trying to create a webhook for a core type, if yes then call create API for that to do the same above and allow the default scaffold occurs.

Copy link
Author

Choose a reason for hiding this comment

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

I think the PROJECT file is updated correctly for the core resource. See below:

$ kubebuilder create api --group "core" --version "v1" --kind "Pod" --resource=false --controller=true

$ cat PROJECT
domain: my-domain.com
layout:
- go.kubebuilder.io/v3
multigroup: true
projectName: kubebuilder-test
repo: github.com/kopiczko/kubebuilder-test
resources:
- controller: true
  group: core
  kind: Pod
  path: k8s.io/api/core/v1
  version: v1
version: "3"

The problem you mention (if I understand it correctly) is present when you set --group "" instead of --group "core". This is kind of confusing. I opened a separate issue for this #2162.

Copy link
Member

@camilamacedo86 camilamacedo86 Oct 2, 2021

Choose a reason for hiding this comment

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

@k8s-triage-robot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 29, 2021
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Aug 28, 2021
@estroz
Copy link
Contributor

estroz commented Aug 30, 2021

/remove-lifecycle rotten
/ok-to-test

@camilamacedo86 follow-up thoughts on this?

@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 lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. labels Aug 30, 2021
@k8s-ci-robot k8s-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 30, 2021
@@ -133,6 +134,11 @@ func (p *createAPISubcommand) InjectResource(res *resource.Resource) error {

// In case we want to scaffold a resource API we need to do some checks
if p.options.DoAPI {
// Generating the API for a core resource is forbidden.
if strings.HasPrefix(p.resource.Path, "k8s.io") {
return errors.New("scaffolding API for core resources is not supported")
Copy link
Member

@camilamacedo86 camilamacedo86 Oct 2, 2021

Choose a reason for hiding this comment

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

Why that is forbidden? If it is a core type we just need to ignore the creation of the resources into the api/ dir and ensure that we will create the imports accordingly. This part also seems missing here. We would need to change the code for the fragments. Is that working now? Could we ensure that we have this scenario scaffold in the test data samples to check it?

@@ -101,7 +101,7 @@ func (p *createWebhookSubcommand) InjectResource(res *resource.Resource) error {
return fmt.Errorf("%s create webhook requires a previously created API ", p.commandName)
}
} else {
if r, err := p.config.GetResource(p.resource.GVK); err != nil {
if r, err := p.config.GetResource(p.resource.GVK); err != nil || !r.HasAPI() {
Copy link
Member

Choose a reason for hiding this comment

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

IHMO we need here to improve this check.

If the error be faced here because the resource was not found we need to check if the resource is a core type and it is scaffold in the project file. If yes, then fine we should not return an error and allow scaffold the webhooks.

Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution 🥇
And sorry for the delay in the response.

This PR requires some small changes. Could you please check my comments?

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: kopiczko
To complete the pull request process, please assign estroz after the PR has been reviewed.
You can assign the PR to them by writing /assign @estroz in a comment when ready.

The full list of commands accepted by this bot can be found 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-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Dec 31, 2021
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Jan 30, 2022
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

@k8s-ci-robot
Copy link
Contributor

@k8s-triage-robot: Closed this PR.

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue or PR with /reopen
  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close

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.

@kopiczko kopiczko deleted the error-core-webhook branch April 29, 2022 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. 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.

create webhook: return error when used with a core type
6 participants