How ngrok/webernetes works
Purpose and Public Entry Points
Webernetes is a TypeScript Kubernetes subset/simulator that runs entirely in a browser or JavaScript runtime—no real container images, Kubernetes API server, or backend cluster. Consumers create a Cluster, register in-process image classes, initialize nodes, apply Kubernetes-like manifests, and issue simulated HTTP requests. Public exports begin at src/index.ts; primary user API surfaces are src/cluster/index.ts and src/client/index.ts. Runnable usage is demonstrated by examples/01-single-pod.ts; the interactive browser frontend starts at demo/src/main.tsx.
Cluster Composition and Lifecycle
Cluster is the orchestration root: it initializes a fixed simulated node set, wiring storage, controllers, kubelets, networking, eventing, and image registration. Manifest application is routed through src/cluster/apply.ts, while server-facing cluster behavior is in src/cluster/cluster.ts and src/cluster/server.ts. Cluster context and synchronization boundaries are represented by src/cluster/context.ts; simulated time is abstracted through src/clock.ts and src/clock-context.ts.
Kubernetes API, Persistence, and Observation
Kubernetes-shaped generated models and typed API implementations form the external resource contract, notably src/client/gen/models/index.ts, src/client/gen/apis/impls/CoreV1Api.ts, and src/client/gen/apis/impls/AppsV1Api.ts. The in-memory control-plane persistence layer owns resource CRUD, serialization, resource versions, and watches: src/cluster/storage/store.ts, src/cluster/storage/watch.ts, and resource-specific stores such as src/cluster/storage/pod.ts. Client caching/informers/watch machinery consume those boundaries through src/client/cache.ts, src/client/informer.ts, and src/client/watch.ts.
Reconciliation Controllers
Controllers observe stored resources and converge desired state. Deployments create/manage ReplicaSets through src/controller/deployment/deployment-controller.ts; ReplicaSets create/delete Pods via src/controller/replicaset/replica-set.ts. Ownership-based cleanup is handled by src/controller/garbagecollector/garbage-collector.ts and its dependency graph in src/controller/garbagecollector/graph-builder.ts. Namespace deletion is separately supported by a built-in image/controller at src/cluster/images/namespace-controller.ts. Work queues, retry/rate-limit semantics, and list/watch primitives are adapted from Kubernetes patterns in src/client-go/util/workqueue/queue.ts, src/client-go/tools/cache/reflector.ts, and src/client-go/tools/cache/listwatch.ts.
Node and Pod Execution Plane
Each simulated node runs a kubelet-like reconciliation loop that translates assigned Pod specs into sandbox/container lifecycle actions. Core orchestration is in src/cluster/kubelet/kubelet.ts and src/cluster/kubelet/pod-workers.ts; runtime execution is delegated through src/cluster/kubelet/kuberuntime/kuberuntime-manager.ts to the custom CRI implementation at src/cluster/cri/runtime.ts. Images are TypeScript BaseImage-style process implementations, registered and resolved through src/cluster/images/base.ts and src/cluster/cri/image.ts, rather than pulled from registries. Probes update lifecycle/readiness state via src/cluster/kubelet/prober/prober-manager.ts.
Simulated Network and Service Discovery
Networking intentionally models DNS and HTTP, not raw TCP/UDP or CNI packet behavior. The CNI boundary is src/cluster/cni/index.ts; service/pod routing and virtual networking reside in src/cluster/cni/network.ts and src/cluster/cni/http.ts. DNS resolution is implemented in src/cluster/cni/dns.ts. Services select Pods and expose ClusterIP/NodePort routes, while EndpointSlices track backing endpoints in src/cluster/storage/service.ts and src/cluster/storage/endpointslice.ts. Requests enter through Cluster.fetch, resolve node/service/pod destinations, and ultimately invoke registered container HTTP listeners.
Runtime, Demo, and External Infrastructure
The main runtime is Node-compatible/browser-oriented TypeScript; the demo is a Vite React application (demo/src/app.tsx, demo/vite.config.ts) visualizing cluster state and requests. There is no production Kubernetes, Docker Hub, or persistent database dependency. Real etcd and k3s appear only as optional test harness infrastructure under src/test/harnesses/etcd-real.ts and src/test/harnesses/kubernetes-k3s.ts. CI publishes the npm package and deploys/serves the demo through GitHub workflows, including .github/workflows/publish-npm.yml and .github/workflows/ngrok-compute-ngrok-webernetes.yml.
Main entry points: store.ts Resource store · index.ts Cluster API · kubelet.ts Kubelet · network.ts Service routing
Components 19
Public APIs
- Package exports
src/index.ts - Cluster API · Creates and operates the simulated cluster.
src/cluster/index.ts - Kubernetes client
src/client/index.ts
Control Plane
- Cluster lifecycle · Wires nodes, controllers, storage, networking, and images.
src/cluster/cluster.ts - Manifest apply
src/cluster/apply.ts - Kubernetes models
src/client/gen/models/index.ts - Resource store · CRUD, serialization, resource versions, and watches.
src/cluster/storage/store.ts - Informers and watches
src/client/informer.ts
Reconciliation
- Deployment controller
src/controller/deployment/deployment-controller.ts - ReplicaSet controller
src/controller/replicaset/replica-set.ts - Garbage collector
src/controller/garbagecollector/garbage-collector.ts - Namespace controller
src/cluster/images/namespace-controller.ts
Node Runtime
- Kubelet · Converges assigned Pods into runtime actions.
src/cluster/kubelet/kubelet.ts - CRI runtime · Runs in-process TypeScript image implementations.
src/cluster/cri/runtime.ts - Image registry
src/cluster/images/base.ts - Probe manager
src/cluster/kubelet/prober/prober-manager.ts
Network
- Service routing · Routes Service, NodePort, and Pod HTTP traffic.
src/cluster/cni/network.ts - DNS resolver
src/cluster/cni/dns.ts
Demo
- React demo · Visualizes cluster state and requests.
demo/src/main.tsx
Connections 24
- Package exports → Cluster API: exports
- Package exports → Kubernetes client: exports
- Cluster API → Cluster lifecycle: creates
- Cluster API → Manifest apply: applies manifests
- Kubernetes client → Kubernetes models: uses types
- Kubernetes client → Resource store: resource API
- Manifest apply → Resource store: persists resources
- Resource store → Informers and watches: watch events
- Resource store → Deployment controller: observed resources
- Deployment controller → Resource store: manages ReplicaSets
- Resource store → ReplicaSet controller: observed resources
- ReplicaSet controller → Resource store: manages Pods
- Resource store → Garbage collector: ownership changes
- Garbage collector → Resource store: deletes dependents
- Cluster lifecycle → Kubelet: initializes nodes
- Resource store → Kubelet: assigned Pods
- Kubelet → CRI runtime: sandbox and containers
- CRI runtime → Image registry: resolves image classes
- Kubelet → Probe manager: runs probes
- Cluster lifecycle → Service routing: initializes
- Service routing → DNS resolver: resolves names
- Service routing → CRI runtime: invokes HTTP listeners
- Cluster API → Service routing: Cluster.fetch
- React demo → Cluster API: operates cluster