我正在关注KubeBuilder书中的CronJob示例:https://book.kubebuilder.io/cronjob-tutorial/cronjob-tutorial.html
我直接使用https://github.com/kubernetes-sigs/kubebuilder/tree/master/docs/book/src/cronjob-tutorial/testdata/project中的代码
运行make run
后,显示如下日志:
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}
从日志中很容易看出, Controller 和准入Webhook均已按预期成功启动。
为了测试admissionWebhook是否正常工作,我使CronJob时间表无效,如下所示:
-*- * * * *
,应用配置后:
kubectl apply -f config/samples/batch_v1_cronjob.yaml
,没有显示来自webhook的日志,唯一显示cronjob调度无效的日志来自 Controller 代码:
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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/pkg/internal/controller/controller.go:211
k8s.io/apimachinery/pkg/util/wait.JitterUntil.func1
/Users/my-name/.go/pkg/mod/k8s.io/[email protected]/pkg/util/wait/wait.go:152
k8s.io/apimachinery/pkg/util/wait.JitterUntil
/Users/my-name/.go/pkg/mod/k8s.io/[email protected]/pkg/util/wait/wait.go:153
k8s.io/apimachinery/pkg/util/wait.Until
/Users/my-name/.go/pkg/mod/k8s.io/[email protected]/pkg/util/wait/wait.go:88
那么为什么webhook无法正常工作?
最佳答案
您必须创建ValidatingWebhookConfiguration
才能将apiserver配置为将验证请求转发到您的Webhook。您可以在这里找到它:https://github.com/kubernetes-sigs/kubebuilder/blob/master/docs/book/src/cronjob-tutorial/testdata/project/config/webhook/manifests.yaml
关于kubernetes - 为什么kubebuilder书中的CronJob示例中的准入webhook无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60350211/