Kubebuilder — SDK for Building Kubernetes APIs and Operators
Kubebuilder is a framework for building Kubernetes APIs using Custom Resource Definitions. It scaffolds an idiomatic controller-runtime project, manages code generation, and gives you the tooling to publish a production-grade operator.
What it is
Kubebuilder is the official SIG-API-Machinery SDK for authoring Kubernetes operators in Go. It wraps controller-runtime, controller-gen, and the Kubernetes client libraries into an opinionated project layout. Instead of writing boilerplate for client-gen, informers, and RBAC wiring, you define your API types and let Kubebuilder generate the rest.
It is aimed at platform engineers and SREs who need to extend Kubernetes with custom resources -- database provisioners, certificate managers, or any domain-specific controller.
How it saves time or tokens
Writing a Kubernetes operator from scratch involves hundreds of lines of scaffolding: Makefile, Dockerfile, kustomize configs, deepcopy methods, RBAC manifests, and webhook boilerplate. Kubebuilder generates all of this with two commands. The built-in envtest framework lets you run controllers against a real API server locally without deploying to a cluster, shortening the development cycle from minutes to seconds.
How to use
- Install Kubebuilder and bootstrap a new project.
curl -L -o kubebuilder https://go.kubebuilder.io/dl/latest/$(go env GOOS)/$(go env GOARCH)
chmod +x kubebuilder && sudo mv kubebuilder /usr/local/bin/
mkdir guestbook && cd guestbook
kubebuilder init --domain example.com --repo example.com/guestbook
- Create an API with a new Kind and generate the controller skeleton.
kubebuilder create api --group webapp --version v1 --kind Guestbook
- Generate manifests and run the controller locally.
make manifests generate
make install run
Example
A minimal reconciler that logs every event:
func (r *GuestbookReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx)
var guestbook webappv1.Guestbook
if err := r.Get(ctx, req.NamespacedName, &guestbook); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
log.Info('reconciling', 'name', guestbook.Name)
return ctrl.Result{}, nil
}
Related on TokRepo
- DevOps tools -- More tools for Kubernetes and infrastructure automation.
- Automation tools -- Workflows that reduce manual operations in cloud-native stacks.
Common pitfalls
- Forgetting to run
make manifestsafter changing API types causes CRD drift. Always regenerate before deploying. - The envtest binary must match your Kubernetes version. Pin it in the Makefile to avoid test failures on CI.
- Reconcilers should be idempotent. Avoid side effects that break when the controller re-enters the same reconcile loop after a transient error.
Frequently Asked Questions
Operator SDK from Red Hat wraps Kubebuilder and adds Helm-based and Ansible-based operator support on top. If you are writing operators in Go, both tools produce nearly identical scaffolding. Kubebuilder is the upstream project; Operator SDK is a superset.
Yes. Running `kubebuilder create webhook` scaffolds admission and conversion webhooks. It generates the webhook server, certificate injection, and kustomize patches needed for deployment.
Kubebuilder is Go-only. For other languages, consider kopf (Python), java-operator-sdk (Java), or kube-rs (Rust). These projects serve the same purpose but do not share Kubebuilder's scaffolding.
Kubebuilder includes envtest, which spins up a lightweight API server and etcd in-process. Run `make test` to execute controller tests against this local API server without needing a full Kubernetes cluster.
Kubebuilder tracks the latest stable Kubernetes releases. Each Kubebuilder release documents its supported Kubernetes version range. Check the compatibility matrix in the project documentation before upgrading.
Citations (3)
- Kubebuilder GitHub— Kubebuilder is the official SIG-API-Machinery SDK for Kubernetes operators
- Kubebuilder Book— Wraps controller-runtime and controller-gen for Go-based operators
- Kubernetes CRD Docs— Custom Resource Definitions extend the Kubernetes API
Related on TokRepo
Discussion
Related Assets
Heimdall — Application Dashboard for Your Server
Heimdall is an elegant self-hosted application dashboard that organizes all your web services and apps into a single, customizable start page with enhanced tile features.
Healthchecks — Cron Job Monitoring with Smart Alerts
Healthchecks is a self-hosted cron job and scheduled task monitor that alerts you when your periodic jobs fail to run on time.
Shiori — Simple Self-Hosted Bookmark Manager
Shiori is a lightweight self-hosted bookmark manager written in Go with full-text search, archiving, and a clean web interface for organizing your saved links.