How castai/k8s-agent works
CAST AI Kubernetes Agent connects a cluster to CAST AI for automation and cost optimization. The principal workflow starts with agent configuration and provider/cluster registration, then watches Kubernetes resources, normalizes events into deltas, and uploads them to CAST AI. Separate capabilities include provider-specific cluster discovery, leader election, monitoring, diagnostic dumping, health and metrics endpoints, and remote log export. Several subsystem links are not visible in the sampled excerpts; those are omitted or marked unverified rather than inferred from names alone.
Main entry points: run.go Agent runtime · controller.go Resource controller · root.go Command routing · delta Delta batching
Components 21
Agent startup
- Agent entry
main.go - Command routing
cmd/root.go - Agent runtime
cmd/agent/run.go - Configuration
internal/config/config.go
Cluster integration
- Provider adapters
pkg/services/providers - Cloud discovery
internal/services/discovery/discovery.go - Cluster registration
pkg/castai/castai.go - Leader election
internal/services/replicas/leaderelection.go - Resource controller
internal/services/controller/controller.go - Resource informers
internal/services/controller/controller.go - Event handlers
internal/services/controller/handlers/handler.go - Agent resource types
internal/services/controller/crd
CAST AI telemetry
- Delta batching
internal/services/controller/delta - CAST AI API client
pkg/castai/castai.go - Remote log export
pkg/log/exporter.go
Operations
- Monitor mode
cmd/monitor/run.go - Diagnostic dump
cmd/dump/run.go - Health and metrics
internal/services/metrics/service.go
Other components
- Cluster operator
- CAST AI platform
- Kubernetes API
Connections 23
- Cluster operator → Agent entry: starts
README.md - Agent entry → Command routing: executes
main.go - Command routing → Agent runtime: routes to
cmd/root.go - Command routing → Monitor mode: routes to
cmd/root.go - Command routing → Diagnostic dump: routes to
cmd/root.go - Agent runtime → Configuration: loads
cmd/agent/run.go - Agent runtime → Provider adapters: selects provider
- Agent runtime → Cluster registration: registers cluster
cmd/agent/run.go - Agent runtime → Leader election: starts optionally
cmd/agent/run.go - Agent runtime → Resource controller: starts
cmd/agent/run.go - Provider adapters → Cloud discovery: discovers cloud
- Agent runtime → Cloud discovery: uses for identity
- Resource controller → Kubernetes API: watches resources
internal/services/controller/controller.go - Resource controller → Resource informers: starts
internal/services/controller/controller.go - Resource controller → Event handlers: dispatches events
- Event handlers → Delta batching: queues changes
internal/services/controller/handlers/handler.go - Resource controller → Agent resource types: watches custom resources
internal/services/controller/controller.go - Resource controller → Delta batching: batches changes
internal/services/controller/controller.go - Delta batching → CAST AI API client: uploads deltas
- Cluster registration → CAST AI platform: registers cluster
pkg/castai/castai.go - CAST AI API client → CAST AI platform: sends API requests
pkg/castai/castai.go - Remote log export → CAST AI API client: sends log events
pkg/log/exporter.go - Agent runtime → Health and metrics: serves endpoints
cmd/agent/run.go