Skip to content

Refactor/k8s cluster port pr1 - #3

Merged
krs2k merged 7 commits into
masterfrom
refactor/k8s-cluster-port-pr1
Apr 30, 2026
Merged

krs2k merged 7 commits into
masterfrom
refactor/k8s-cluster-port-pr1

Conversation

@krs2k

@krs2k krs2k commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

No description provided.

krs2k and others added 7 commits April 30, 2026 18:41
Introduce the Cluster interface in pkg/k8s as the single port between ct and
Kubernetes, plus a live adapter (NewLiveCluster) that delegates to the existing
*Client and an in-memory Fake adapter in pkg/k8s/k8stest for tests.

This change is additive: no existing API or consumer is modified. Subsequent
PRs will migrate internal/cli, pkg/sync, and internal/dev to depend on Cluster
and PodExecutor instead of *k8s.Client and the funcvar/type-assert pattern.

The Fake exposes its state via public fields (Releases, Pods, Namespaces, hooks,
recorded calls) and ActiveOps/HasActiveExec helpers — designed to make the
runDevSession cancel-cascade test (impossible today) trivial in PR4.

Refs: #2

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replace per-command k8s funcvars (newK8sClientFor*, applyReleaseForApply,
loadInventoryForDelete, listReleasesForList, …) with a single seam
newClusterFn in internal/cli/cluster.go. apply/delete/list now depend on
k8s.Cluster instead of *k8s.Client.

Tests use k8stest.NewFake() through a withFakeCluster helper. An errCluster
wrapper drives specific error paths (apply/list/delete failures, ensure
namespace failures) without rebuilding the whole adapter.

runDelete now calls cluster.DeleteRelease (composite: load inventory ->
delete refs -> drop inventory CM) instead of orchestrating the three steps
manually. ensureApplyNamespace takes k8s.Cluster.

10 funcvars removed across apply.go (4), delete.go (4), list.go (2).
~130 lines deleted from CLI tests.

Refs: #2

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Syncer now depends on k8s.PodExecutor instead of *k8s.Client. NewSyncer takes
the port plus an explicit namespace. The three internal funcvars
(waitForPodFn, execStreamFn, execSimpleFn) are removed; sync helpers
(execStream, execSimple) are private methods that route through the port.

Tests use a 25-line inline fakePodExecutor with optional WaitFn/ExecFn hooks
and a recorded ExecCalls slice. The previous saveSyncSeams pattern (three
funcvar overrides per test) is gone.

internal/dev/runner.go's runSyncFn is bridged through k8s.AsPodExecutor — a
temporary helper that wraps an existing *Client as a PodExecutor. This will
be removed in PR4 when runDevSession takes a k8s.Cluster directly from
RunOpts.

Refs: #2

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The dev runner now depends on k8s.Cluster instead of *k8s.Client. Removed:
the kubeApplier interface, all 11 K8s funcvars (newK8sClient, runWaitForPodFn,
runTerminalFn, runPortForwardFn, runLogsFn, runWatchPodHealthFn, runSyncFn,
ensureNamespaceFn, applyReleaseFn, loadInventoryFn, deleteResourcesFn,
deleteInventoryFn, injectReleaseLabelsFn), the seven type-asserts to
*k8s.Client, and startDevFeatures as a funcvar.

runDevSession and startDevFeatures now take a k8s.Cluster plus an explicit
namespace. Sync uses ctsync.NewSyncer(cluster, ns, sel, rule) directly — no
more AsPodExecutor bridge. Terminal calls cluster.Exec with k8s.ExecOpts;
TTY raw-mode handling stays in pkg/k8s/live_cluster.go for now (move to
internal/tty deferred). runDevDelete collapses to cluster.DeleteRelease.

Tests rewritten to use a devCluster fake (embeds k8stest.Fake plus per-method
hooks). The cancel-cascade integration test that was impossible to write
before — TestRunDevSession_CancelCascadesToAllFeatures — now runs in <50ms
with the race detector and verifies that PortForward, StreamLogs, WatchPod
and Exec all return after ctx cancel.

Pre-existing data race in TestStartDevFeatures_HealthWatcherTriggersReconnect
(reproducible on master with -race) is fixed: the test no longer monkey-patches
funcvars; goroutine ownership of state is clean. A small no-op-spinner seam
(progressSpinner / startSpinner, 1 funcvar) sidesteps a separate race in
pterm.SpinnerPrinter.IsActive that surfaces under -race.

Net deletion: ~487 lines (runner.go + runner_test.go).

Refs: #2

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
After PRs 1-4 nothing outside pkg/k8s calls the package-level helpers
(WaitForPod, Exec, ExecStream, ExecSimple, PortForward, StreamLogs,
WatchPodHealth, EnsureNamespace, LoadInventory, SaveInventory,
DeleteInventory, ListReleases, ComputeOrphaned, ResourcesToRefs) or the
*Client struct/constructors. This PR makes that explicit:

- All package-level helpers are now lowercase (waitForPod, ensureNamespace,
  …). The thin WaitForPod wrapper is deleted (waitForPod was always there).
  Exec is renamed execInteractive — it still exists as a private helper for
  raw-mode TTY tests, used internally by liveCluster.Exec.
- *Client struct → *client; NewClient/NewClientFromInterfaces → newClient,
  newClientFromInterfaces; methods Apply/Delete/ApplyRelease → apply, del,
  applyRelease.
- AsPodExecutor (the temporary PR3 bridge) is removed — internal/dev now
  takes a Cluster directly from RunOpts (PR4) so the bridge is unused.
- ExecStreamOpts → execStreamOpts (private).

Public surface of pkg/k8s is now:
  Cluster, PodExecutor                (interfaces)
  NewLiveCluster                       (live constructor)
  InjectReleaseLabels                  (pure helper)
  Resource, ResourceRef, ReleaseInfo,
  Selector, ExecOpts, LiveOpts, PortRule  (value types)

Down from ~17 functions + 3 methods exported before. Consumers depend only
on the port and value types; client-go is fully quarantined inside pkg/k8s.

Refs: #2

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Merge inventory.go, prune.go, labels.go, namespace.go into release.go.
The four files held 36-56 lines each — every one defining a single helper
in the release lifecycle (inventory CRUD, orphan diff, label injection,
namespace bootstrap). Reading "how does ct manage a release" required
jumping across four files; tracking the managed-by/instance label scheme
required reading two files (constants in inventory.go vs labels.go, with
duplicate values).

After:
- pkg/k8s shrinks from 14 production files to 10
- release.go (409 lines) is the single source of truth for release
  lifecycle: applyRelease orchestrator, ensureNamespace, inventory CRUD
  (load/save/delete/list), resourcesToRefs, computeOrphaned, and
  InjectReleaseLabels
- duplicate label key constants unified (managedByLabelKey,
  instanceLabelKey, inventoryDataKey, inventoryCMPrefix) — single source
- ResourceRef stays in delete.go alongside orderForDelete (delete primitive)

Test files unchanged in count: inventory_test.go, prune_test.go,
labels_test.go, namespace_test.go remain split because each tests a
focused public/internal helper and consolidation would yield an 800+ line
test file.

Refs: #2

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
template_e2e_test.go drives the cobra command via cmd.Execute() with on-disk
.ct fixtures, then asserts on the rendered YAML/JSON captured from stdout.
Exercises the full pipeline: argument parsing → resolveSourceDir →
renderResources → engine.Bundle (esbuild) → engine.Execute (goja) →
InjectReleaseLabels → output.Serialize.

Fixtures use only the __ct_resources global (no URL imports), so tests run
fully offline and don't depend on cache state.

Coverage:
- YAML output (default) and explicit -o json
- Auto-detected values.json
- --set overrides
- -f explicit values file (multi-env)
- -n namespace flag as default for namespaced resources
- Multi-resource render order
- User-provided labels win over injected managed-by; instance label still added
- Release.{name,namespace} globals exposed to the script
- Bundle/exec error surfaces to caller
- Unsupported -o format produces serialization error

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@krs2k
krs2k merged commit 4ff2dff into master Apr 30, 2026
6 checks passed
@krs2k
krs2k deleted the refactor/k8s-cluster-port-pr1 branch October 5, 2026 17:04
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.

1 participant