https://book.kubebuilder.io/cronjob-tutorial/
- Kustomize YAML defs for launching controller in cluster
- Kustomize base in
config/default
config/manager
: Launch controllers as pods in cluster/config/rbac
: permissions required to run controllers under svc acct
- Runs all of our controllers and webhooks;
- Sets up clients to API server
Convention: resources are always the lowercase form of the Kind
Kind
: API typeResource
: A usage of aKind
in the API- Usually a 1:1 mapping between Kinds and resources, but with CRDs, each Kind corresponds to a single resource
Go
: GVK for GroupVersionKindGo
: GVR for GroupVersionResourceScheme
: tracking what Go type (GVR) corresponds to a GVK. (seegroupversion_info.go
)
CRDs are the canonical API for custom resources;
... Controllers are the API's business logic;
... Controllers reconcile
state
andspec
.
CRDs are a definition of our customized Objects, and the CRs are an instance of it.
- New APIs are how we teach Kubernetes about our custom objects;
Go structs
generate aCRD
, which includes the dataschema
- We then create instances of our custom objects;
- Those instances are managed by
controllers
. Controllers manage the CR.
- Add API. Add new resource and controller, too.
kubebuilder create api --group batch --version v1 --kind CronJob
- This creates
/api/vi
dir which corresponds tobatch.{DOMAIN}/v1
per our--domain
settings from earlier, so ours would bebatch.example.com/v1
. - This also creates file for
Cronjob Kind
,/api/v1/cronjob_types.go
; - Each time we call the command with a different Kind, a new file will be created!
Cronjob Kind file
cronjob_types.go
- All Kubernetes objects have:
TypeMeta
(API Version and Kind)Objectmeta
(name, namespace, labels)
Struct tags for serialization
runtime.Object
interface
- all types representing Kinds must implement this
- kubebuilder generates this via
marker comments
forcontroller-tools
codegen - marker comments for codegen look like
+kubebuilder:object:root=true
- See
zz_generated.deepcopy.go
which contains the autogen implementation ofruntime.Object
interface. - The core of the runtime.Object interface is a deep-copy method,
DeepCopyObject
.
What does a cronjob need? a
cron
and ajob
. Deadlines are nice to have too, and handling concurrency is nice.
- This is why our
CronjobSpec
struct has fields for these concerns.
Remember, we don't read our own
Status
; we use old jobs instead to keep track of whether a job has run.
Remember that we don't read our own
Status
; we use old jobs to do this instead. Contains any information we want users or other controllers to get
- List of actively running jobs, and, the last time we successfully ran a job.