Getting started with Operator SDK for Go-based Operators

To demonstrate the basics of setting up and running a Go-based Operator using tools and libraries provided by the Operator SDK, Operator developers can build an example Go-based Operator for Memcached, a distributed key-value store, and deploy it to a cluster.

Prerequisites

Creating and deploying Go-based Operators

You can build and deploy a simple Go-based Operator for Memcached by using the Operator SDK.

Procedure

  1. Create a project.

    1. Create your project directory:

      1. $ mkdir memcached-operator
    2. Change into the project directory:

      1. $ cd memcached-operator
    3. Run the operator-sdk init command to initialize the project:

      1. $ operator-sdk init \
      2. --domain=example.com \
      3. --repo=github.com/example-inc/memcached-operator

      The command uses the Go plug-in by default.

    4. To enable your Go-based Operator to run on OKD, edit the config/manager/manager.yaml file and replace the following line:

      1. runAsUser: 65532

      with:

      1. runAsNonRoot: true

      This step is a temporary workaround required for Go-based Operators only. For more information, see BZ#1914406.

  2. Create an API.

    Create a simple Memcached API:

    1. $ operator-sdk create api \
    2. --resource=true \
    3. --controller=true \
    4. --group cache \
    5. --version v1 \
    6. --kind Memcached
  3. Build and push the Operator image.

    Use the default Makefile targets to build and push your Operator. Set IMG with a pull spec for your image that uses a registry you can push to:

    1. $ make docker-build docker-push IMG=<registry>/<user>/<image_name>:<tag>
  4. Run the Operator.

    1. Install the CRD:

      1. $ make install
    2. Deploy the project to the cluster. Set IMG to the image that you pushed:

      1. $ make deploy IMG=<registry>/<user>/<image_name>:<tag>
  5. Create a sample custom resource (CR).

    1. Create a sample CR:

      1. $ oc apply -f config/samples/cache_v1_memcached.yaml \
      2. -n memcached-operator-system
    2. Watch for the CR to reconcile the Operator:

      1. $ oc logs deployment.apps/memcached-operator-controller-manager \
      2. -c manager \
      3. -n memcached-operator-system
  6. Clean up.

    Run the following command to clean up the resources that have been created as part of this procedure:

    1. $ make undeploy

Next steps