Digital Developer Conference: Cloud Security 2021 – Build the skills to secure your cloud and data Register free

Set up your environment

To follow the steps in this learning path for developing Golang-based operators, you need to install the following software on your machine. Environment-specific installation instructions for installing Operator SDK and the oc or kubectl CLIs and for provisioning a cluster are below.

Prerequisites for installing Operator SDK on a macOS

To develop Golang-based operators on macOS, you need to install the following:

  • Homebrew
  • Go 1.10+
  • Kustomize
  • kubectl
  • Docker v17.03+
  • OpenShift CLI (If you plan to deploy to OpenShift cluster) oc
  • To use an OpenShift cluster, we recommend using version 4.6+

Additionally, you need:

  • Access to a Kubernetes v1.11.3+ cluster (v1.16.0+ if using apiextensions.k8s.io/v1 CRDs). See minikube or CodeReady Containers to access a free cluster.
  • Cluster admin permission. See how to grant yourself cluster-admin privileges or be logged in as admin.
  • Access to a container registry such as Quay.io or DockerHub

Prerequisites for installing opeartor-sdk on Linux and Windows

To develop Golang-based operators on Linux and Windows, you need to install the following:

  • dep v0.5.0+
  • Git
  • Go v1.10+
  • Docker v17.03+
  • OpenShift CLI (oc) v4.1+ installed
  • Kustomize
  • OpenShift CLI (If you plan to deploy to OpenShift Cluster) oc
  • To use an OpenShift cluster, we recommend you use version 4.6+

Additionally, you need:

Steps

  1. Install Operator SDK
  2. Install oc or kubectl cli
  3. (Optional) Provision OpenShift Cluster

1. Install Operator SDK

Install Operator SDK (version 1.5+) for macOS

  1. Install Homebrew if you don’t have it.
  2. Use the Homebrew command brew install operator-sdk to install Operator SDK for macOS. This guide is tested for operator-sdk version 1.5+, since the commands have changed with the 1.0 release.

Install Operator SDK (version 1.5+) for Linux or Windows

  • For Linux or Windows, install the Operator SDK (version 1.0+) from the GitHub release. Note that commands have changed with the 1.0 release.

Test your environment for Operator SDK

  1. Run the following command in the terminal of your choice:

     $ operator-sdk version
    
  2. You should see output like this:

     operator-sdk version: "v1.5.0", commit: "1abf57985b43bf6a59dcd18147b3c574fa57d3f6", kubernetes version: "v1.19.4", go version: "go1.15.5", GOOS: "darwin", GOARCH: "amd64"
    
  3. Ensure kustomize is installed. Install it via brew for macOS:

$ brew install kustomize
$ kustomize version

You should see output like this:

{Version:kustomize/v3.9.1 GitCommit:7439f1809e5ccd4677ed52be7f98f2ad75122a93 BuildDate:2020-12-30T01:08:17+00:00 GoOs:darwin GoArch:amd64}

2. Install oc or kubectl cli

If you plan to use an OpenShift cluster, install the OpenShift CLI (oc) using instructions in the OpenShift docs.

Otherwise, install kubectl from the Kubernetes docs.

Alternatively, you can install the oc CLI through the OpenShift web console as this article describes.

Test your CLI by issuing the following command to see the version of your CLI:

$ oc version
Client Version: openshift-clients-4.5.0-202006231303.p0-18-g6082e941e
Kubernetes Version: v1.19.2

If you plan to use kubectl instead of oc use:

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-14T05:15:04Z", GoVersion:"go1.15.6", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"18+", GitVersion:"v1.18.3+e574db2", GitCommit:"e574db2", GitTreeState:"clean", BuildDate:"2021-01-30T06:33:00Z", GoVersion:"go1.13.15", Compiler:"gc", Platform:"linux/amd64"}

3. Provision an OpenShift cluster

(If you haven’t already) provision an OpenShift cluster by going to https://cloud.ibm.com/ and clicking Red Hat OpenShift on IBM Cloud tile. From there, you can select an OpenShift cluster of your choosing.

We recommend you use an OpenShift cluster of version 4.6 or higher.

OpenShift

After the cluster finishes provisioning, you should get a message that the Worker nodes are 100% normal, and the Master status is ready, as the picture below shows.

OpenShift

Provisioning an OpenShift cluster takes some time, so do so now if you don’t have one already. Skip down to the prepare your OpenShift Cluster step to see how to create an OpenShift cluster on IBM Cloud.

Ensure OpenShift Lifecycle Manager (OLM) is updated

To make sure your OpenShift Lifecycle Manager is up to date and running properly, run the operator-sdk olm status command:

operator-sdk olm status
INFO[0003] Fetching CRDs for version "0.16.1"           
INFO[0003] Using locally stored resource manifests      
INFO[0005] Successfully got OLM status for version "0.16.1"
NAME                                            NAMESPACE    KIND                        STATUS
operators.operators.coreos.com                               CustomResourceDefinition    Installed
operatorgroups.operators.coreos.com                          CustomResourceDefinition    Installed
installplans.operators.coreos.com                            CustomResourceDefinition    Installed
clusterserviceversions.operators.coreos.com                  CustomResourceDefinition    Installed
olm-operator                                    olm          Deployment                  Installed
subscriptions.operators.coreos.com                           CustomResourceDefinition    Installed
olm-operator-binding-olm                                     ClusterRoleBinding          Installed
operatorhubio-catalog                           olm          CatalogSource               Installed
olm-operators                                   olm          OperatorGroup               Installed
aggregate-olm-view                                           ClusterRole                 Installed
catalog-operator                                olm          Deployment                  Installed
aggregate-olm-edit                                           ClusterRole                 Installed
olm                                                          Namespace                   Installed
global-operators                                operators    OperatorGroup               Installed
operators                                                    Namespace                   Installed
packageserver                                   olm          ClusterServiceVersion       Installed
olm-operator-serviceaccount                     olm          ServiceAccount              Installed
catalogsources.operators.coreos.com                          CustomResourceDefinition    Installed
system:controller:operator-lifecycle-manager                 ClusterRole                 Installed

As you can see from this output above, all of the components of the OLM are in the Installed status which means your Operator Lifecycle Manager is working properly.

Note: If you see an error, you can read the SDK guide to learn how to install OLM on your cluster.

(Optional) Troubleshooting OLM error

If you get an error similar to the following two, you need to uninstall a specific version

Error 1

$ operator-sdk olm status
FATA[0002] Failed to get OLM status: error getting installed OLM version (set --version to override the default version): no existing installation found

Error 2

$ operator-sdk olm status
FATA[0002] Failed to get OLM status: error getting installed OLM version (set --version to override the default version): no existing installation found


$ operator-sdk olm install
FATA[0005] Failed to install OLM version "latest": detected existing OLM resources: OLM must be completely uninstalled before installation


$ operator-sdk olm uninstall
FATA[0002] Failed to uninstall OLM: error getting installed OLM version (set --version to override the default version): no existing installation found

These errors indicate the need to uninstall a specific version. My OpenShift cluster is version 4.5.31_1531, so I had to uninstall version 0.16.1 using the following command.

$ operator-sdk olm uninstall --version 0.16.1
INFO[0009] Successfully uninstalled OLM version "0.16.1"

Next, I installed the same version, and checked the status:

$ operator-sdk olm install --version 0.16.1
INFO[0072] Successfully installed OLM version "0.16.1"  

$ operator-sdk olm status
INFO[0004] Successfully got OLM status for version "0.16.1"

NAME                                            NAMESPACE    KIND                        STATUS
operators.operators.coreos.com                               CustomResourceDefinition    Installed
operatorgroups.operators.coreos.com                          CustomResourceDefinition    Installed
installplans.operators.coreos.com                            CustomResourceDefinition    Installed
clusterserviceversions.operators.coreos.com                  CustomResourceDefinition    Installed
olm-operator                                    olm          Deployment                  Installed
subscriptions.operators.coreos.com                           CustomResourceDefinition    Installed
olm-operator-binding-olm                                     ClusterRoleBinding          Installed
operatorhubio-catalog                           olm          CatalogSource               Installed
olm-operators                                   olm          OperatorGroup               Installed
aggregate-olm-view                                           ClusterRole                 Installed
catalog-operator                                olm          Deployment                  Installed
aggregate-olm-edit                                           ClusterRole                 Installed
olm                                                          Namespace                   Installed
global-operators                                operators    OperatorGroup               Installed
operators                                                    Namespace                   Installed
packageserver                                   olm          ClusterServiceVersion       Installed
olm-operator-serviceaccount                     olm          ServiceAccount              Installed
catalogsources.operators.coreos.com                          CustomResourceDefinition    Installed
system:controller:operator-lifecycle-manager                 ClusterRole                 Installed

Once everything says installed, congratulations you are ready to start developing!

Conclusion

If you’ve installed these prerequisites, your environment is set up to develop an operator and deploy it to an OpenShift (or Kubernetes) cluster.