-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Conversation
Welcome @kopiczko! |
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 Once the patch is verified, the new status will be reflected by the 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. |
/cc @estroz |
pkg/model/resource/resource.go
Outdated
@@ -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"` |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
- it is a breaking change
- 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.
9eefee0
to
121cc3b
Compare
068ba61
to
a38e802
Compare
pkg/plugins/golang/v3/webhook.go
Outdated
// 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") | ||
} |
There was a problem hiding this comment.
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 thePath
starts withk8s.io
create webhook
fails if there is no scaffolded API
a38e802
to
9252e90
Compare
// 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") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Issues go stale after 90d of inactivity. If this issue is safe to close now please do so with Send feedback to sig-contributor-experience at kubernetes/community. |
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:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
/remove-lifecycle rotten @camilamacedo86 follow-up thoughts on this? |
@@ -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") |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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.
There was a problem hiding this 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?
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: kopiczko 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 |
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:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle stale |
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:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /lifecycle rotten |
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:
You can:
Please send feedback to sig-contributor-experience at kubernetes/community. /close |
@k8s-triage-robot: Closed this PR. In response to this:
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. |
This change makes:
create api
fail when--resource
is set and the API is not already scaffolded for a core typecreate webhook
fail for a core typeFixes #2141