Skip to content

refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite - #2527

Merged
dan-petty merged 2 commits into
mainfrom
refactor/1315-kubernetes-api-seam
Oct 28, 2026
Merged

dan-petty merged 2 commits into
mainfrom
refactor/1315-kubernetes-api-seam

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

devops-cli reached the Kubernetes API through KubernetesService and four other loaders, and its tests reached the API by patching the SDK. This change makes the service the one seam of the Kubernetes API edge, with a shared fake and a contract suite that holds the fake to the real client.

  • src/devops_cli/k8s/api.py is the seam. The Protocols CoreApi, CustomObjectsApi and VersionApi name the 18 calls that devops-cli makes on the SDK's API objects, and KubernetesApiSource names where the objects come from: contexts() and connect(context).
  • KubernetesClientSource is the one real source, with one configuration rule: the kubeconfig when the kubeconfig names the context, else the pod's service account. Before, the service tried the service account first, so inside a pod it ignored the configured context. The source reads every KUBECONFIG entry when it connects, and it builds each ApiClient with its own Configuration, so it never writes the client's global default.
  • KubernetesService takes a source. KubernetesService.install(source) replaces the process's service until reset_instance. core_api and custom_objects_api are its public accessors, and the informer and the service proxy use core_api instead of the private _core_v1.
  • switch_context checks that the kubeconfig names the context and points the in-process clients at it. It no longer writes the kubeconfig after kubectl config use-context, so the comments of the file stay. The unused AppsV1Api client and _get_kubeconfig_path are deleted.
  • tests/support/fakes/kubernetes.py promotes tests/k8s_fakes.py: FakeCluster, a source over FakeCoreV1, FakeCustomObjects and FakeVersion, behind the cluster fixture. A missing object is the SDK's ApiException with its status, unreachable() refuses every call as an API server that does not listen, and each fake records its calls.
  • tests/k8s/test_kubernetes_contract.py runs each case through the service over the fake and over the real client, whose kubeconfig in tmp_path points at a loopback server that replays tests/fixtures/kubernetes/recorded_responses.json.
  • The tests that reached the service through _core_v1, patched load_config, or patched kubernetes modules now use the fake. tests/test_k8s_service.py patches no kubernetes name and no method of the service.
  • k8s/service_proxy.py, k8s/doctor.py, commands/k8s/cluster_context.py and ui/data_providers.py keep their own loaders until test(k8s): k8s/ is tested through its public interface over the Kubernetes API, process, HTTP and keyring fakes, with no internal patches or private imports #1322, test(k8s): devops k8s commands are thin adapters tested over edge fakes, and the _K8sModule patch forwarding is gone #1323 and test(telemetry): telemetry, server, ui, output and docs tests run on edge fakes, and the dashboard suite splits by behaviour #1334 route them through the seam.

Closes #1315

Change: openspec/changes/archive/2026-10-28-1315-kubernetes-api-seam/

…hared fake cluster and its contract suite

`k8s/api.py` holds the seam. The Protocols `CoreApi`, `CustomObjectsApi`
and `VersionApi` name the 18 calls that devops-cli makes on the SDK's
API objects, and `KubernetesApiSource` names where the objects come
from. `KubernetesClientSource` is the one real source. It uses the
kubeconfig when the kubeconfig names the context, else the pod's service
account, reads every `KUBECONFIG` entry when it connects, and builds each
`ApiClient` with its own `Configuration`.

`KubernetesService` takes a source. `install(source)` replaces the
process's service until `reset_instance`, and `core_api` and
`custom_objects_api` are its public accessors, so the informer and the
service proxy no longer read `_core_v1`. `switch_context` checks the
context and points the clients at it; it no longer writes the kubeconfig
after `kubectl config use-context`. The unused `AppsV1Api` client is
deleted.

`tests/support/fakes/kubernetes.py` promotes `tests/k8s_fakes.py`:
`FakeCluster` over `FakeCoreV1`, `FakeCustomObjects` and `FakeVersion`,
behind the `cluster` fixture. `tests/k8s/test_kubernetes_contract.py`
runs each case over the fake and over the real client against recorded
API responses that a loopback server replays. The tests that reached the
service through `_core_v1`, `load_config` patches or patched SDK modules
now use the fake.
@dan-petty

Copy link
Copy Markdown
Owner Author

Test evidence:

Pending a person:

  • Capture the recorded responses in tests/fixtures/kubernetes/ once from a disposable local cluster, scrubbed to example.com names and documentation-range addresses, and commit them with the commands that captured them. The responses in this pull request are written from the API reference.
  • Review the diff in tuicr (tuicr -r origin/main..HEAD).

@dan-petty
dan-petty marked this pull request as ready for review October 28, 2026 20:15
@dan-petty
dan-petty merged commit 0f6b9af into main Oct 28, 2026
17 checks passed
@dan-petty
dan-petty deleted the refactor/1315-kubernetes-api-seam branch October 28, 2026 20:16
dan-petty pushed a commit that referenced this pull request Oct 28, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.11.1](v0.11.0...v0.11.1)
(2026-10-28)


### Bug Fixes

* **config:** devops config audit-keys scans each config file that the
settings merge
([#2516](#2516))
([57541df](57541df))
* **security:** the osv, internetdb, radar and cloudflare api clients
dial through the egress-checked factory at the public level
([#2509](#2509))
([0379f21](0379f21))


### Code Refactoring

* **k8s:** KubernetesService is the Kubernetes API seam, with a shared
fake cluster and its contract suite
([#2527](#2527))
([0f6b9af](0f6b9af)),
closes [#1315](#1315)

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(k8s): KubernetesService is the Kubernetes API seam, with a shared fake cluster and its contract suite

1 participant