# attune-io/attune architecture

> Architecture diagram of the GitHub repository attune-io/attune, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/attune-io/attune
- Repository: https://github.com/attune-io/attune

## Overview

#### 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: [`internal/controller/attunepolicy_controller.go`](https://github.com/attune-io/attune/blob/main/internal/controller/attunepolicy_controller.go) (Policy reconciler), [`api/v1alpha1/attunepolicy_types.go`](https://github.com/attune-io/attune/blob/main/api/v1alpha1/attunepolicy_types.go) (Policy and defaults API), [`internal/webhook/validation.go`](https://github.com/attune-io/attune/blob/main/internal/webhook/validation.go) (Admission webhooks), [`internal/controller/savings.go`](https://github.com/attune-io/attune/blob/main/internal/controller/savings.go) (Status, events, savings)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Kubernetes API &amp; install"]
  node_api_types["Policy and defaults API<br/>CRD types"]
  node_crd_delivery["CRD delivery<br/>Helm/Kustomize/OLM"]
  node_webhooks["Admission webhooks<br/>validation/defaulting<br/>[validation.go]"]
  node_webhook_deploy["Webhook deployment<br/>Helm manifests<br/>[webhooks.yaml]"]
end

subgraph group_control["Operator control plane"]
  node_manager["Manager process<br/>Go entrypoint<br/>[main.go]"]
  node_reconciler["Policy reconciler<br/>controller"]
  node_workloads["Workload adapters<br/>controller adaptation"]
  node_resize_engine["Resize engine<br/>actuation<br/>[engine.go]"]
  node_kube_resize{{"Kubernetes Pod /resize<br/>Kubernetes API"}}
end

subgraph group_decision["Metrics &amp; decisions"]
  node_metrics["Telemetry collectors<br/>metrics adapters<br/>[collector.go]"]
  node_recommendations["Recommendation chain<br/>sizing pipeline<br/>[chain.go]"]
end

subgraph group_operations["Safety &amp; integrations"]
  node_safety_checks["Safety and conflicts<br/>admission controls<br/>[safety.go]"]
  node_safety_monitor["Post-change monitor<br/>guardrail monitor<br/>[monitor.go]"]
  node_status_outputs["Status, events, savings<br/>operator outputs<br/>[savings.go]"]
  node_fleet_reports["Fleet reports<br/>report export<br/>[exporter.go]"]
  node_gitops["GitOps export and drift<br/>integration<br/>[client.go]"]
  node_cli["kubectl attune<br/>CLI entrypoint<br/>[main.go]"]
end

node_crd_delivery -->|"ships CRDs"| node_api_types
node_webhook_deploy -->|"registers"| node_webhooks
node_api_types -->|"watches policies/defaults"| node_reconciler
node_manager -->|"runs"| node_reconciler
node_manager -->|"serves"| node_webhooks
node_webhooks -->|"validates/defaults"| node_api_types
node_reconciler -->|"targets"| node_workloads
node_reconciler -->|"collects usage"| node_metrics
node_metrics -->|"usage samples"| node_recommendations
node_recommendations -->|"proposed changes"| node_safety_checks
node_safety_checks -->|"approved changes"| node_resize_engine
node_resize_engine -->|"calls"| node_kube_resize
node_kube_resize -->|"observed after change"| node_safety_monitor
node_safety_monitor -->|"revert or cooldown"| node_reconciler
node_reconciler -->|"records"| node_status_outputs
node_status_outputs -->|"feeds"| node_fleet_reports
node_reconciler -.->|"exports/drift workflow"| node_gitops
node_cli -->|"reads cluster state"| node_api_types
node_cli -->|"shows recommendations and savings"| node_status_outputs

click node_manager "https://github.com/attune-io/attune/blob/main/cmd/manager/main.go"
click node_api_types "https://github.com/attune-io/attune/blob/main/api/v1alpha1/attunepolicy_types.go"
click node_webhooks "https://github.com/attune-io/attune/blob/main/internal/webhook/validation.go"
click node_webhook_deploy "https://github.com/attune-io/attune/blob/main/charts/attune/templates/webhooks.yaml"
click node_reconciler "https://github.com/attune-io/attune/blob/main/internal/controller/attunepolicy_controller.go"
click node_workloads "https://github.com/attune-io/attune/blob/main/internal/controller/workload_adapters.go"
click node_metrics "https://github.com/attune-io/attune/blob/main/internal/metrics/collector.go"
click node_recommendations "https://github.com/attune-io/attune/blob/main/internal/recommendation/chain.go"
click node_safety_checks "https://github.com/attune-io/attune/blob/main/internal/controller/safety.go"
click node_resize_engine "https://github.com/attune-io/attune/blob/main/internal/resize/engine.go"
click node_safety_monitor "https://github.com/attune-io/attune/blob/main/internal/safety/monitor.go"
click node_status_outputs "https://github.com/attune-io/attune/blob/main/internal/controller/savings.go"
click node_fleet_reports "https://github.com/attune-io/attune/blob/main/internal/fleetreport/exporter.go"
click node_gitops "https://github.com/attune-io/attune/blob/main/internal/gitops/client.go"
click node_cli "https://github.com/attune-io/attune/blob/main/cmd/kubectl-attune/main.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_api_types,node_crd_delivery,node_webhooks,node_webhook_deploy toneBlue
class node_manager,node_reconciler,node_workloads,node_resize_engine,node_kube_resize toneAmber
class node_metrics,node_recommendations toneMint
class node_safety_checks,node_safety_monitor,node_status_outputs,node_fleet_reports,node_gitops,node_cli toneRose
```

## Components

### Kubernetes API & install

- [Policy and defaults API](https://github.com/attune-io/attune/blob/main/api/v1alpha1/attunepolicy_types.go): CRD types · Policies, defaults, conditions, and runtime defaulting. · `api/v1alpha1/attunepolicy_types.go`
- **CRD delivery**: Helm/Kustomize/OLM
- [Admission webhooks](https://github.com/attune-io/attune/blob/main/internal/webhook/validation.go): validation/defaulting · Validates configuration and sizes new Pods from recommendations. · `internal/webhook/validation.go`
- [Webhook deployment](https://github.com/attune-io/attune/blob/main/charts/attune/templates/webhooks.yaml): Helm manifests · `charts/attune/templates/webhooks.yaml`

### Operator control plane

- [Manager process](https://github.com/attune-io/attune/blob/main/cmd/manager/main.go): Go entrypoint · `cmd/manager/main.go`
- [Policy reconciler](https://github.com/attune-io/attune/blob/main/internal/controller/attunepolicy_controller.go): controller · Coordinates modes, defaults, status, history, and rescheduling. · `internal/controller/attunepolicy_controller.go`
- [Workload adapters](https://github.com/attune-io/attune/blob/main/internal/controller/workload_adapters.go): controller adaptation · Finds targeted workloads and Pods. · `internal/controller/workload_adapters.go`
- [Resize engine](https://github.com/attune-io/attune/blob/main/internal/resize/engine.go): actuation · Turns recommendations into pod-level resize operations. · `internal/resize/engine.go`
- **Kubernetes Pod /resize**: Kubernetes API · Default in-place actuation boundary; recreate or eviction is optional.

### Metrics & decisions

- [Telemetry collectors](https://github.com/attune-io/attune/blob/main/internal/metrics/collector.go): metrics adapters · Prometheus-compatible, CloudWatch, Datadog, and VPA inputs. · `internal/metrics/collector.go`
- [Recommendation chain](https://github.com/attune-io/attune/blob/main/internal/recommendation/chain.go): sizing pipeline · Applies estimation, confidence, margins, bounds, and change filtering. · `internal/recommendation/chain.go`

### Safety & integrations

- [Safety and conflicts](https://github.com/attune-io/attune/blob/main/internal/controller/safety.go): admission controls · Enforces capacity, quota, budget, HPA/VPA, and directional constraints. · `internal/controller/safety.go`
- [Post-change monitor](https://github.com/attune-io/attune/blob/main/internal/safety/monitor.go): guardrail monitor · Detects OOMs, throttling, restarts, readiness failures, and triggers revert/cooldown. · `internal/safety/monitor.go`
- [Status, events, savings](https://github.com/attune-io/attune/blob/main/internal/controller/savings.go): operator outputs · Publishes CRD status, Kubernetes events, and savings metrics. · `internal/controller/savings.go`
- [Fleet reports](https://github.com/attune-io/attune/blob/main/internal/fleetreport/exporter.go): report export · `internal/fleetreport/exporter.go`
- [GitOps export and drift](https://github.com/attune-io/attune/blob/main/internal/gitops/client.go): integration · Supports ConfigMap and PR-oriented workflows instead of direct mutation. · `internal/gitops/client.go`
- [kubectl attune](https://github.com/attune-io/attune/blob/main/cmd/kubectl-attune/main.go): CLI entrypoint · Presents recommendations, savings, diffs, and multi-cluster views. · `cmd/kubectl-attune/main.go`

## Connections

- 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
