Hello World K8S Operator tutorial with Operator SDK and OpenShift — Part 1

This two parts guide walks through an example of building a simple hw-operator using the operator-sdk CLI tool and controller-runtime library API.

Dmitry Kartsev
Nov 9 · 3 min read
Photo by amin khorsand on Unsplash

In order to develop an operator, you will need to install the following tools

As for example, we going to implement simple hw-operator which will encode operational logic of deploying a new Nginx web server with single website. The website will be constructed from basic HTML code.

The hw-operator operator will take care of the following K8S/OpenShift objects

  • Deployment
  • Service
  • Router
  • ConfigMap with basic HTML code

The operator diagram

Let’s start from creating directory and operator project layout

mkdir -p  $GOPATH/src/github.comcd $GOPATH/src/github.comoperator-sdk new hw-operator --vendor=truecd hw-operator

Now, when we’ve created our empty operator project, let’s generate our first API. Add a new Custom Resource Definition(CRD) API called HelloWorld, with API versionhw.okto.io/v1alpha1 and Kind HelloWorld.

operator-sdk add api --api-version=hw.okto.io/v1alpha1 --kind=HelloWorld

This will scaffold the HelloWorld resource API under pkg/apis/hw/v1alpha1/...

Define the spec and status by modifying the spec and status of the HelloWorld Custom Resource(CR) at pkg/apis/hw/v1alpha1/helloworld_types.go.

Update the HelloWorldSpec struct to

type HelloWorldSpec struct {
Message string `json:"message"`

Update the HelloWorldStatus struct to

type HelloWorldStatus struct {
Message string `json:"message"`

After modifying the *_types.go file always run the following command to update the generated code for that resource type

operator-sdk generate k8s

To update the OpenAPI validation section in the CRD deploy/crds/hw_v1alpha1_helloworld_crd.yaml, run the following command

operator-sdk generate openapi

After each new code generation, you may run the following command to make sure that all dependencies are synced properly into vendor directory

go mod vendor

Add a new Controller to the project that will watch and reconcile the HelloWorld resource

operator-sdk add controller --api-version=hw.okto.io/v1alpha1 --kind=HelloWorld

This will scaffold a new Controller implementation under pkg/controller/helloworld/…

Since hw-operator will use OpenShift Route for exposing the website to external access, we’ll have to register 3rd Party Resources to the hw-operator

Edit the cmd/manager/main.go and add the following code

To the import section add the following

routev1 "github.com/openshift/api/route/v1"

To the main function, add the following

Note, your 3rd party resource needs to be added before add the controller in “Setup all Controllers”, also, don’t forget to run the go mod vendor to update vendor directory.

Add required imports into the import section of pkg/controller/helloworld/helloworld_controller.go

Add watches for each K8S object that hw-operator should manage by updating pkg/controller/helloworld/helloworld_controller.go . Update the add function with the following code

Now, when we’ve created our watches, let’s build the Reconcile functionality

First add the following functions to pkg/controller/helloworld/helloworld_controller.go

Let’s update the Reconcile loop with the following code

Add Finalizer functions

When everything in place, let’s try and run the hw-operator

Build Operator locally can be done by standart go build utils. Run the following command to build the hw-operator .

go build -o hw-operator cmd/manager/main.go

To run the hw-operator locally, outside of the cluster, we’ll need export following environment variables

export KUBERNETES_CONFIG=/path/to/kubeconfig
export SLACK_TOKEN=YOUR-SLACK-API-TOKEN # if empty finalizer will be skipped

Before running the hw-operator , let’s deploy the CRD

Once the CRD is deployed, we are ready to start our hw-operator


Now, let’s deploy our first HelloWorld CR

The hw-operator will deploy all required resource, once the Deployment’s pod become ready, try to access to your website. To get the website route, run the following command

echo https://$(oc get routes | grep example-helloworld  | awk '{print $2}')

The full Operator example can be found at GitHub

In the part 2 of this guide, you’ll learn how to build an Operator Docker image with Source2Image tool and create production grade deployment inside OpenShift Cluster.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade