Overview
In this demo, we will build a GO-based Operator called Cloner. The project name is openinfra-summit. kind is kind: Cloner
Goal
Create a simple Go-based Operator where the custom resource for the application is Kind: Cloner. Cloner is a simple nignx based application.
Overview
Create a project called
openinfra-summitusing theoperator-sdkcommand line.The main program for the operator is
cmd/manager/main.gothat initializes and runs the Manager. The Manager will automatically register the scheme for all CR defined underpkg/apis/...and run all controllers underpkg/controller/...Add a new CRD API called Cloner, with APIVersion
kubedge.cloud.com/v1alpha1andKind: Clonerusing theoperator-sdkcommand line.Update
pkg/apis/kubedge/v1alpha1/cloner_types.goas required with customSpecsandStatusto deploy our applicationCloner. After updatingcloner_types.gorunoperator-sdk generate k8sandoperator-sdk generate openapito update the generated code for that resource type.Add a new Controller for our project
openinfra-summitwhich watches and reconciles theClonerresource, this is done using the operator-sdk command line. This will scaffold a new Controller implementation underpkg/controller/cloner/.Register our new CRD with Kubernetes APIServer.
Setup RBAC and deploy
openinfra-summit.Deploy our new CR or application called Cloner.
Getting Started
Login to server with username summit and password as summit.
ssh summit@IPADRESS
Naviagte to /home/summit/src
cd /home/summit/src
Steps to Execute
Step1 implements,
- Executes
operator-sdk new openinfra-summitcommand to create a new Project calledopeninfra-summit. Executes
go mod tidyfrom the folder theopeninfra-summitfolder to install all dependencies required forGO../operator.sh step1
- Executes
Step2 implements,
- Executes
operator-sdk add api --api-version=kubedge.cloud.com/v1alpha1 --kind=Clonerto add a new Custom Resource Definition API called Cloner with APIVersion kubedge.cloud.com/v1alpha1 and Kind Cloner. - Modifies
pkg/apis/kubedge/v1alpha1/cloner_types.goto define new fields forClonerspecandstatus. - Executes
operator-sdk generate k8safter modifying thecloner_types.gofile to update the generated code for that resource type. Executes
operator-sdk generate openapito automatically generate the OpenAPI validations../operator.sh step2
- Executes
Step3 implements,
- Executes
operator-sdk add controller --api-version=kubedge.cloud.com/v1alpha1 --kind=Clonerto add a new Controller to the project that will watch and reconcile theClonerresource. - Execute
kubectl create -f deploy/crds/kubedge_v1alpha1_cloner_crd.yamlto register the CRD with Kubernetes apiserver. Update
deploy/operator.yamlwith openinfra-summit image../operator.sh step3
- Executes
Step4 implements,
- Creates Service Account by executing
kubectl create -f deploy/service_account.yaml. - Creates Role by executing
kubectl create -f deploy/role.yaml. - Creates RoleBinding Account by executing
kubectl create -f deploy/role_binding.yaml. Deploy openinfra-summit Controller
kubectl create -f deploy/operator.yaml../operator.sh step4
- Creates Service Account by executing
Verify that the openinfra-summit Deployment is up and running:
kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
openinfra-summit 1/1 1 1 29m
Step5 implements,
- Update the CR to match with the latest template Spec.
Create the Cloner CR that was generated at
kubectl apply -f deploy/crds/kubedge_v1alpha1_cloner_cr.yaml./operator.sh step5
Check if the pods and CR status to confirm the status is updated with the Cloner pod names
kubectl get pods
NAME READY STATUS RESTARTS AGE
summit-cloner-pod 1/1 Running 0 7s
openinfra-summit-5f5d76564d-f45tx 1/1 Running 0 25s
Check our newly created operator called Cloner
kubectl get cloner
NAME AGE
summit-cloner 10s

Share this post
Twitter
Google+
Facebook
Reddit
LinkedIn
StumbleUpon
Pinterest
Email