<p>I'm trying to create a validating webhook</p>
<pre class="lang-sh prettyprint-override"><code>kubebuilder create webhook batch \
            --version v1 \
            --kind Webhook \
            --defaulting \
            --programmatic-validation
</code></pre>
<p>but it always gives me an error.</p>
<pre><code>failed to create webhook: unable to inject the resource to. 
&quot;base.go.kubebuilder.io/v3&quot;: kubebuilder create webhook requires. 
a previously created API
</code></pre>
<p>and i'm not sure what to add extra in the kubebuilder command.
Any help is appreciated.</p>

alok verma
<p>I just got the same problem, seems like kubebuilder look at a file called <code>PROJECT</code> 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</p>
<p>so at root of your project, if you run
<code>$ cat PROJECT</code>
it will look someting like this</p>
<pre><code>domain: example.org
layout:
- go.kubebuilder.io/v3
projectName: khello
repo: example.com/khello
version: &quot;3&quot;
</code></pre>
<p>now if we run your command</p>
<pre><code>kubebuilder create webhook batch \
            --version v1 \
            --kind Webhook \
            --defaulting \
            --programmatic-validation
</code></pre>
<p>it will complains and says something like</p>
<pre><code>....
2021/11/17 13:15:03 failed to create webhook: unable to inject the resource to &quot;base.go.kubebuilder.io/v3&quot;: kubebuilder create webhook requires a previously created API
</code></pre>
<p>ok cool, now we're at the same state, now what?</p>
<p>now, if you haven't create the API, do one make one with</p>
<pre><code>kubebuilder create api  --version v1 --kind Webhook
</code></pre>
<p>now if you notice a file with the name <code>PROJECT</code> at the root of your project dir, it will says something like</p>
<pre><code>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: &quot;3&quot;
</code></pre>
<p>now that we have created the api, we can run your command</p>
<pre><code>kubebuilder create webhook batch \
            --version v1 \
            --kind Webhook \
            --defaulting \
            --programmatic-validation
</code></pre>
<p>voila, it works now</p>
<p>and the <code>PROJECT</code> file will become something like</p>
<pre><code>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: &quot;3&quot;
</code></pre>
<p>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 <code>PROJECT</code> whenever the <code>create</code> command happens. So, my suggestion would be check your <code>PROJECT</code> file, make sure the API is created and if it does, make sure you put the right parameter in your <code>kubebuilder create weboook</code> command to match the contents of thta <code>PROJECT</code> file.</p>
