Designing an API

In Kubernetes, we have a few rules for how we design APIs. Namely, allserialized fields must be camelCase, so we use JSON struct tags tospecify this. We can also use the omitempty struct tag to mark thata field should be omitted from serialization when empty.

Fields may use most of the primitive types. Numbers are the exception:for API compatibility purposes, we accept two forms of numbers: int32for integers, and resource.Quantity for decimals.

Hold up, what’s a Quantity?

Quantities are a special notation for decimal numbers that have anexplicitly fixed representation that makes them more portable acrossmachines. You’ve probably noticed them when specifying resources requestsand limits on pods in Kubernetes.

They conceptually work similar to floating point numbers: they havea significand, base, and exponent. Their serialize, human readable foruses whole numbers and suffixes to specify values much the way we describecomputer storage.

For instance, the value 2m means 0.002 in decimal notation. 2Kimeans 2048 in decimal, while 2K means 2000 in decimal. If we wantto specify fractions, we switch to a suffix that lets us use a wholenumber: 2.5 is 2500m.

There are two supported bases: 10 and 2 (called decimal and binary,respectively). Decimal base is indicated with “normal” SI suffixes (e.g.M and K), while Binary base is specified in “mebi” notation (e.g. Miand Ki). Think megabytes vsmebibytes.

There’s one other special type that we use: metav1.Time. This functionsidentically to time.Time, except that it has a fixed, portableserialization format.

With that out of the way, let’s take a look at what our CronJob objectlooks like!

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 v1
  2. import (
  3. batchv1beta1 "k8s.io/api/batch/v1beta1"
  4. corev1 "k8s.io/api/core/v1"
  5. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  6. )
  7. // EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
  8. // NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

First, let’s take a look at our spec. As we discussed before, spec holdsdesired state, so any “inputs” to our controller go here.

Fundamentally a CronJob needs the following pieces:

  • A schedule (the cron in CronJob)
  • A template for the Job to run (thejob in CronJob) We’ll also want a few extras, which will make our users’ lives easier:

  • A deadline for starting jobs (if we miss this deadline, we’ll just wait tillthe next scheduled time)

  • What to do if multiple jobs would run at once (do we wait? stop the old one? run both?)
  • A way to pause the running of a CronJob, in case something’s wrong with it
  • Limits on old job history Remember, since we never read our own status, we need to have some other way tokeep track of whether a job has run. We can use at least one old job to dothis.

We’ll use several markers (// +comment) to specify additional metadata. Thesewill be used by controller-tools when generating our CRD manifest.As we’ll see in a bit, controller-tools will also use GoDoc to form descriptions forthe fields.

  1. // CronJobSpec defines the desired state of CronJob
  2. type CronJobSpec struct {
  3. // +kubebuilder:validation:MinLength=0
  4. // The schedule in Cron format, see https://en.wikipedia.org/wiki/Cron.
  5. Schedule string `json:"schedule"`
  6. // +kubebuilder:validation:Minimum=0
  7. // Optional deadline in seconds for starting the job if it misses scheduled
  8. // time for any reason. Missed jobs executions will be counted as failed ones.
  9. // +optional
  10. StartingDeadlineSeconds *int64 `json:"startingDeadlineSeconds,omitempty"`
  11. // Specifies how to treat concurrent executions of a Job.
  12. // Valid values are:
  13. // - "Allow" (default): allows CronJobs to run concurrently;
  14. // - "Forbid": forbids concurrent runs, skipping next run if previous run hasn't finished yet;
  15. // - "Replace": cancels currently running job and replaces it with a new one
  16. // +optional
  17. ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy,omitempty"`
  18. // This flag tells the controller to suspend subsequent executions, it does
  19. // not apply to already started executions. Defaults to false.
  20. // +optional
  21. Suspend *bool `json:"suspend,omitempty"`
  22. // Specifies the job that will be created when executing a CronJob.
  23. JobTemplate batchv1beta1.JobTemplateSpec `json:"jobTemplate"`
  24. // +kubebuilder:validation:Minimum=0
  25. // The number of successful finished jobs to retain.
  26. // This is a pointer to distinguish between explicit zero and not specified.
  27. // +optional
  28. SuccessfulJobsHistoryLimit *int32 `json:"successfulJobsHistoryLimit,omitempty"`
  29. // +kubebuilder:validation:Minimum=0
  30. // The number of failed finished jobs to retain.
  31. // This is a pointer to distinguish between explicit zero and not specified.
  32. // +optional
  33. FailedJobsHistoryLimit *int32 `json:"failedJobsHistoryLimit,omitempty"`
  34. }

We define a custom type to hold our concurrency policy. It’s actuallyjust a string under the hood, but the type gives extra documentation,and allows us to attach validation on the type instead of the field,making the validation more easily reusable.

  1. // ConcurrencyPolicy describes how the job will be handled.
  2. // Only one of the following concurrent policies may be specified.
  3. // If none of the following policies is specified, the default one
  4. // is AllowConcurrent.
  5. // +kubebuilder:validation:Enum=Allow;Forbid;Replace
  6. type ConcurrencyPolicy string
  7. const (
  8. // AllowConcurrent allows CronJobs to run concurrently.
  9. AllowConcurrent ConcurrencyPolicy = "Allow"
  10. // ForbidConcurrent forbids concurrent runs, skipping next run if previous
  11. // hasn't finished yet.
  12. ForbidConcurrent ConcurrencyPolicy = "Forbid"
  13. // ReplaceConcurrent cancels currently running job and replaces it with a new one.
  14. ReplaceConcurrent ConcurrencyPolicy = "Replace"
  15. )

Next, let’s design our status, which holds observed state. It contains any informationwe want users or other controllers to be able to easily obtain.

We’ll keep a list of actively running jobs, as well as the last time that we succesfullyran our job. Notice that we use metav1.Time instead of time.Time to get the stableserialization, as mentioned above.

  1. // CronJobStatus defines the observed state of CronJob
  2. type CronJobStatus struct {
  3. // INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
  4. // Important: Run "make" to regenerate code after modifying this file
  5. // A list of pointers to currently running jobs.
  6. // +optional
  7. Active []corev1.ObjectReference `json:"active,omitempty"`
  8. // Information when was the last time the job was successfully scheduled.
  9. // +optional
  10. LastScheduleTime *metav1.Time `json:"lastScheduleTime,omitempty"`
  11. }

Finally, we have the rest of the boilerplate that we’ve already discussed.As previously noted, we don’t need to change this, except to mark thatwe want a status subresource, so that we behave like built-in kubernetes types.

  1. // +kubebuilder:object:root=true
  2. // +kubebuilder:subresource:status
  3. // CronJob is the Schema for the cronjobs API
  4. type CronJob struct {

Root Object Definitions

  1. metav1.TypeMeta `json:",inline"`
  2. metav1.ObjectMeta `json:"metadata,omitempty"`
  3. Spec CronJobSpec `json:"spec,omitempty"`
  4. Status CronJobStatus `json:"status,omitempty"`
  5. }
  6. // +kubebuilder:object:root=true
  7. // CronJobList contains a list of CronJob
  8. type CronJobList struct {
  9. metav1.TypeMeta `json:",inline"`
  10. metav1.ListMeta `json:"metadata,omitempty"`
  11. Items []CronJob `json:"items"`
  12. }
  13. func init() {
  14. SchemeBuilder.Register(&CronJob{}, &CronJobList{})
  15. }

Now that we have an API, we’ll need to write a controller to actuallyimplement the functionality.