The most efficient way to write a Kubernetes Operator is to use Go to combine Kubebuilder and controller-runtime. 1. Understand the Operator pattern: define custom resources through CRD, write a controller to listen for resource changes and perform reconciliation loops to maintain the expected state. 2. Initialize the project with Kubebuilder and create APIs to automatically generate CRDs, controllers, and configuration files. 3. Define the Spec and Status structures of CRD in api/v1/myapp_types.go, and run make manifests to generate CRD YAML. 4. Implement business logic in the controller's Reconcile method, and use r.Create, r.Update and other methods to manage Kubernetes resources. 5. Define RBAC permissions through Go annotations, build images using make manifests and make docker-build, and deploy them through kubectl or make deploy. 6. Follow best practices: Ensure reconciliation of logic idempotence, handle errors reasonably and use RequeueAfter to control retry, add logs and events, and use Finalizers and Webhooks if necessary. 7. Use envtest for integration testing and fakeclient for unit testing. Ultimately, the core of Operator development is to define CRD, write harmonic logic, use controller-runtime to simplify K8s interaction, and complete testing and deployment through mature toolchains.
Writing Kubernetes Operators in Go is one of the most effective ways to extend the Kubernetes control plane for managing complex, stateful applications. An operator uses custom resources (CRs) and controllers to automate tasks that a human operator would otherwise perform—like backups, scaling, upgrades, and failover. Go is the natural choice because Kubernetes itself is written in Go, and the ecosystem (like client-go and controller-runtime) is mature and well-documented.

Here's a practical guide to help you get started and understand the key components.
1. Understand the Operator Pattern
An Operator combines a Custom Resource Definition (CRD) with a controller that watches for changes to that resource and take action to reconcile the desired state with the actual state of the cluster.

- Custom Resource (CR) : A YAML manifest defining your application's desired state (eg,
MyAppDatabase
). - Controller : A Go program that watches
MyAppDatabase
objects and manages Kubernetes resources (eg, StatefulSets, Services) to match the spec.
This is based on the informer-pattern and reconciliation loop .
2. Use Kubebuilder and controller-runtime
The easiest way to build an operator in Go is using Kubebuilder , part of the Kubernetes SIGs, which scaffolds projects using controller-runtime —a library that handles low-level details like client setup, event handling, and reconciliation.

Install tools:
# Install kubebuilder curl -L -O https://go.kubebuilder.io/dl/latest/$(go env GOOS)/$(go env GOARCH) tar -xzf kubebuilder_*_$(go env GOOS)_$(go env GOARCH).tar.gz sudo mv kubebuilder_*_$(go env GOOS)_$(go env GOARCH) /usr/local/kubebuilder export PATH=$PATH:/usr/local/kubebuilder/bin
Create a new project:
mkdir myapp-operator cd myapp-operator kubebuilder init --domain example.com --repo example.com/myapp-operator kubebuilder create api --group apps --version v1 --kind MyApp
This generates:
-
api/v1/myapp_types.go
– Define your CRD schema. -
controllers/myapp_controller.go
– Where you write reconciliation logic. -
config/
– Kustomize manifests for deploying CRD and RBAC.
3. Define Your Custom Resource
Edit api/v1/myapp_types.go
:
type MyAppSpec struct { Replicas int32 `json:"replicas"` Image string `json:"image"` Port int32 `json:"port"` } type MyAppStatus struct { ReadyReplicas int32 `json:"readyReplicas"` Conditions []metav1.Condition `json:"conditions,omitempty"` }
Run make manifests
to generate CRD YAML from Go annotations.
4. Implement the Reconciliation Logic
In controllers/myapp_controller.go
, the Reconcile
method is called whenever a MyApp resource changes.
func (r *MyAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { log := r.Log.WithValues("myapp", req.NamespacedName) var myapp MyApp if err := r.Get(ctx, req.NamespacedName, &myapp); err != nil { return ctrl.Result{}, client.IgnoreNotFound(err) } // Ensure a Deployment exists desiredDep := &appsv1.Deployment{ ObjectMeta: metav1.ObjectMeta{ Name: myapp.Name, Namespace: myapp.Namespace, }, Spec: appsv1.DeploymentSpec{ Replicas: &myapp.Spec.Replicas, Selector: &metav1.LabelSelector{ MatchLabels: map[string]string{"app": myapp.Name}, }, Template:corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ Labels: map[string]string{"app": myapp.Name}, }, Spec: corev1.PodSpec{ Containers: []corev1.Container{ { Name: "app", Image: myapp.Spec.Image, Ports: []corev1.ContainerPort{{ContainerPort: myapp.Spec.Port}}, }, }, }, }, }, } // Use controller-runtime's client to create or update if err := r.Create(ctx, desiredDep); err != nil { if !errors.IsAlreadyExists(err) { return ctrl.Result{}, err } } // Update status myapp.Status.ReadyReplicas = 0 // Update from actual deployment if err := r.Status().Update(ctx, &myapp); err != nil { return ctrl.Result{}, err } return ctrl.Result{}, nil }
Use r.Create
, r.Update
, r.Patch
, or r.Delete
to manage objects.
5. Add RBAC and Deploy
Kubebuilder uses Go annotations to generate RBAC rules:
// kubebuilder:rbac:groups=apps.example.com,resources=myapps,verbs=get;list;watch;create;update;patch;delete // kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete // kubebuilder:rbac:groups=core,resources=pods,verbs=list
Run:
Make manifests make docker-build IMG=myapp-operator:v0.0.1 kubectl apply -f config/crd/bases/apps.example.com_myapps.yaml kubectl create deployment myapp-operator --image=myapp-operator:v0.0.1
Or use make deploy IMG=myapp-operator:v0.0.1
if using the default kustomize setup.
6. Best Practices
- Idempotency : Reconcile loops may run multiple times—ensure operations are safe to repeat.
- Error handling : Return errors to request; use
ctrl.Result{RequeueAfter: time.Second}
for periodic checks. - Logging & Events : Use
r.Log
andr.Recorder.Event()
to emit Kubernetes events. - Finalizers : Use them when you need to perform cleanup before a CR is deleted.
- Webhooks : Add validation (ValidatingAdmissionWebhook) or defaults (MutatingAdmissionWebhook) via
kubebuilder create webhook
.
7. Testing
- Use
envtest
for integration tests that start etcd and kube-apiserver locally. - Write unit tests for your reconciliation logic using
fakeclient
.
Example test setup:
import ( "sigs.k8s.io/controller-runtime/pkg/envtest" ) var testEnv *envtest.Environment
Basically, building Kubernetes Operators in Go boils down to:
- Defining a CRD with
kubebuilder
- Writing a controller that reconciles desired vs. actual state
- Using
controller-runtime
to handle Kubernetes interactions - Testing with
envtest
and deploying with standard manifests
It's not trivial, but the tooling has matured a lot—Kubebuilder and controller-runtime do most of the heavy lifting. Start small, automate one thing well, and expand from there.
The above is the detailed content of Developing Kubernetes Operators in Go. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The most efficient way to write a KubernetesOperator is to use Go to combine Kubebuilder and controller-runtime. 1. Understand the Operator pattern: define custom resources through CRD, write a controller to listen for resource changes and perform reconciliation loops to maintain the expected state. 2. Use Kubebuilder to initialize the project and create APIs to automatically generate CRDs, controllers and configuration files. 3. Define the Spec and Status structure of CRD in api/v1/myapp_types.go, and run makemanifests to generate CRDYAML. 4. Reconcil in the controller

Stack allocation is suitable for small local variables with clear life cycles, and is automatically managed, with fast speed but many restrictions; heap allocation is used for data with long or uncertain life cycles, and is flexible but has a performance cost. The Go compiler automatically determines the variable allocation position through escape analysis. If the variable may escape from the current function scope, it will be allocated to the heap. Common situations that cause escape include: returning local variable pointers, assigning values to interface types, and passing in goroutines. The escape analysis results can be viewed through -gcflags="-m". When using pointers, you should pay attention to the variable life cycle to avoid unnecessary escapes.

Panic is like a program "heart attack" in Go. Recover can be used as a "first aid tool" to prevent crashes, but Recover only takes effect in the defer function. 1.recover is used to avoid service lapse, log logs, and return friendly errors. 2. It must be used in conjunction with defer and only takes effect on the same goroutine. The program does not return to the panic point after recovery. 3. It is recommended to use it at the top level or critical entrance, and do not abuse it, and give priority to using error processing. 4. The common pattern is to encapsulate safeRun functions to wrap possible panic logic. Only by mastering its usage scenarios and limitations can it play its role correctly.

In Go, selecting buffered or unbufferedchannel depends on whether synchronous communication is required. 1.Unbufferedchannel is used for strict synchronization, and sending and receiving operations are blocked by each other, suitable for scenarios such as task chains, handshakes, real-time notifications; 2. Bufferedchannel allows asynchronous processing, the sender only blocks when the channel is full, and the receiver blocks when the channel is empty, suitable for scenarios such as producer-consumer model, concurrency control, data flow buffering, etc.; 3. When choosing, it should be decided one by one based on whether the sending and receiving needs to be sent. If the task must be processed immediately, use unbuffered, and use buffered if queueing or parallel processing is allowed. master

InGo,thereisnobuilt-insettype,butasetcanbeefficientlyimplementedusingamapwithstruct{}valuestominimizememoryusage.1.Useamap[T]struct{}torepresentasetwherekeysaretheelements.2.Performaddoperationsbyassigningakeytostruct{}.3.Checkexistenceusingthecomma-

Go does not have a built-in collection type, but it can be implemented efficiently through maps. Use map[T]struct{} to store element keys, empty structures have zero memory overhead, and the implementation of addition, inspection, deletion and other operations are O(1) time complexity; in a concurrent environment, sync.RWMutex or sync.Map can be combined to ensure thread safety; in terms of performance, memory usage, hashing cost and disorder; it is recommended to encapsulate Add, Remove, Contains, Size and other methods to simulate standard collection behavior.

UselightweightrouterslikeChiforefficientHTTPhandlingwithbuilt-inmiddlewareandcontextsupport.2.Leveragegoroutinesandchannelsforconcurrency,alwaysmanagingthemwithcontext.Contexttopreventleaks.3.OptimizeservicecommunicationbyusinggRPCwithProtocolBuffers

In Go language, there are two main ways to initialize structure pointers: 1. Use the new() function to initialize zero value; 2. Use the &Struct{} syntax to initialize and assign values. new() is suitable for cases where only zero values are required, while &Struct{} is more flexible and supports specifying field values during initialization. Both create pointers, but the latter is more commonly used and readable. In addition, depending on whether the original data needs to be modified, the appropriate initialization method should be selected to match the method recipient type.
