Skip to content

DirectPV Operator 2

Cesar Celis Hernandez edited this page Jul 21, 2023 · 6 revisions

Objective:

To document how I created the DirectPV Operator from scratch!.

Diagrams:

image

telegram-cloud-photo-size-1-5030769656109050868-y

Documentation:

Steps:

createcluster

Create a new project

cd ~
sudo rm -rf ~/projects/memcached-operator
mkdir -p ~/projects/memcached-operator
cd ~/projects/memcached-operator
operator-sdk init --domain example.com --repo github.com/example/memcached-operator --plugins=go/v4-alpha

Create a new API and Controller

operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller

Define the API

rm /Users/cniackz/projects/memcached-operator/api/v1alpha1/memcached_types.go
cp /Users/cniackz/base-operator/operator-tuto/memcached_types.go /Users/cniackz/projects/memcached-operator/api/v1alpha1/memcached_types.go
cd /Users/cniackz/projects/memcached-operator
make generate

Generating CRD manifests

cd /Users/cniackz/projects/memcached-operator
make manifests

Implement the Controller

rm /Users/cniackz/projects/memcached-operator/internal/controller/memcached_controller.go
cp /Users/cniackz/base-operator/operator-tuto/memcached_controller.go /Users/cniackz/projects/memcached-operator/internal/controller/memcached_controller.go

Setup a Recorder

rm /Users/cniackz/projects/memcached-operator/cmd/main.go
cp /Users/cniackz/base-operator/operator-tuto/main.go /Users/cniackz/projects/memcached-operator/cmd/main.go

Specify permissions and generate RBAC manifests

cd /Users/cniackz/projects/memcached-operator
make manifests

Configure the operator’s image registry

rm /Users/cniackz/projects/memcached-operator/Dockerfile
cp /Users/cniackz/base-operator/operator-tuto/Dockerfile /Users/cniackz/projects/memcached-operator/Dockerfile
rm /Users/cniackz/projects/memcached-operator/Makefile
cp /Users/cniackz/base-operator/operator-tuto/Makefile.mk /Users/cniackz/projects/memcached-operator/Makefile
cd /Users/cniackz/projects/memcached-operator
make docker-build docker-push

Run the Operator

2. Run as a Deployment inside the cluster

  • Add MEMCACHED_IMAGE env var to the Deployment so it does not fail:
rm /Users/cniackz/projects/memcached-operator/config/manager/manager.yaml
cp /Users/cniackz/base-operator/operator-tuto/manager.yaml /Users/cniackz/projects/memcached-operator/config/manager/manager.yaml
make deploy
kubectl get deployment -n memcached-operator-system

Create a Memcached CR

cd /Users/cniackz/base-operator/operator-tuto
kubectl apply -f cache_v1alpha1_memcached.yaml
kubectl get deployment
kubectl get pods
kubectl get memcached/memcached-sample -o yaml
Clone this wiki locally