How to Create the Skeleton of an Operator - cniackz/public GitHub Wiki
Objective:
To create the skeleton of an operator
Diagram:
Recommendation:
Use chatgpt to get the steps and explore things further in a quick way, remember chatgpt and AI is a tool to help us be better! So use it well and use it wize with love!
Steps:
- Prepare an empty folder to work with:
cd ~
rm -rf ~/tmp
cd ~
mkdir ~/tmp
cd ~/tmp
- Initialize the operator
operator-sdk init --domain example.com --repo github.com/my-org/my-operator
- Create the API:
operator-sdk create api --group apps --version v1alpha1 --kind MyApp
-
In the controller, get the app instance & print the message from the custom resource:
-
File:
subl /Users/cniackz/tmp/internal/controller/myapp_controller.go
func (r *MyAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
...
log := log.FromContext(ctx)
// Fetch the MyApp instance
var myapp appsv1alpha1.MyApp
if err := r.Get(ctx, req.NamespacedName, &myapp); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// Print the message from the Custom Resource
log.Info("Received MyResource", "message", myapp.Spec.Message)
...
- In the types file, add the message to the struct
subl /Users/cniackz/tmp/api/v1alpha1/myapp_types.go
type MyAppSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
// Foo is an example field of MyApp. Edit myapp_types.go to remove/update
Foo string `json:"foo,omitempty"`
Message string `json:"message,omitempty"`
}
- Then make and generate the files:
make generate
make manifests
- Build the image:
make docker-build IMG=radical-123
kind load docker-image docker.io/library/radical-123
createcluster
kubectl create namespace system
kubectl apply -k /Users/cniackz/tmp/config/crd
kubectl apply -k /Users/cniackz/tmp/config/rbac
kubectl apply -f /Users/cniackz/tmp/config/manager
- change deployment
subl /Users/cniackz/tmp/config/manager/manager.yaml
- Under containers add
image
andimagePullPolicy
as below:
containers:
- command:
- /manager
args:
- --leader-elect
image: docker.io/library/radical-123
imagePullPolicy: Never
k apply -f /Users/cniackz/tmp/config/manager/manager.yaml
- in manager-role cluster role do:
rules:
- verbs:
- create
apiGroups:
- ''
resources:
- events
- verbs:
- get
- create
- update
apiGroups:
- coordination.k8s.io
resources:
- leases
or apply below:
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: manager-role
rules:
- apiGroups:
- ''
resources:
- events
verbs:
- create
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- get
- create
- update
- apiGroups:
- apps.example.com
resources:
- myapps
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- apps.example.com
resources:
- myapps/finalizers
verbs:
- update
- apiGroups:
- apps.example.com
resources:
- myapps/status
verbs:
- get
- patch
- update
- apply CR
apiVersion: apps.example.com/v1alpha1
kind: MyApp
metadata:
name: myapp-sample
spec:
message: "Hello, Kubernetes Operator!"