Kubernetes API Client, CRD Sync Loop, and Background Reap Worker
Source:
docs/enhancements/0008-kube-controller-loop/README.md· Status:implemented· Area:core· Milestone:—| Field | Value | | :--- | :--- | | CEP Number |0008| | Title | Kubernetes API Client, CRD Sync Loop, and Background Reap Worker (pkg/controller) | | Authors |@cubeplex-arch| | Status |implemented| | Target Version |v0.2.0| | Created |2026-11-10| | Last Updated |2026-11-10|
Summary
Section titled “Summary”This CEP upgrades cubepod-controller (pkg/controller and cmd/cubepod-controller) from a purely passive in-memory reconciler into a complete Kubernetes Operator with:
- A pluggable
KubeClientinterface and lightweight HTTP/REST Kubernetes API client (pkg/controller/kubeclient.go) capable of applying/deletingPersistentVolumeClaim(volumeMode: Block),Pod(runtimeClassName: kata-qemu), andSecretresources against a real Kubernetes APIServer (or anhttptest.ServerK8s mock). - A background controller manager loop (
RunControllerLoop) that periodically reconcilesPodWarmPoolreplenishment, executesReapIdlefor expired sandboxes, and exposes/healthzand/metricsendpoints on:8081.
Motivation
Section titled “Motivation”In v0.1.0, pkg/controller synthesized BlockPVCManifest and PodManifest structs in memory, and cmd/cubepod-controller/main.go blocked on SIGTERM without running a reconciliation ticker or health server. For v0.2.0, cubepod-controller must actively drive a KubeClient backend and run an observable background control loop.
- Define
KubeClientinterface inpkg/controller/kubeclient.go:EnsurePVC(ctx context.Context, pvc BlockPVCManifest) errorDeletePVC(ctx context.Context, namespace, name string) errorEnsurePod(ctx context.Context, pod PodManifest) (podIP string, err error)DeletePod(ctx context.Context, namespace, name string) error
- Implement
RESTKubeClient(talking standard Kubernetes JSON REST endpoints/api/v1/namespaces/{ns}/persistentvolumeclaimsand/api/v1/namespaces/{ns}/podswith Bearer token / TLS support) andMemoryKubeClient(recording applied K8s objects for inspection). - Wire
Reconcilerto invoke its configuredKubeClientwheneverReconcile,ReconcileWarmPool,Pause,ResetRootFS, orDeletetransitions state. - Implement
ControllerManager(pkg/controller/manager.go) with:- Periodic
ReapIdleandPodWarmPoolreplenishment loop (Start(ctx)). - HTTP
/healthzand/metrics(JSON/Prometheus counters forcubepod_active_sandboxes,cubepod_paused_sandboxes,cubepod_warmpool_ready) handler.
- Periodic
- Update
cmd/cubepod-controller/main.goto startControllerManagerand serve:8081(/healthz,/metrics).
Non-Goals
Section titled “Non-Goals”- Pulling in the multi-hundred-megabyte
k8s.io/client-godependency tree; standard Gonet/httpagainst the Kubernetes v1 REST API keeps compilation fast and dependency-free.
Proposal
Section titled “Proposal”Risks and Mitigations
Section titled “Risks and Mitigations”| Risk | Impact | Likelihood | Mitigation |
|---|---|---|---|
| Transient K8s APIServer 409 Conflict on PVC/Pod creation | Low | High | Treat 409 Conflict (AlreadyExists) on POST /pods or POST /persistentvolumeclaims as idempotent success in RESTKubeClient. |
Design Details
Section titled “Design Details”K8s REST Serialization (pkg/controller/kubeclient.go)
Section titled “K8s REST Serialization (pkg/controller/kubeclient.go)”RESTKubeClient serializes BlockPVCManifest into standard Kubernetes v1.PersistentVolumeClaim JSON (apiVersion: v1, kind: PersistentVolumeClaim, spec.volumeMode: "Block", spec.dataSource when DataSourcePVC != "") and PodManifest into standard Kubernetes v1.Pod JSON (apiVersion: v1, kind: Pod, spec.runtimeClassName, spec.initContainers, spec.containers, spec.volumes).
Test Plan
Section titled “Test Plan”- Unit & Integration Tests (
pkg/controller/kubeclient_test.go):- Stand up a mock Kubernetes APIServer using
httptest.NewServerhandling/api/v1/namespaces/cubepod-workloads/persistentvolumeclaimsand/api/v1/namespaces/cubepod-workloads/pods. - Verify
Reconcilerconfigured withRESTKubeClientcreates dualvolumeMode: BlockPVCs and the Kata Pod onDesiredStateRunning, deletes only the Pod (keeping both PVCs) onDesiredStatePaused, deletes old RootFS PVC and createsgen2RootFS PVC (keeping Workspace PVC) onRootfsGeneration: 2, and populatesdataSourceonForkFrom. - Verify
ControllerManagerbackground loop runsReapIdleand serves/healthzand/metrics.
- Stand up a mock Kubernetes APIServer using
Graduation Criteria
Section titled “Graduation Criteria”go test -v -race ./pkg/controller/...andgo build ./cmd/cubepod-controllerpass with zero warnings.
Upgrade / Downgrade Strategy
Section titled “Upgrade / Downgrade Strategy”- Default
ReconcilerusesMemoryKubeClientwhen noKubeClientis injected, preserving 100% backward compatibility withv0.1.0callers.
Production Readiness Review (PRR)
Section titled “Production Readiness Review (PRR)”- Observability:
/healthzand/metricsendpoints exposed byControllerManager.
Alternatives Considered
Section titled “Alternatives Considered”k8s.io/client-go: Rejected to keep the repository lightweight, fast to build, and free of transitive dependency bloat.