Kube: Operators, Custom Resources, and Custom Resource Definitions - zhuje/openshift-wiki GitHub Wiki

Diagrams

https://www.cncf.io/wp-content/uploads/2022/07/k8s-operator.webp

https://images.contentstack.io/v3/assets/blt300387d93dabf50e/blt23908721de556466/6299717ed0f3ca0f53e0a1e3/3Creating_Custom_Kubernetes_Operators_3.png

Definitions

Resource = aka kind, objects. This is a Kubernetes API endpoint that stores a collection of API objects of a certain kind. Example is a pods resource contains a collection of Pod objects.

Controllers = rectifies desired state vs. actual state by creating, configuring, and managing instances of your application

Operators = custom controllers

Custom Resources = custom kinds

Custom Resource Definitions = files which define a schema for your custom resources. It defines what configurations are allowed in your custom resource.

How Operators, CRs, and CRDs work together

“ K8s Operators are controllers for packaging, managing, and deploying applications on Kubernetes. In order to do these things, the Operator uses Custom Resources (CR) that define the desired configuration and state of a specific application through Custom Resource Definitions (CRD).” [4]

Operators are custom controllers watches the cluster’s current state. If the current state doesn’t match the desired state, the operator will take action to create, configure, and manage instances of your application. What defines the desired state is the custom resources (CRs). Custom Resource Definitions are schemas, they define what values are allowed when a user creates a custom resource.

Custom Resource Definitions Validate a Custom Resource: Example

One of the purposes of CRDs is to validate CRs.

Example below is taken from : https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation

CRD

The schema is defined in the CustomResourceDefinition. In the following example, the CustomResourceDefinition applies the following validations on the custom object:

  • spec.cronSpec must be a string and must be of the form described by the regular expression.
  • spec.replicas must be an integer and must have a minimum value of 1 and a maximum value of 10.

Save the CustomResourceDefinition to resourcedefinition.yaml:

Invalid CR — Rejected

Valid CR — Accepted

Callouts

Operators are automations meant to replace ‘human operators’.

“In addition, Operators actually allow for automatic implementation of typical Day-1 tasks (installation, configuration, etc.) and Day-2 tasks (reconfiguration, upgrade, backup, failover, recovery, etc.), for a software running within the Kubernetes cluster, integrating natively with Kubernetes concepts and APIs. …In this way, the Operators serve the purpose of automating the entire life cycle of the software they manage, taking over the classic tasks to be done for day 1 and day 2 that, without automation, would have to be performed by one or more human operators.” [4]

Operator Capability Levels

image Source

Sources

  1. Creating Custom Kubernetes Operators
    1. This is a really good overview and contains code examples
    2. https://www.weave.works/blog/creating-custom-kubernetes-operators
  2. Create a CustomResourceDefinition. https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#create-a-customresourcedefinition
    1. JZ Notes: this is a great article to see the a full example of what a CRD looks like and situations user defined CR would be accepted vs. reject if it doesn’t meet the parameters set by the CRD.
  3. How to create a custom resource definition in OpenShift v4.13
    1. https://docs.openshift.com/container-platform/4.13/operators/understanding/crds/crd-extending-api-with-crds.html#crd-creating-custom-resources-definition_crd-extending-api-with-crds
  4. Kubernetes Operators: what are they? Some examples https://www.cncf.io/blog/2022/06/15/kubernetes-operators-what-are-they-some-examples/
  5. Kubernetes — Objects (Resources/Kinds) overview
    1. This was a great list of all the different groups of native Object in Kubernetes
    2. https://medium.com/devops-mojo/kubernetes-objects-resources-overview-introduction-understanding-kubernetes-objects-24d7b47bb018