modulepackage
0.0.0-20240814021034-c6118327a723
Repository: https://github.com/noonme/demo-operator.git
Documentation: pkg.go.dev
# README
Create a kubebuilder project, which requires an empty folder
kubebuilder init --domain opsblogs.cn
Check project layout
cat PROJECT
domain: opsblogs.cn
layout:
- go.kubebuilder.io/v3
projectName: demo-operator
repo: github.com/noonme/demo-operator
version: "3"
Create API, create resource[Y], create controller[Y]
kubebuilder create api --group apps --version v1beta1 --kind MyDaemonset
Open project with IDE and edit api/v1alpha1/simplestatefulset_types.go
// MyDaemonsetSpec defines the desired state of MyDaemonset
type MyDaemonsetSpec 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 MyDaemonset. Edit mydaemonset_types.go to remove/update
Image string `json:"image,omitempty"`
}
// MyDaemonsetStatus defines the observed state of MyDaemonset
type MyDaemonsetStatus struct {
AvaiableReplicas int `json:"avaiableReplicas,omitempty"`
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
}
Check Makefile
Build targets:
### create code skeletion
manifests: generate crd
generate: generate api functions, like deepCopy
### generate crd and install
run: Run a controller from your host.
install: Install CRDs into the K8s cluster specified in ~/.kube/config.
### docker build and deploy
docker-build: Build docker image with the manager.
docker-push: Push docker image with the manager.
deploy: Deploy controller to the K8s cluster specified in ~/.kube/config.
Edit controllers/mydaemonset_controller.go
, add permissions to the controller
//+kubebuilder:rbac:groups=apps.cncamp.io,resources=mydaemonsets/finalizers,verbs=update
// Add the following
//+kubebuilder:rbac:groups=core,resources=nodes,verbs=get;list;watch
//+kubebuilder:rbac:groups=core,resources=pods,verbs=get;list;watch;create;update;patch;delete
Generate crd
make manifests
Enable webhooks
Install cert-manager
kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v1.6.1/cert-manager.yaml
Create webhooks
kubebuilder create webhook --group apps --version v1beta1 --kind MyDaemonset --defaulting --programmatic-validation
Change code
Enable webhook in config/default/kustomization.yaml
Redeploy
Build & install
make build
make docker-build
make docker-push
kubectl create -f demo-operator/config/samples/roleAdd/*
make deploy