You said something about main?

But first, remember how we said we’d come back to main.goagain? Let’s take a look and see what’schanged, and what we need to add.

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

  1. http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License. Imports

  1. package main
  2. import (
  3. "flag"
  4. "os"
  5. kbatchv1 "k8s.io/api/batch/v1"
  6. "k8s.io/apimachinery/pkg/runtime"
  7. clientgoscheme "k8s.io/client-go/kubernetes/scheme"
  8. _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
  9. ctrl "sigs.k8s.io/controller-runtime"
  10. "sigs.k8s.io/controller-runtime/pkg/log/zap"
  11. batchv1 "tutorial.kubebuilder.io/project/api/v1"
  12. "tutorial.kubebuilder.io/project/controllers"
  13. // +kubebuilder:scaffold:imports
  14. )

The first difference to notice is that kubebuilder has added the new APIgroup’s package (batchv1) to our scheme. This means that we can use thoseobjects in our controller.

We’ll also need to add the kubernetes batch v1 (kbatchv1) scheme, since we’re creatingand listing Jobs.

  1. var (
  2. scheme = runtime.NewScheme()
  3. setupLog = ctrl.Log.WithName("setup")
  4. )
  5. func init() {
  6. _ = clientgoscheme.AddToScheme(scheme)
  7. _ = kbatchv1.AddToScheme(scheme) // we've added this ourselves
  8. _ = batchv1.AddToScheme(scheme)
  9. // +kubebuilder:scaffold:scheme
  10. }

The other thing that’s changed is that kubebuilder has added a block calling ourCronJob controller’s SetupWithManager method. Since we now use a Scheme as well,we’ll need to pass that to the reconciler ourselves.

  1. func main() {

old stuff

  1. var metricsAddr string
  2. var enableLeaderElection bool
  3. flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.")
  4. flag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
  5. "Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
  6. flag.Parse()
  7. ctrl.SetLogger(zap.Logger(true))
  8. mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
  9. Scheme: scheme,
  10. MetricsBindAddress: metricsAddr,
  11. LeaderElection: enableLeaderElection,
  12. })
  13. if err != nil {
  14. setupLog.Error(err, "unable to start manager")
  15. os.Exit(1)
  16. }
  1. if err = (&controllers.CronJobReconciler{
  2. Client: mgr.GetClient(),
  3. Log: ctrl.Log.WithName("controllers").WithName("Captain"),
  4. Scheme: mgr.GetScheme(), // we've added this ourselves
  5. }).SetupWithManager(mgr); err != nil {
  6. setupLog.Error(err, "unable to create controller", "controller", "Captain")
  7. os.Exit(1)
  8. }
  9. if err = (&batchv1.CronJob{}).SetupWebhookWithManager(mgr); err != nil {
  10. setupLog.Error(err, "unable to create webhook", "webhook", "Captain")
  11. os.Exit(1)
  12. }
  13. // +kubebuilder:scaffold:builder

old stuff

  1. setupLog.Info("starting manager")
  2. if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
  3. setupLog.Error(err, "problem running manager")
  4. os.Exit(1)
  5. }
  1. }

Now we can implement our controller.