Overview

Previously, I wrote two articles:

In these articles, I introduced the concept of Operator and the ways to implement it. Now, almost 5 years have passed since those articles were written. Although the Operator SDK hasn’t changed much, it has seen some enhancements. Therefore, in this article, I will further introduce a new tool: OLM.

Installing Operator-SDK

  1. [root@liqiang.io]# export ARCH=$(case $(uname -m) in x86_64) echo -n amd64 ;; aarch64) echo -n arm64 ;; *) echo -n $(uname -m) ;; esac)
  2. [root@liqiang.io]# export OS=$(uname | awk '{print tolower($0)}')
  3. [root@liqiang.io]# export OPERATOR_SDK_DL_URL=https://github.com/operator-framework/operator-sdk/releases/download/v1.34.2
  4. [root@liqiang.io]# curl -LO ${OPERATOR_SDK_DL_URL}/operator-sdk_${OS}_${ARCH}
  5. [root@liqiang.io]# chmod +x operator-sdk_${OS}_${ARCH} && sudo mv operator-sdk_${OS}_${ARCH} /usr/local/bin/operator-sdk
  6. [root@liqiang.io]# operator-sdk version

Creating an Operator

Initializing the Operator framework

  1. [root@liqiang.io]# operator-sdk init

Writing CRD and Controller

  1. [root@liqiang.io]# operator-sdk create api --group test --version=v1alpha1 --kind=CronjobRuntime --resource --controller
  2. [root@liqiang.io]# make docker-build docker-push IMG="liqiangliu443/operator-test:v0.0.1"

At this point, our Operator has been packaged into an image and published to a remote repository.

[Optional] Modifying CRD

If you want to modify the CRD (usually to add new fields, be cautious when modifying CRD fields), you just need to edit the CRD struct in the Go files under the /api directory. After making changes, run the following command to generate a new CRD definition:

  1. [root@liqiang.io]# make manifests

OLM

OLM (Operator Lifecycle Manager): Used for managing the lifecycle of Operators.

Installing OLM

  1. [root@liqiang.io]# operator-sdk olm install

Publishing a Bundle

  1. [root@liqiang.io]# make bundle IMG="liqiangliu443/operator-test:v0.0.1"
  2. [root@liqiang.io]# make bundle-build bundle-push BUNDLE_IMG="liqiangliu443/operator-bundle-test:v0.0.1"
  3. [root@liqiang.io]#

At this point, our operator has been packaged into a bundle and published to a remote repository.

Running the Bundle

  1. [root@liqiang.io]# operator-sdk run bundle docker.io/liqiangliu443/operator-bundle-test:v0.0.1

At this point, our operator will be running in the corresponding Kubernetes cluster.

Uninstalling the Bundle

  1. [root@liqiang.io]# operator-sdk cleanup tenant-deploy

At this point, our operator, including the CRD definitions, has been removed from the Kubernetes cluster.

Manual Deployment

Of course, we can also manage the Operator without using OLM, for example, by running the Operator manually.

  1. [root@liqiang.io]# make install
  2. [root@liqiang.io]# make deploy IMG="liqiangliu443/operator-test:v0.0.1"

Where:

Uninstallation

  1. [root@liqiang.io]# make uninstall
  2. [root@liqiang.io]# make undeploy