Skip to main content

Kubebuilder

Kubebuilder

Kubernetes API development framework

Kubebuilder is the framework used to build the Control Plane's operators and custom resources.

Development framework

Kubebuilder provides the scaffolding and tooling used to develop the Control Plane's operators and custom resources.

Overviewโ€‹

Kubebuilder is a framework for building Kubernetes APIs using CRDs, controllers, and webhooks. It's designed to simplify the development of Kubernetes operators and custom resources by providing code generation, scaffolding, and testing capabilities.

Kubebuilder Logo

Why Kubebuilder?โ€‹

๐Ÿ—๏ธ Standardized Structure

Provides a consistent project structure that follows Kubernetes community best practices.

โšก Rapid Development

Accelerates development with automatic generation of boilerplate code and manifests.

๐Ÿงฉ Controller Runtime

Built on the battle-tested controller-runtime library used by Kubernetes core components.

๐Ÿ”’ Security by Default

Automatically generates RBAC rules based on controller access patterns.

๐Ÿงช Testing Framework

Includes a testing framework that simulates the Kubernetes API server for unit and integration tests.

๐Ÿš€ Production-Ready

Used by major Kubernetes projects and enterprises for building production operators.

๐Ÿ—๏ธ Scaffolding

Generate code and manifests for new API types and controllers.

๐Ÿงช Testing

Framework for controller and webhook testing with envtest.

๐Ÿ”’ RBAC

Automatic generation of role-based access control manifests.

๐Ÿ” Webhooks

Infrastructure for validation and defaulting webhooks.

๐Ÿ“– Documentation

Marker-based API documentation generation.

๐Ÿ”ง CLI Tools

Command-line tools for project management and scaffolding.

Project Structureโ€‹

The Control Plane follows the standard Kubebuilder project structure:

โ”œโ”€โ”€ api/
โ”‚ โ””โ”€โ”€ v1/
โ”‚ โ”œโ”€โ”€ rover_types.go
โ”‚ โ”œโ”€โ”€ apispecification_types.go
โ”‚ โ”œโ”€โ”€ groupversion_info.go
โ”‚ โ””โ”€โ”€ zz_generated.deepcopy.go
โ”œโ”€โ”€ controllers/
โ”‚ โ”œโ”€โ”€ rover_controller.go
โ”‚ โ”œโ”€โ”€ apispecification_controller.go
โ”‚ โ””โ”€โ”€ suite_test.go
โ”œโ”€โ”€ config/
โ”‚ โ”œโ”€โ”€ crd/
โ”‚ โ”‚ โ””โ”€โ”€ bases/
โ”‚ โ”‚ โ””โ”€โ”€ rover.cp.ei.telekom.de_rovers.yaml
โ”‚ โ”œโ”€โ”€ rbac/
โ”‚ โ”‚ โ”œโ”€โ”€ role.yaml
โ”‚ โ”‚ โ””โ”€โ”€ role_binding.yaml
โ”‚ โ”œโ”€โ”€ manager/
โ”‚ โ”‚ โ””โ”€โ”€ manager.yaml
โ”‚ โ””โ”€โ”€ samples/
โ”‚ โ””โ”€โ”€ rover_v1_rover.yaml
โ””โ”€โ”€ main.go
Standardized organization

The Kubebuilder project structure ensures consistency across all Control Plane operators, making it easier to navigate, understand, and maintain the codebase.

Working with Kubebuilderโ€‹

Creating a New APIโ€‹

The Control Plane uses Kubebuilder to scaffold new APIs and controllers:

# Create a new API with group 'gateway', version 'v1', kind 'Route'
kubebuilder create api --group gateway --version v1 --kind Route

# Create a new webhook for an existing API
kubebuilder create webhook --group gateway --version v1 --kind Route --defaulting --programmatic-validation

Generating CRDsโ€‹

Kubebuilder automatically generates CRDs from Go types with markers:

# Generate CRDs from API definitions
make manifests

# Install CRDs into a cluster
make install

Running Controllers Locallyโ€‹

The Control Plane developers can run controllers locally for testing:

# Run controller against a remote cluster
make run

# Run controller with specific configuration
KUBECONFIG=/path/to/kubeconfig ENABLE_WEBHOOKS=false make run

Testing with Kubebuilderโ€‹

Kubebuilder Testing

Kubebuilder provides the envtest package, which sets up a temporary API server for testing controllers without requiring a real Kubernetes cluster.

Best Practicesโ€‹

Control Plane patterns

The Control Plane follows these best practices for Kubebuilder-based development.

API Design

  • Follow Kubernetes API conventions
  • Use CRD validation for all fields
  • Separate spec (desired state) from status (observed state)
  • Define clear ownership and dependency relationships

Controller Implementation

  • Make reconciliation idempotent
  • Use finalizers for cleanup operations
  • Log relevant information at appropriate levels
  • Handle errors with appropriate retry strategies

Resource Management

  • Implement owner references for garbage collection
  • Set resource requests and limits appropriately
  • Use metrics for performance monitoring
  • Implement proper error handling and reporting

Testing

  • Write comprehensive unit tests
  • Use envtest for controller integration testing
  • Test failure scenarios and edge cases
  • Verify webhooks with admission review requests

Kubernetes

Learn about the core platform extended by Kubebuilder.

Controller Runtime

Explore the library that powers Kubebuilder-based controllers.