Quickstart for Go-based Operators

A simple set of instructions that demonstrates the basics of setting up and running Go-based operator.

This guide walks through an example of building a simple memcached-operator using tools and libraries provided by the Operator SDK.

Prerequisites

  • Install operator-sdk and its prequisites.
  • Access to a Kubernetes v1.11.3+ cluster (v1.16.0+ if using apiextensions.k8s.io/v1 CRDs).
  • User authorized with cluster-admin permissions

Quickstart Steps

Create a project

Create and change into a directory for your project. Then call operator-sdk init with the Go plugin to initialize the project.

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

Create an API

Create a simple Memcached API:

  1. operator-sdk create api --group cache --version v1 --kind Memcached --resource=true --controller=true

Configuring your test environment

Setup the envtest binaries and environment for your project. Update your test Makefile target to the following:

  1. # Run tests
  2. ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
  3. test: generate fmt vet manifests
  4. mkdir -p ${ENVTEST_ASSETS_DIR}
  5. test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
  6. source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out

Build and push the operator image

Use the built-in Makefile targets to build and push your operator. Make sure to define IMG when you call make:

  1. make docker-build docker-push IMG=<some-registry>/<project-name>:<tag>

NOTE: To allow the cluster pull the image the repository needs to be set as public or you must configure an image pull secret.

Run the operator

Install the CRD and deploy the project to the cluster. Set IMG with make deploy to use the image you just pushed:

  1. make install
  2. make deploy IMG=<some-registry>/<project-name>:<tag>

Create a sample custom resource

Create a sample CR:

  1. kubectl apply -f config/samples/cache_v1_memcached.yaml

Watch for the CR be reconciled by the operator:

  1. kubectl logs deployment.apps/memcached-operator-controller-manager -n memcached-operator-system -c manager

Clean up

Delete the CR to uninstall memcached:

  1. kubectl delete -f config/samples/cache_v1_memcached.yaml

Uninstall the operator and its CRDs:

  1. kustomize build config/default | kubectl delete -f -

Next Steps

Read the tutorial for an in-depth walkthough of building a Go operator.

Last modified August 5, 2020: doc: quickstart for Go projects (#3551) (7e029625)