Kubernetes adapter
AlphaSwarm wraps every cluster-side operation in a pluggable
:class:alphaswarm.kubernetes.KubernetesAdapter. The abstraction makes the
rpi_kubernetes attach optional: AlphaSwarm works fully standalone with
NoneAdapter, attaches to the rpi management API with
RpiClusterAdapter, talks to a Kubernetes API directly with
InClusterAdapter, or treats the local Docker Compose stack as the
cluster surface with LocalComposeAdapter.
Architecture
get_kubernetes_adapter() returns the active adapter based on:
- Explicit
settings.kubernetes_adapter(none/rpi_cluster/in_cluster/local_compose). - Auto-promote: empty kind +
cluster_mgmt_urlset →rpi_cluster. - Default:
none.
Failures during a call surface as
:class:KubernetesAdapterUnavailable (routes return 503) or
:class:KubernetesAdapterError (routes return 502). Adapters opt out
of unsupported methods by raising
:class:KubernetesAdapterUnavailable.
Adapter capabilities
See .cursor/rules/kubernetes-adapter.mdc
for the per-method matrix. Today every adapter implements
is_available(); RpiClusterAdapter covers the full Kafka / Flink /
AlphaVantage / scale_deployment surface; InClusterAdapter covers
scale_deployment / pod_logs / apply_manifest; LocalComposeAdapter
covers scale_deployment / pod_logs.
The /cluster REST surface is the primary user — /cluster-mgmt is
kept as a backwards-compat alias.
Test patterns
tests/kubernetes/test_adapter.py covers:
- The metaclass registers every concrete adapter under
"k8s_adapter"in the AlphaSwarm registry. NoneAdapter.is_available()isFalse; every op raises.RpiClusterAdapterforwards to the wrapped client and translatesClusterMgmtError→KubernetesAdapterError.InClusterAdapterreports unavailable when kubernetes isn't installed (CI default).register_adapter(...)/reset_kubernetes_adapter()give tests clean fixtures.
Adding capabilities
When you need a new cluster op (say list_namespaces):
- Add an abstract method (default: raise
:class:
KubernetesAdapterUnavailable) on the ABC inalphaswarm/kubernetes/protocol.py. - Implement it in each adapter that can support it.
- Add a route in
alphaswarm/api/routes/cluster_mgmt.pythat calls the adapter. - Adapters that can't service the op leave the default; routes
catch
KubernetesAdapterUnavailableand translate to 503.
Migrating finops_tasks
The FinOps audit (alphaswarm/tasks/finops_tasks.py) currently uses the
kubernetes SDK directly because it needs list APIs (list_pod_for_all_namespaces,
etc.) that the adapter doesn't yet expose. Adding those list methods
to the adapter ABC + the in-cluster implementation is the migration
target — until then, the direct path is grandfathered by the
.cursor/rules/kubernetes-adapter.mdc
rule.