Smooth K8s Operator is a custom Kubernetes operator written in Go that manages a custom resource definition (CRD) called StaticPage. This operator automatically creates and updates a Deployment and a ConfigMap based on the configuration specified in your StaticPage custom resource. It’s designed to provide a simple example for managing content pages with dynamic configuration changes.
This operator allows you to define a StaticPage CR that contains:
- Contents: The HTML content to be served.
- Image: The container image to use (e.g., nginx:latest).
- Replicas: The number of pod replicas to run.
Once you create a StaticPage resource, the operator creates or updates the following Kubernetes resources in the same namespace:
- A Deployment for running the specified container.
- A ConfigMap that holds the HTML content.
The operator monitors changes to the custom resource and performs reconciliation to ensure that the live state of the cluster reflects the desired specification.
- Custom Resource Definition (CRD): Define a
StaticPagecustom resource. - Reconciliation Loop: Automatically creates, updates, or deletes related resources (Deployment and ConfigMap) based on changes to the StaticPage resource.
- Logging: Uses structured logging (via zap) for better troubleshooting.
- RBAC Integration: Provides YAML manifest files for required RBAC permissions.
- Docker Integration: Multi-stage Dockerfile builds a minimal runtime image for the operator.
├── api
│ └── v1
│ ├── deepcopy.go # Custom deep copy implementations
│ ├── register.go # API registration to the runtime scheme
│ └── staticpage.go # CRD type definitions for StaticPage
├── cmd
│ └── controller
│ └── main.go # The operator's main entry point
├── yaml
│ ├── crd.yaml # CRD definition for StaticPage
│ ├── deploy-controller.yaml # Deployment, ServiceAccount, and RBAC for the operator
│ └── example.yaml # An example StaticPage custom resource
└── Dockerfile # Dockerfile for building the operator imageApply the provided YAML manifests in the following order:
- CRD – Deploy the CustomResourceDefinition for StaticPage:
kubectl apply -f yaml/crd.yaml
- Operator Deployment and RBAC – Deploy the operator and associated RBAC settings:
kubectl apply -f yaml/deploy-controller.yaml
- Example Custom Resource – Create an example StaticPage custom resource:
kubectl apply -f yaml/example.yaml
You can run the operator locally from VS Code. Ensure that your kubeconfig is properly configured. To start the operator from your local machine:
go run cmd/controller/main.goCheck the logs to verify that the operator is correctly reconciling the StaticPage resource.
-
RBAC and API Groups: Verify that your RBAC definitions in
deploy-controller.yamluse the correct API group matching your CRD (kubernetes.chinzzii.com). -
Future Enhancements: You might consider adding support for additional fields in the CRD, more advanced reconciliation logic, custom events, or metrics.
This project was inspired by Edward Viaenek ’s excellent Kubernetes Operator tutorial. Huge thanks for the clear and insightful content!