6

I'm trying to create a validating webhook

kubebuilder create webhook batch \
            --version v1 \
            --kind Webhook \
            --defaulting \
            --programmatic-validation

but it always gives me an error.

failed to create webhook: unable to inject the resource to. 
"base.go.kubebuilder.io/v3": kubebuilder create webhook requires. 
a previously created API

and i'm not sure what to add extra in the kubebuilder command. Any help is appreciated.

CodeWizard
  • 128,036
  • 21
  • 144
  • 167
alok verma
  • 97
  • 7

2 Answers2

5

I just got the same problem, seems like kubebuilder look at a file called PROJECT at the root of your project to validate whether the API has been created or not. So before you create the defaulting webhook, make sure you have created the API before you create the webhook, I'm having a hard time explaining this but I think some example will make it clear

so at root of your project, if you run $ cat PROJECT it will look someting like this

domain: example.org
layout:
- go.kubebuilder.io/v3
projectName: khello
repo: example.com/khello
version: "3"

now if we run your command

kubebuilder create webhook batch \
            --version v1 \
            --kind Webhook \
            --defaulting \
            --programmatic-validation

it will complains and says something like

....
2021/11/17 13:15:03 failed to create webhook: unable to inject the resource to "base.go.kubebuilder.io/v3": kubebuilder create webhook requires a previously created API

ok cool, now we're at the same state, now what?

now, if you haven't create the API, do one make one with

kubebuilder create api  --version v1 --kind Webhook

now if you notice a file with the name PROJECT at the root of your project dir, it will says something like

domain: example.org
layout:
- go.kubebuilder.io/v3
projectName: khello
repo: example.com/khello
resources:
- api:
    crdVersion: v1
    namespaced: true
  controller: true
  domain: example.org
  kind: Webhook
  path: example.com/khello/api/v1
  version: v1
version: "3"

now that we have created the api, we can run your command

kubebuilder create webhook batch \
            --version v1 \
            --kind Webhook \
            --defaulting \
            --programmatic-validation

voila, it works now

and the PROJECT file will become something like

domain: example.org
layout:
- go.kubebuilder.io/v3
projectName: khello
repo: example.com/khello
resources:
- api:
    crdVersion: v1
    namespaced: true
  controller: true
  domain: example.org
  kind: Webhook
  path: example.com/khello/api/v1
  version: v1
  webhooks:
    defaulting: true
    validation: true
    webhookVersion: v1
version: "3"

with that being said, I'm not sure how kubebuilder works under the hood, but from what I understand it check whether something is created from that PROJECT whenever the create command happens. So, my suggestion would be check your PROJECT file, make sure the API is created and if it does, make sure you put the right parameter in your kubebuilder create weboook command to match the contents of thta PROJECT file.

ThatBuffDude
  • 451
  • 4
  • 11
  • 1
    Thanks, It worked little further I have one more issue when i try to make deploy it and got `Error: no matches for OriginalId admissionregistration.k8s.io_v1_MutatingWebhookConfiguration|system|mutating-webhook-configuration; no matches for CurrentId admissionregistration.k8s.io_v1_MutatingWebhookConfiguration|system|mutating-webhook-configuration; failed to find unique target for patch admissionregistration.k8s.io_v1_MutatingWebhookConfiguration|mutating-webhook-configuration error: no objects passed to apply make: *** [deploy] Error 1` – alok verma Nov 18 '21 at 10:50
  • 1
    Hmm I'm not sure how to solve that particular issue but that's kustomize error message for sure. And I don't er have much experience with kustomize, but I would suggest you to double check the docs, https://book.kubebuilder.io/cronjob-tutorial/running.html , is section 1.9.2 you have to uncomment certain parts of the kustomize file in order for the deploy with webhook to work. Oh and I just followed that docs yesterday and it works. – ThatBuffDude Nov 18 '21 at 14:23
0

Tips for rookies: The --group and --version values must be exactly the same as "the --group and --version values when you used to create your api".

levi
  • 39
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34253640) – AthulMuralidhar Apr 25 '23 at 14:46