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-summit
using theoperator-sdk
command line.The main program for the operator is
cmd/manager/main.go
that 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/v1alpha1
andKind: Cloner
using theoperator-sdk
command line.Update
pkg/apis/kubedge/v1alpha1/cloner_types.go
as required with customSpecs
andStatus
to deploy our applicationCloner
. After updatingcloner_types.go
runoperator-sdk generate k8s
andoperator-sdk generate openapi
to update the generated code for that resource type.Add a new Controller for our project
openinfra-summit
which watches and reconciles theCloner
resource, 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-summit
command to create a new Project calledopeninfra-summit
. Executes
go mod tidy
from the folder theopeninfra-summit
folder to install all dependencies required forGO
../operator.sh step1
- Executes
Step2 implements,
- Executes
operator-sdk add api --api-version=kubedge.cloud.com/v1alpha1 --kind=Cloner
to 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.go
to define new fields forCloner
spec
andstatus
. - Executes
operator-sdk generate k8s
after modifying thecloner_types.go
file to update the generated code for that resource type. Executes
operator-sdk generate openapi
to automatically generate the OpenAPI validations../operator.sh step2
- Executes
Step3 implements,
- Executes
operator-sdk add controller --api-version=kubedge.cloud.com/v1alpha1 --kind=Cloner
to add a new Controller to the project that will watch and reconcile theCloner
resource. - Execute
kubectl create -f deploy/crds/kubedge_v1alpha1_cloner_crd.yaml
to register the CRD with Kubernetes apiserver. Update
deploy/operator.yaml
with 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
Twitter
Google+
Facebook
Reddit
LinkedIn
StumbleUpon
Pinterest
Email