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

In the part 2 of the Hello World Operator tutorial you’ll learn how to build docker image for the hw-operator and run the operator inside the OpenShift cluster.

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

Operator SDK provides out-of-the-box tolling to build a docker image for your operator. You can build an operator image by running the following command

operator-sdk build <image>

The Operator SDK out-of-the-box build solution based on Dockerfile and provides an opinionated way of building operator docker image which is not alway fits with underlying ecosystem. For example, the build system may not include the operator-sdk cli, or you may need to apply some custom build logic during the build process.

For building the source and pack it into a docker image we’ll use Source2Image tool. OpenShift BuildConfig and SourceStrategy uses S2I as a default build engine, which means that once we’ll create a local S2I build, it will work the same inside OpenShift cluster, as a result, we’ll enjoy a Cloud Native way of building the source code and docker images. In this tutorial we’ll use existing S2I build image, if you are interested to create your own S2I build image or learn more deeply about S2I tool, see the S2I docs.

In short, S2I uses two files which are cover the build and run stages.

  • assemble file lists all required steps to build your source code.
  • run file lists all required steps to run your executable inside the container.

The assemble and run files could be burned into S2I build image or provided/overrated by the end user. In our example, we’ll use our own assemble and run files, while S2I build image will includes only required build tools such as go binary.

S2I will look for users provided assemble and run files under .s2i/bin directory. Create required directory and files in your project root directory

mkdir -p .s2i/bintouch .s2i/bin/assembletouch .s2i/bin/run

Create the assemble script as following

#!/bin/ash
set -x
mv /tmp/src /go/src/hw-operator
cd /go/src/hw-operator
export GO111MODULE=on
echo "Building sources..."
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -x -o /go/bin/hw-operator -mod vendor cmd/manager/main.go
rm -fr /go/src/hw-operator
ls -all /go/bin

Create the run script as following

#!/bin/ash
set -x
hw-operator

Don’t forget to add the .s2i folder into your git repo. To start s2i local build cd into your project root directory and run the following command

s2i build . docker.io/dimssss/golang-s2i:0.5 <reslut-image>

Follow the build logs, once the build successfully finished you may try and run your operator image locally by running the following command

docker run -it <result-image> 

Building operator image locally with S2I can useful for learning or debugging use cases, for production grade setups, use OpenShift BuildConfig with type: SourceStrategy . Use following yaml to create your own BuildConfig object. Make sure to replace all placeholder with relevant values before creating the BuildConfig object.

Issue new BuildConfig build by running the following command

oc start-build <buildconfig-name> --follow

Now, we are ready to deploy our operator inside the cluster.

To deploy operator we’ll need to create following objects

  • Service Account
  • Role
  • Role Binding
  • Deployment

Fortunately, the Operat SDK will generate all required manifests us, and yet, you may require to update the Role manifest and include any non default permissions that my required for proper functionality of our hw-operator .

Deploy the operator by creating the following objecst

Service Account

Role

Role Binding

Deployment, do not forget to update the image directive with valid image name

Note, the CRD have to be deployed before actual deployment of hw-operator , in case the hw-operator CRD not deployed yet, create the following object

Now, you are read to deploy actual website by creating an CR

The hw-operator will crate all required resources and once the operator reconciliation loop fill be successfully finished, try and open your website. To get the website url execute the following command

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

In this 2 parts tutorial you’ve learned how to develop, build and deploy your operator.

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