# ngrok/webernetes architecture

> Architecture diagram of the GitHub repository ngrok/webernetes, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ngrok/webernetes
- Repository: https://github.com/ngrok/webernetes

## Overview

#### 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: [`src/cluster/storage/store.ts`](https://github.com/ngrok/webernetes/blob/main/src/cluster/storage/store.ts) (Resource store), [`src/cluster/index.ts`](https://github.com/ngrok/webernetes/blob/main/src/cluster/index.ts) (Cluster API), [`src/cluster/kubelet/kubelet.ts`](https://github.com/ngrok/webernetes/blob/main/src/cluster/kubelet/kubelet.ts) (Kubelet), [`src/cluster/cni/network.ts`](https://github.com/ngrok/webernetes/blob/main/src/cluster/cni/network.ts) (Service routing)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Public APIs"]
  node_package["Package exports<br/>TypeScript entry<br/>[index.ts]"]
  node_cluster_api["Cluster API<br/>user API<br/>[index.ts]"]
  node_client_api["Kubernetes client<br/>typed client<br/>[index.ts]"]
end

subgraph group_control["Control Plane"]
  node_cluster_core["Cluster lifecycle<br/>orchestrator<br/>[cluster.ts]"]
  node_manifest_apply["Manifest apply<br/>resource ingestion<br/>[apply.ts]"]
  node_api_models["Kubernetes models<br/>generated contract<br/>[index.ts]"]
  node_storage[("Resource store<br/>in-memory persistence<br/>[store.ts]")]
  node_client_observation["Informers and watches<br/>client cache<br/>[informer.ts]"]
end

subgraph group_controllers["Reconciliation"]
  node_deployment_controller["Deployment controller<br/>reconciler"]
  node_replicaset_controller["ReplicaSet controller<br/>reconciler<br/>[replica-set.ts]"]
  node_garbage_collector["Garbage collector<br/>ownership cleanup"]
  node_namespace_controller["Namespace controller<br/>built-in controller image"]
end

subgraph group_runtime["Node Runtime"]
  node_kubelet["Kubelet<br/>node reconciler<br/>[kubelet.ts]"]
  node_container_runtime["CRI runtime<br/>container runtime<br/>[runtime.ts]"]
  node_images["Image registry<br/>in-process images<br/>[base.ts]"]
  node_probes["Probe manager<br/>pod lifecycle<br/>[prober-manager.ts]"]
end

subgraph group_network["Network"]
  node_networking["Service routing<br/>virtual HTTP network<br/>[network.ts]"]
  node_dns["DNS resolver<br/>service discovery<br/>[dns.ts]"]
end

subgraph group_ui["Demo"]
  node_demo["React demo<br/>Vite frontend<br/>[main.tsx]"]
end

node_package -->|"exports"| node_cluster_api
node_package -->|"exports"| node_client_api
node_cluster_api -->|"creates"| node_cluster_core
node_cluster_api -->|"applies manifests"| node_manifest_apply
node_client_api -->|"uses types"| node_api_models
node_client_api -->|"resource API"| node_storage
node_manifest_apply -->|"persists resources"| node_storage
node_storage -->|"watch events"| node_client_observation
node_storage -->|"observed resources"| node_deployment_controller
node_deployment_controller -->|"manages ReplicaSets"| node_storage
node_storage -->|"observed resources"| node_replicaset_controller
node_replicaset_controller -->|"manages Pods"| node_storage
node_storage -->|"ownership changes"| node_garbage_collector
node_garbage_collector -->|"deletes dependents"| node_storage
node_cluster_core -->|"initializes nodes"| node_kubelet
node_storage -->|"assigned Pods"| node_kubelet
node_kubelet -->|"sandbox and containers"| node_container_runtime
node_container_runtime -->|"resolves image classes"| node_images
node_kubelet -->|"runs probes"| node_probes
node_cluster_core -->|"initializes"| node_networking
node_networking -->|"resolves names"| node_dns
node_networking -->|"invokes HTTP listeners"| node_container_runtime
node_cluster_api -->|"Cluster.fetch"| node_networking
node_demo -->|"operates cluster"| node_cluster_api

click node_package "https://github.com/ngrok/webernetes/blob/main/src/index.ts"
click node_cluster_api "https://github.com/ngrok/webernetes/blob/main/src/cluster/index.ts"
click node_client_api "https://github.com/ngrok/webernetes/blob/main/src/client/index.ts"
click node_cluster_core "https://github.com/ngrok/webernetes/blob/main/src/cluster/cluster.ts"
click node_manifest_apply "https://github.com/ngrok/webernetes/blob/main/src/cluster/apply.ts"
click node_api_models "https://github.com/ngrok/webernetes/blob/main/src/client/gen/models/index.ts"
click node_storage "https://github.com/ngrok/webernetes/blob/main/src/cluster/storage/store.ts"
click node_client_observation "https://github.com/ngrok/webernetes/blob/main/src/client/informer.ts"
click node_deployment_controller "https://github.com/ngrok/webernetes/blob/main/src/controller/deployment/deployment-controller.ts"
click node_replicaset_controller "https://github.com/ngrok/webernetes/blob/main/src/controller/replicaset/replica-set.ts"
click node_garbage_collector "https://github.com/ngrok/webernetes/blob/main/src/controller/garbagecollector/garbage-collector.ts"
click node_namespace_controller "https://github.com/ngrok/webernetes/blob/main/src/cluster/images/namespace-controller.ts"
click node_kubelet "https://github.com/ngrok/webernetes/blob/main/src/cluster/kubelet/kubelet.ts"
click node_container_runtime "https://github.com/ngrok/webernetes/blob/main/src/cluster/cri/runtime.ts"
click node_images "https://github.com/ngrok/webernetes/blob/main/src/cluster/images/base.ts"
click node_probes "https://github.com/ngrok/webernetes/blob/main/src/cluster/kubelet/prober/prober-manager.ts"
click node_networking "https://github.com/ngrok/webernetes/blob/main/src/cluster/cni/network.ts"
click node_dns "https://github.com/ngrok/webernetes/blob/main/src/cluster/cni/dns.ts"
click node_demo "https://github.com/ngrok/webernetes/blob/main/demo/src/main.tsx"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_package,node_cluster_api,node_client_api toneBlue
class node_cluster_core,node_manifest_apply,node_api_models,node_storage,node_client_observation toneAmber
class node_deployment_controller,node_replicaset_controller,node_garbage_collector,node_namespace_controller toneMint
class node_kubelet,node_container_runtime,node_images,node_probes toneRose
class node_networking,node_dns toneIndigo
class node_demo toneTeal
```

## Components

### Public APIs

- [Package exports](https://github.com/ngrok/webernetes/blob/main/src/index.ts): TypeScript entry · `src/index.ts`
- [Cluster API](https://github.com/ngrok/webernetes/blob/main/src/cluster/index.ts): user API · Creates and operates the simulated cluster. · `src/cluster/index.ts`
- [Kubernetes client](https://github.com/ngrok/webernetes/blob/main/src/client/index.ts): typed client · `src/client/index.ts`

### Control Plane

- [Cluster lifecycle](https://github.com/ngrok/webernetes/blob/main/src/cluster/cluster.ts): orchestrator · Wires nodes, controllers, storage, networking, and images. · `src/cluster/cluster.ts`
- [Manifest apply](https://github.com/ngrok/webernetes/blob/main/src/cluster/apply.ts): resource ingestion · `src/cluster/apply.ts`
- [Kubernetes models](https://github.com/ngrok/webernetes/blob/main/src/client/gen/models/index.ts): generated contract · `src/client/gen/models/index.ts`
- [Resource store](https://github.com/ngrok/webernetes/blob/main/src/cluster/storage/store.ts): in-memory persistence · CRUD, serialization, resource versions, and watches. · `src/cluster/storage/store.ts`
- [Informers and watches](https://github.com/ngrok/webernetes/blob/main/src/client/informer.ts): client cache · `src/client/informer.ts`

### Reconciliation

- [Deployment controller](https://github.com/ngrok/webernetes/blob/main/src/controller/deployment/deployment-controller.ts): reconciler · `src/controller/deployment/deployment-controller.ts`
- [ReplicaSet controller](https://github.com/ngrok/webernetes/blob/main/src/controller/replicaset/replica-set.ts): reconciler · `src/controller/replicaset/replica-set.ts`
- [Garbage collector](https://github.com/ngrok/webernetes/blob/main/src/controller/garbagecollector/garbage-collector.ts): ownership cleanup · `src/controller/garbagecollector/garbage-collector.ts`
- [Namespace controller](https://github.com/ngrok/webernetes/blob/main/src/cluster/images/namespace-controller.ts): built-in controller image · `src/cluster/images/namespace-controller.ts`

### Node Runtime

- [Kubelet](https://github.com/ngrok/webernetes/blob/main/src/cluster/kubelet/kubelet.ts): node reconciler · Converges assigned Pods into runtime actions. · `src/cluster/kubelet/kubelet.ts`
- [CRI runtime](https://github.com/ngrok/webernetes/blob/main/src/cluster/cri/runtime.ts): container runtime · Runs in-process TypeScript image implementations. · `src/cluster/cri/runtime.ts`
- [Image registry](https://github.com/ngrok/webernetes/blob/main/src/cluster/images/base.ts): in-process images · `src/cluster/images/base.ts`
- [Probe manager](https://github.com/ngrok/webernetes/blob/main/src/cluster/kubelet/prober/prober-manager.ts): pod lifecycle · `src/cluster/kubelet/prober/prober-manager.ts`

### Network

- [Service routing](https://github.com/ngrok/webernetes/blob/main/src/cluster/cni/network.ts): virtual HTTP network · Routes Service, NodePort, and Pod HTTP traffic. · `src/cluster/cni/network.ts`
- [DNS resolver](https://github.com/ngrok/webernetes/blob/main/src/cluster/cni/dns.ts): service discovery · `src/cluster/cni/dns.ts`

### Demo

- [React demo](https://github.com/ngrok/webernetes/blob/main/demo/src/main.tsx): Vite frontend · Visualizes cluster state and requests. · `demo/src/main.tsx`

## Connections

- 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
