This example demonstrates the usage of the service primitive within the operator component framework. It shows how to
manage a Kubernetes Service as a component of a larger application, utilising features like:
Base Construction: Initializing a Service with basic metadata, selector, and ports.
Feature Mutations: Applying version-gated or conditional changes (additional ports, labels) using the Mutator.
Field Flavors: Preserving annotations that might be managed by external tools (e.g., cloud load balancer
controllers).
Operational Status: Tracking whether the Service is operational (relevant for LoadBalancer types).
Suspension: Demonstrating that, by default, the Service remains present when the component is suspended
(DeleteOnSuspend=false), and how to opt into deletion if desired.
Data Extraction: Harvesting information (ClusterIP, ports) from the reconciled resource.
Directory Structure
app/: Defines the controller that uses the component framework. The ExampleApp CRD is shared from
examples/shared/app.
features/: Contains modular feature definitions:
mutations.go: version labelling and conditional metrics port.
resources/: Contains the central NewServiceResource factory that assembles all features using service.Builder.
main.go: A standalone entry point that demonstrates multiple reconciliation cycles with a fake client.
Running the Example
go run examples/service-primitive/main.go
This will:
Initialize a fake Kubernetes client.
Create an ExampleApp owner object.
Reconcile through four spec variations, printing the Service ports after each cycle.