This example demonstrates the usage of the hpa primitive within the operator component framework. It shows how to
manage a Kubernetes HorizontalPodAutoscaler as a component of a larger application, utilizing features like:
Base Construction: Initializing an HPA with a scale target ref, min/max replicas, and labels.
Feature Mutations: Applying version-gated or conditional changes (CPU metrics, memory metrics, scaling behavior)
using the Mutator.
Operational Status: Reporting HPA health based on ScalingActive and AbleToScale conditions.
Suspension (Delete): Demonstrating delete-on-suspend behavior — the HPA is removed during suspension to prevent it
from scaling the target back up.
Data Extraction: Harvesting information from the reconciled resource.
Directory Structure
app/: Defines the mock ExampleApp CRD and the controller that uses the component framework.
features/: Contains modular feature definitions:
mutations.go: CPU metric, memory metric, and scale behavior feature mutations.
resources/: Contains the central NewHPAResource factory that assembles all features using the hpa.Builder.
main.go: A standalone entry point that demonstrates a reconciliation loop using a fake client.
Running the Example
You can run this example directly using go run:
go run examples/hpa-primitive/main.go
This will:
Initialize a fake Kubernetes client.
Create an ExampleApp owner object.
Reconcile the ExampleApp components through multiple spec changes.
Print the resulting status conditions and HPA state.