main 文件修改什么?

还记得最开始我们说要回到 main.go 文件么? 让我们看看 main.go 新增了什么。

Apache License

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package main

import (

	clientgoscheme ""
	_ ""
	ctrl ""

	batchv1 ""
	// +kubebuilder:scaffold:imports

首先要注意的是 kubebuilder 已将新 API group 的 package(batchv1)添加到我们的 scheme 中了。 这意味着我们可以在 controller 中使用这些对象了。

如果要使用任何其他 CRD,则必须以相同的方式添加其 scheme。 诸如 Job 之类的内置类型的 scheme 是由 clientgoscheme 添加的。

var (
	scheme   = runtime.NewScheme()
	setupLog = ctrl.Log.WithName("setup")

func init() {
	_ = clientgoscheme.AddToScheme(scheme)

	_ = batchv1.AddToScheme(scheme)
	// +kubebuilder:scaffold:scheme

另一处更改是 kubebuilder 添加了一个块代码, 该代码调用了我们的 CronJob controller 的 SetupWithManager 方法。

func main() {
old stuff
	var metricsAddr string
	var enableLeaderElection bool
	flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
	flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
		"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")


	mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
		Scheme:             scheme,
		MetricsBindAddress: metricsAddr,
		LeaderElection:     enableLeaderElection,
		Port:               9443,
	if err != nil {
		setupLog.Error(err, "unable to start manager")
	if err = (&controllers.CronJobReconciler{
		Client: mgr.GetClient(),
		Log:    ctrl.Log.WithName("controllers").WithName("Captain"),
		Scheme: mgr.GetScheme(),
	}).SetupWithManager(mgr); err != nil {
		setupLog.Error(err, "unable to create controller", "controller", "Captain")

我们还会为我们的 type 设置 webhook。 我们只需要将它们添加到 manager 中就行。 由于我们可能想单独运行 webhook,或者在本地测试 controller 不运行它们,因此我们将其是否启动放在环境变量里面。

如不需要启动 webhook,只需要设置 ENABLE_WEBHOOKS = false 即可。

	if os.Getenv("ENABLE_WEBHOOKS") != "false" {
		if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
			setupLog.Error(err, "unable to create webhook", "webhook", "Captain")
	// +kubebuilder:scaffold:builder
old stuff
	setupLog.Info("starting manager")
	if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
		setupLog.Error(err, "problem running manager")

现在,我们可以实现我们的 controller 了