How attune-io/attune works
Purpose and Runtime
Attune is a Go Kubernetes operator for safe, in-place pod CPU/memory right-sizing. It observes workload usage, computes bounded recommendations, and optionally applies them through Kubernetes’ Pod /resize subresource, with canary and automatic-revert controls. The manager process is entered at cmd/manager/main.go; a separate kubectl attune plugin is entered at cmd/kubectl-attune/main.go.
Kubernetes API and Installation Boundary
The operator exposes AttunePolicy, cluster defaults, and namespace defaults CRDs. Policy/default types, conditions, and runtime defaulting are defined in api/v1alpha1/attunepolicy_types.go, api/v1alpha1/attunedefaults_types.go, and api/v1alpha1/defaults.go. Generated CRDs are shipped through Helm and Kustomize/OLM artifacts: charts/attune/crds/, config/crd/bases/, and config/olm/template/.
Reconciliation Orchestrator
The central reconcile loop resolves effective defaults, finds targeted workloads and pods, collects metrics, produces recommendations, selects an update strategy, records status/history, and schedules future work. Its architectural hub is internal/controller/attunepolicy_controller.go, with workload adaptation in internal/controller/workload.go and internal/controller/workload_adapters.go. It handles policy modes including Observe, Recommend, OneShot, Canary, and Auto, while coordinating HPA/VPA/policy conflict handling.
Metrics Acquisition and Recommendation Pipeline
Metrics adapters isolate external telemetry from decision logic. internal/metrics/collector.go and internal/metrics/query_builder.go query Prometheus-compatible backends; alternate adapters cover CloudWatch, Datadog, and VPA recommendations in internal/metrics/cloudwatch.go, internal/metrics/datadog.go, and internal/metrics/vpa.go. Usage samples flow into the composable recommendation chain—percentiles/profile, margin, confidence, bounds, and change filtering—implemented in internal/recommendation/chain.go, internal/recommendation/estimator.go, and internal/recommendation/bounds.go.
Resize Execution and Safety Loop
Recommendations become pod-level resize operations through internal/resize/engine.go and controller integration in internal/controller/resize.go. The default actuation boundary is the Kubernetes API /resize subresource; optional recreate/eviction behavior supports infeasible in-place requests. Before and after changes, safety logic applies capacity, quota, schedule, scaling-budget, HPA/VPA, and directional-change controls in internal/controller/safety.go, internal/controller/hpa.go, and internal/conflict/detector.go. internal/safety/monitor.go watches OOMs, throttling, restart/ready signals, and guardrails to trigger revert/cooldown.
Admission, Defaults, and Initial Sizing
Admission webhooks validate policies/defaults and can mutate new Pods with established recommendations. This separates configuration-time feedback and creation-time sizing from reconcile-time resizing: internal/webhook/validation.go, internal/webhook/defaulting.go, and internal/webhook/pod_mutating.go. Webhook TLS, Service, and registration are deployed from charts/attune/templates/webhooks.yaml and charts/attune/templates/certificate.yaml.
Outputs and Operational Integrations
The operator publishes CRD status, Kubernetes events, Prometheus-format operator metrics, savings, and fleet reports. Core exposure paths are internal/operatormetrics/metrics.go, internal/controller/savings.go, and internal/fleetreport/exporter.go. GitOps export/drift handling is isolated in internal/gitops/client.go and internal/controller/gitops_pr.go, enabling ConfigMap or PR-oriented workflows instead of direct mutation. The CLI reads cluster state and presents recommendations, savings, diffs, and multi-cluster views via cmd/kubectl-attune/diff.go and cmd/kubectl-attune/multicluster.go.
Main entry points: attunepolicy_controller.go Policy reconciler · attunepolicy_types.go Policy and defaults API · validation.go Admission webhooks · savings.go Status, events, savings
Components 17
Kubernetes API & install
- Policy and defaults API · Policies, defaults, conditions, and runtime defaulting.
api/v1alpha1/attunepolicy_types.go - CRD delivery
- Admission webhooks · Validates configuration and sizes new Pods from recommendations.
internal/webhook/validation.go - Webhook deployment
charts/attune/templates/webhooks.yaml
Operator control plane
- Manager process
cmd/manager/main.go - Policy reconciler · Coordinates modes, defaults, status, history, and rescheduling.
internal/controller/attunepolicy_controller.go - Workload adapters · Finds targeted workloads and Pods.
internal/controller/workload_adapters.go - Resize engine · Turns recommendations into pod-level resize operations.
internal/resize/engine.go - Kubernetes Pod /resize · Default in-place actuation boundary; recreate or eviction is optional.
Metrics & decisions
- Telemetry collectors · Prometheus-compatible, CloudWatch, Datadog, and VPA inputs.
internal/metrics/collector.go - Recommendation chain · Applies estimation, confidence, margins, bounds, and change filtering.
internal/recommendation/chain.go
Safety & integrations
- Safety and conflicts · Enforces capacity, quota, budget, HPA/VPA, and directional constraints.
internal/controller/safety.go - Post-change monitor · Detects OOMs, throttling, restarts, readiness failures, and triggers revert/cooldown.
internal/safety/monitor.go - Status, events, savings · Publishes CRD status, Kubernetes events, and savings metrics.
internal/controller/savings.go - Fleet reports
internal/fleetreport/exporter.go - GitOps export and drift · Supports ConfigMap and PR-oriented workflows instead of direct mutation.
internal/gitops/client.go - kubectl attune · Presents recommendations, savings, diffs, and multi-cluster views.
cmd/kubectl-attune/main.go
Connections 19
- CRD delivery → Policy and defaults API: ships CRDs
- Webhook deployment → Admission webhooks: registers
- Policy and defaults API → Policy reconciler: watches policies/defaults
- Manager process → Policy reconciler: runs
- Manager process → Admission webhooks: serves
- Admission webhooks → Policy and defaults API: validates/defaults
- Policy reconciler → Workload adapters: targets
- Policy reconciler → Telemetry collectors: collects usage
- Telemetry collectors → Recommendation chain: usage samples
- Recommendation chain → Safety and conflicts: proposed changes
- Safety and conflicts → Resize engine: approved changes
- Resize engine → Kubernetes Pod /resize: calls
- Kubernetes Pod /resize → Post-change monitor: observed after change
- Post-change monitor → Policy reconciler: revert or cooldown
- Policy reconciler → Status, events, savings: records
- Status, events, savings → Fleet reports: feeds
- Policy reconciler → GitOps export and drift: exports/drift workflow
- kubectl attune → Policy and defaults API: reads cluster state
- kubectl attune → Status, events, savings: shows recommendations and savings