This tutorial demonstrates simple operator-sdk workflow and examines Reconcile loop which will typical Operator developer deal with.
Official Operator SDK repository.
Check operator-sdk prerequisites doc before starting this tutorial.
Create minimal Operator in GOPATH
:
mkdir -p $GOPATH/src/github.com/prgcont/
cd $GOPATH/src/github.com/prgcont/
operator-sdk new workshop-operator
Add API to generated Operator(shall be executed inside workshop-operator directory):
operator-sdk add api --api-version=workshopapp.operator.prgcont.cz/v1alpha1 --kind=WorkshopApp
Check which files were added or updated.
Add Controller to generated Operator:
operator-sdk add controller --api-version=workshopapp.operator.prgcont.cz/v1alpha1 --kind=WorkshopApp
Check which files were added or updated.
Build and run operator locally:
# Create CRD
kubectl create -f ./deploy/crds/workshopapp_v1alpha1_workshopapp_crd.yaml
go build ./cmd/manager
WATCH_NAMESPACE=default ./manager
# Create instance of app
kubectl create -f ./deploy/crds/workshopapp_v1alpha1_workshopapp_cr.yaml
Tasks:
- delete the pod, observe what happened and explain it
- hint: check Reconcile method in
pkg/controller/workshopapp/workshopapp_controller.go
file.
- hint: check Reconcile method in
- Stop Operator and delete CR, observe if Pod was deleted and explain why.