Tutorial: Building CronJob

Too many tutorials start out with some really contrived setup, or some toyapplication that gets the basics across, and then stalls out on the morecomplicated stuff. Instead, this tutorial should take you through (almost)the full gamut of complexity with Kubebuilder, starting off simple andbuilding up to something pretty full-featured.

Let’s pretend (and sure, this is a teensy bit contrived) that we’vefinally gotten tired of the maintenance burden of the non-Kubebuilderimplementation of the CronJob controller in Kubernetes, and we’d like torewrite it using KubeBuilder.

The job (no pun intended) of the CronJob controller is to run one-offtasks on the Kubernetes cluster at regular intervals. It does this bybuilding on top of the Job controller, whose task is to run one-off tasksonce, seeing them to completion.

Instead of trying to tackle rewriting the Job controller as well, we’lluse this as an opportunity to see how to interact with external types.

Scaffolding Out Our Project

As covered in the quick start, we’ll need to scaffoldout a new project. Make sure you’ve installedKubebuilder, then scaffold out a newproject:

  1. # we'll use a domain of tutorial.kubebuilder.io,
  2. # so all API groups will be <group>.tutorial.kubebuilder.io.
  3. kubebuilder init --domain tutorial.kubebuilder.io

Now that we’ve got a project in place, let’s take a look at whatKubebuilder has scaffolded for us so far…