<p>I am following the CronJob example in KubeBuilder book: <a href="https://book.kubebuilder.io/cronjob-tutorial/cronjob-tutorial.html" rel="noreferrer">https://book.kubebuilder.io/cronjob-tutorial/cronjob-tutorial.html</a></p>

<p>I directly use the code from <a href="https://github.com/kubernetes-sigs/kubebuilder/tree/master/docs/book/src/cronjob-tutorial/testdata/project" rel="noreferrer">https://github.com/kubernetes-sigs/kubebuilder/tree/master/docs/book/src/cronjob-tutorial/testdata/project</a></p>

<p>after run <code>make run</code>, logs like this were shown:</p>

<pre><code>INFO    controller-runtime.metrics  metrics server is starting to listen    {"addr": ":8080"}
INFO    controller-runtime.builder  Registering a mutating webhook  {"GVK": "batch.tutorial.kubebuilder.io/v1, Kind=CronJob", "path": "/ilder-io-v1-cronjob"}
INFO    controller-runtime.webhook  registering webhook {"path": "/mutate-batch-tutorial-kubebuilder-io-v1-cronjob"}
INFO    controller-runtime.builder  Registering a validating webhook    {"GVK": "batch.tutorial.kubebuilder.io/v1, Kind=CronJob", "path": "/builder-io-v1-cronjob"}
INFO    controller-runtime.webhook  registering webhook {"path": "/validate-batch-tutorial-kubebuilder-io-v1-cronjob"}
INFO    setup   starting manager
INFO    controller-runtime.manager  starting metrics server {"path": "/metrics"}
INFO    controller-runtime.webhook.webhooks starting webhook server
INFO    controller-runtime.controller   Starting EventSource    {"controller": "cronjob", "source": "kind source: /, Kind="}
INFO    controller-runtime.certwatcher  Updated current TLS certificate
INFO    controller-runtime.webhook  serving webhook server  {"host": "", "port": 9443}
INFO    controller-runtime.certwatcher  Starting certificate watcher
INFO    controller-runtime.controller   Starting EventSource    {"controller": "cronjob", "source": "kind source: /, Kind="}
INFO    controller-runtime.controller   Starting Controller {"controller": "cronjob"}
INFO    controller-runtime.controller   Starting workers    {"controller": "cronjob", "worker count": 1}
</code></pre>

<p>From the log, it is easy to tell that both the controller and the admission webhook have been started successfully as expected.</p>

<p>In order to test if admissionWebhook is working, I make the CronJob schedule invalid like this:
<code>-*- * * * *</code>,</p>

<p>After apply the config: <code>kubectl apply -f config/samples/batch_v1_cronjob.yaml</code>,</p>

<p>No log from webhook were shown, and the only log showing that the cronjob schedule is invalid is from the code of controller:</p>

<pre><code>2020-02-22T15:45:17.665+0800    ERROR   controllers.Captain unable to figure out CronJob schedule   {"cronjob": "default/cronjob-sample", "error": "Unparseable schedule \"-*- * * * *\": Failed to parse int from : strconv.Atoi: parsing \"\": invalid syntax"}
github.com/go-logr/zapr.(*zapLogger).Error
    /Users/my-name/.go/pkg/mod/github.com/go-logr/zapr@v0.1.0/zapr.go:128
tutorial.kubebuilder.io/project/controllers.(*CronJobReconciler).Reconcile
    /Users/my-name/tmp/kubebuilder/docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller.go:380
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).reconcileHandler
    /Users/my-name/.go/pkg/mod/sigs.k8s.io/controller-runtime@v0.4.0/pkg/internal/controller/controller.go:256
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).processNextWorkItem
    /Users/my-name/.go/pkg/mod/sigs.k8s.io/controller-runtime@v0.4.0/pkg/internal/controller/controller.go:232
sigs.k8s.io/controller-runtime/pkg/internal/controller.(*Controller).worker
    /Users/my-name/.go/pkg/mod/sigs.k8s.io/controller-runtime@v0.4.0/pkg/internal/controller/controller.go:211
k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1
    /Users/my-name/.go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190913080033-27d36303b655/pkg/util/wait/wait.go:152
k8s.io/apimachinery/pkg/util/wait.JitterUntil
    /Users/my-name/.go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190913080033-27d36303b655/pkg/util/wait/wait.go:153
k8s.io/apimachinery/pkg/util/wait.Until
    /Users/my-name/.go/pkg/mod/k8s.io/apimachinery@v0.0.0-20190913080033-27d36303b655/pkg/util/wait/wait.go:88
</code></pre>

<p>so why webhook is not working?</p>

<p>You must create <code>ValidatingWebhookConfiguration</code> in order to configure apiserver to forward request for validation to your webhook. You can find it here: <a href="https://github.com/kubernetes-sigs/kubebuilder/blob/master/docs/book/src/cronjob-tutorial/testdata/project/config/webhook/manifests.yaml" rel="nofollow noreferrer">https://github.com/kubernetes-sigs/kubebuilder/blob/master/docs/book/src/cronjob-tutorial/testdata/project/config/webhook/manifests.yaml</a> </p>
