Quickstart for Go-based Operators
A simple set of instructions to set up and run a 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
- Go through the installation guide.
- User authorized with
cluster-admin
permissions.
Steps
- Create a project directory for your project and initialize the project:
mkdir memcached-operator
cd memcached-operator
operator-sdk init --domain example.com --repo github.com/example/memcached-operator
- Create a simple Memcached API:
operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller
- Use the built-in Makefile targets to build and push your operator. Make sure to define
IMG
when you callmake
:
export USERNAME=<quay-namespace>
export OPERATOR_IMG="quay.io/$USERNAME/memcached-operator:v0.0.1"
make docker-build docker-push IMG=$OPERATOR_IMG
Note: If using an OS which does not point sh
to the bash
shell (Ubuntu for example) then you should add the following line to the Makefile
:
SHELL := /bin/bash
This will fix potential issues when the docker-build
target runs the controller test suite. Issues maybe similar to following error: failed to start the controlplane. retried 5 times: fork/exec /usr/local/kubebuilder/bin/etcd: no such file or directory occurred
OLM deployment
- Install OLM:
operator-sdk olm install
- Bundle your operator and push the bundle image:
make bundle IMG=$OPERATOR_IMG
# Note the "-bundle" component in the image name below.
export BUNDLE_IMG="quay.io/$USERNAME/memcached-operator-bundle:v0.0.1"
make bundle-build BUNDLE_IMG=$BUNDLE_IMG
make docker-push IMG=$BUNDLE_IMG
- Run your bundle:
operator-sdk run bundle $BUNDLE_IMG
- Create a sample Memcached custom resource:
$ kubectl apply -f config/samples/cache_v1alpha1_memcached.yaml
memcached.cache.example.com/memcached-sample created
- Uninstall the operator:
operator-sdk cleanup memcached-operator
Direct deployment
- Deploy your operator:
make deploy IMG=$OPERATOR_IMG
- Create a sample Memcached custom resource:
$ kubectl apply -f config/samples/cache_v1alpha1_memcached.yaml
memcached.cache.example.com/memcached-sample created
- Uninstall the operator:
make undeploy
Next Steps
Read the full tutorial for an in-depth walkthough of building a Go operator.
Last modified January 20, 2021: Updates to the tutorial docs (#4395) (160cd9f6)