<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="rss.xsl"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
    <channel>
        <title>Joël Vappiani Blog</title>
        <link>https://blog.djovap.com/</link>
        <description>Joël Vappiani Blog</description>
        <lastBuildDate>Mon, 02 Nov 2026 00:00:00 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <language>en</language>
        <item>
            <title><![CDATA[Multi-tenant Kubernetes workspaces: isolation, self-heal, and one config]]></title>
            <link>https://blog.djovap.com/multi-tenant-workspace-operator</link>
            <guid>https://blog.djovap.com/multi-tenant-workspace-operator</guid>
            <pubDate>Mon, 02 Nov 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[How we built a Workspace operator that isolates each student in their own namespace, keeps platform-owned resources from drifting, and propagates one Git-managed template to every tenant — without requiring ArgoCD in the cluster.]]></description>
            <content:encoded><![CDATA[<p>Kappsul started as a service offered at the University of Lorraine: students who needed
an isolated environment to deploy an application could get one from a shared Kubernetes
cluster, through a web interface. The platform had to turn a click into a namespace with
a quota, access rules, a network boundary, and a place to run the student's app. That is
a multi-tenancy problem before it is a Kubernetes problem: every student needs enough
freedom to be useful, and a hard enough boundary that they cannot affect anyone else.</p>
<!-- -->
<p>The first implementation used ArgoCD. Each workspace was an ArgoCD Application, all
pointing at the same Git repository, and ArgoCD did the reconciliation. It was
declarative, auditable, and it worked. But it made a central feature of our product
depend on ArgoCD: creating a workspace required an ArgoCD installation and a Git source,
and the platform could not run on a cluster that did not have them. We wanted the service
to be portable to any Kubernetes cluster, and we did not want one of its core features to
be delegated to an external tool.</p>
<p>We also realised we did not need all of ArgoCD. We needed two behaviours:</p>
<ul>
<li class=""><strong>self-heal</strong>: a student must not be able to change the platform-owned parts of their
workspace;</li>
<li class=""><strong>propagation</strong>: one change to the central configuration must update every workspace
and its resources.</li>
</ul>
<p>So we kept GitOps for what it is good at — changing the central configuration in Git —
and wrote a small operator that belongs to the application. The operator creates the
workspace envelope, keeps it from drifting, and propagates the shared template to every
tenant. It uses only the Kubernetes API and a CRD, so it runs on any cluster.</p>
<p>This article is about that operator: the isolation model, the two GitOps behaviours, and
the trade-offs.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="multi-tenancy-is-the-reason-the-operator-exists">Multi-tenancy is the reason the operator exists<a href="https://blog.djovap.com/multi-tenant-workspace-operator#multi-tenancy-is-the-reason-the-operator-exists" class="hash-link" aria-label="Direct link to Multi-tenancy is the reason the operator exists" title="Direct link to Multi-tenancy is the reason the operator exists" translate="no">​</a></h2>
<p>A workspace is not just a namespace. It is an isolation envelope:</p>
<table><thead><tr><th>Layer</th><th>What it gives the student</th><th>What it protects the platform from</th></tr></thead><tbody><tr><td>Namespace</td><td>A private place to deploy</td><td>Name collisions and accidental cross-tenant access</td></tr><tr><td>ResourceQuota</td><td>A predictable CPU, memory, storage and pod budget</td><td>One workspace starving the cluster</td></tr><tr><td>LimitRange</td><td>Default requests and limits for containers</td><td>Pods without limits slipping through</td></tr><tr><td>RBAC</td><td><code>admin</code> inside their namespace, for the owner and guests</td><td>Students acting outside their workspace</td></tr><tr><td>NetworkPolicy</td><td>Default-deny with explicit ingress and egress</td><td>Cross-namespace traffic and unexpected egress</td></tr><tr><td>Pod Security Admission</td><td><code>baseline</code> enforcement, <code>restricted</code> audit/warn</td><td>Privileged or unsafe pods</td></tr></tbody></table>
<p>The operator's first job is to create that envelope for every student, in the same shape,
with the right values. The student then gets <code>admin</code> inside the namespace so they can
deploy and manage their applications. That is the multi-tenant contract: <strong>a private
namespace with a hard boundary, and enough freedom inside it to be useful.</strong></p>
<p>The boundary is enforced by Kubernetes primitives, not by the operator. The operator's
job is to make sure the envelope exists and stays correct. That distinction matters for
the rest of this article.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="portability-dont-put-an-external-tool-on-the-critical-path">Portability: don't put an external tool on the critical path<a href="https://blog.djovap.com/multi-tenant-workspace-operator#portability-dont-put-an-external-tool-on-the-critical-path" class="hash-link" aria-label="Direct link to Portability: don't put an external tool on the critical path" title="Direct link to Portability: don't put an external tool on the critical path" translate="no">​</a></h2>
<p>Portability, for us, means one thing: the operator must not need anything beyond a
Kubernetes API and the CRD. No ArgoCD, no Git server, no network access to Git at runtime.
That is the direct consequence of the first implementation: one ArgoCD Application per
workspace, all pointing at the same Git repository. It worked, but it made workspace
creation depend on an ArgoCD installation and its Git source. A core product operation
was sitting behind a dependency that had nothing to do with the product.</p>
<p>The operator therefore uses only the Kubernetes API and a CRD. GitOps is still how the
<strong>template</strong> reaches the cluster: the platform chart, including the workspace resource
template, can be deployed by ArgoCD, Flux, Helm, or <code>kubectl apply</code>. The operator does not
care. It is the runtime that turns that template into tenant namespaces.</p>
<p>That is the portability argument in one sentence: <strong>GitOps for delivery, an operator for
runtime.</strong></p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-two-gitops-behaviours-we-actually-needed">The two GitOps behaviours we actually needed<a href="https://blog.djovap.com/multi-tenant-workspace-operator#the-two-gitops-behaviours-we-actually-needed" class="hash-link" aria-label="Direct link to The two GitOps behaviours we actually needed" title="Direct link to The two GitOps behaviours we actually needed" translate="no">​</a></h2>
<p>We did not need all of ArgoCD. We needed self-heal and propagation, and nothing else from
the GitOps toolbox.</p>
<ul>
<li class=""><strong>Self-heal.</strong> If a student edits the quota, the limit range, the RBAC, or the network
policy, the operator puts it back.</li>
<li class=""><strong>Propagation.</strong> The workspace envelope is defined once. Change that one configuration,
and every workspace and its resources are updated to match.</li>
</ul>
<p>We did not need a per-tenant Application, a sync dashboard per workspace, an app-of-apps,
or multi-cluster orchestration. We needed a controller that owns the envelope, detects
drift, and applies a shared template.</p>
<p>The operator also handles creation and deletion, because a tenant lifecycle needs both.
But self-heal and propagation are the reason it exists.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-crd-is-the-tenant-record">The CRD is the tenant record<a href="https://blog.djovap.com/multi-tenant-workspace-operator#the-crd-is-the-tenant-record" class="hash-link" aria-label="Direct link to The CRD is the tenant record" title="Direct link to The CRD is the tenant record" translate="no">​</a></h2>
<p>The <code>Workspace</code> CRD is deliberately small. The spec is the input; the status is the
operator's durable memory.</p>
<div class="language-go codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-go codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> WorkspaceSpec </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">struct</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    UserID            </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token plain">   </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"userId"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    UserEmail         </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token plain">   </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"userEmail,omitempty"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    UserCheckSum      </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token plain">   </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"userCheckSum,omitempty"`</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// generated by the controller</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    Guests            </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"guests,omitempty"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    Namespace         </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token plain">   </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"namespace"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    TemplateConfigMap </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token plain">   </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"templateConfigMap,omitempty"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">type</span><span class="token plain"> WorkspaceStatus </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">struct</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    Phase               WorkspacePhase    </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"phase"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    ObservedGeneration  </span><span class="token builtin" style="color:rgb(189, 147, 249)">int64</span><span class="token plain">             </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"observedGeneration,omitempty"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    ManagedResources    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain">ManagedResource </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"managedResources,omitempty"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    LastTemplateVersion </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token plain">            </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"lastTemplateVersion,omitempty"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    PendingDeletions    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain">PendingDeletion </span><span class="token string" style="color:rgb(255, 121, 198)">`json:"pendingDeletions,omitempty"`</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></div></code></pre></div></div>
<p><code>managedResources</code> is the list of resources the operator created, keyed by group,
version, kind, namespace and name. <code>lastTemplateVersion</code> is the ConfigMap
<code>resourceVersion</code> that was applied. <code>pendingDeletions</code> is the retry queue for resources
that were removed from the template but not yet deleted.</p>
<p>Putting all of that in status is deliberate. The operator keeps no durable state of its
own; a pod restart loses nothing. The CRD is the tenant record, and the namespace is the
isolation boundary.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-loop">The loop<a href="https://blog.djovap.com/multi-tenant-workspace-operator#the-loop" class="hash-link" aria-label="Direct link to The loop" title="Direct link to The loop" translate="no">​</a></h2>
<p>The controller is a conventional informer-and-workqueue design, with three event sources:</p>
<ul>
<li class="">a <strong>Workspace informer</strong>, scoped to the namespace that holds the CRs;</li>
<li class="">a <strong>ConfigMap informer</strong>, scoped to the namespace that holds the templates;</li>
<li class="">a <strong>dynamic resource watcher</strong>, watching everything labelled as owned by the controller,
across all namespaces, for drift.</li>
</ul>
<!-- -->
<p>The queue is rate limited twice over: an exponential failure backoff from 5 milliseconds
to 1000 seconds, and a token bucket at 50 items per second with a burst of 300. The
exponential part protects a single broken workspace from hot-looping. The bucket protects
the API server when a shared event fans out to everyone.</p>
<p>The most important optimisation is the one that does nothing. If a workspace is <code>Active</code>,
its <code>generation</code> has not changed, there is no drift annotation, and its template version
is current, the reconciler returns without a single API call. In steady state that skip
path removes the vast majority of reconciliation work.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="one-template-many-tenants">One template, many tenants<a href="https://blog.djovap.com/multi-tenant-workspace-operator#one-template-many-tenants" class="hash-link" aria-label="Direct link to One template, many tenants" title="Direct link to One template, many tenants" translate="no">​</a></h2>
<p>The workspace envelope is defined once, in a ConfigMap. That ConfigMap is part of the
platform chart, so it is managed in Git and delivered by whatever GitOps tool the cluster
uses.</p>
<p>The template contains two layers of templating. The outer layer is Helm, evaluated when
the platform is deployed. The inner layer is a Go template, evaluated by the operator for
each workspace. The platform team controls the <strong>shape</strong>; the operator fills in the
<strong>values</strong> from the CR.</p>
<div class="language-yaml codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-yaml codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)"># workspace-resource-template (rendered by Helm, then by the operator)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token key atrule">data</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token key atrule">namespace.yaml</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">|</span><span class="token scalar string" style="color:rgb(255, 121, 198)"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token scalar string" style="color:rgb(255, 121, 198)">    apiVersion: v1</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token scalar string" style="color:rgb(255, 121, 198)">    kind: Namespace</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token scalar string" style="color:rgb(255, 121, 198)">    metadata:</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token scalar string" style="color:rgb(255, 121, 198)">      name: {{ "{{" }} .Workspace.Namespace {{ "}}" }}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token scalar string" style="color:rgb(255, 121, 198)">      labels:</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token scalar string" style="color:rgb(255, 121, 198)">        meta.kappsul.io/user-ns: "yes"</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token scalar string" style="color:rgb(255, 121, 198)">        meta.kappsul.io/user-email: "{{ "{{" }} .Workspace.UserEmail | replace "@" "_at_" {{ "}}" }}"</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token scalar string" style="color:rgb(255, 121, 198)">        meta.kappsul.io/user-checksum: "{{ "{{" }} .Workspace.UserCheckSum {{ "}}" }}"</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token scalar string" style="color:rgb(255, 121, 198)">        pod-security.kubernetes.io/enforce: baseline</span><br></div></code></pre></div></div>
<p>The same ConfigMap also defines the quota, the limit range, the RBAC bindings, and the
network policy. That is the "one config" in the title: a single Git-managed object
describes the envelope for every tenant.</p>
<p>The operator also uses the template for <strong>discovery</strong>. It does not hard-code a list of
resource types to watch; it scans the template ConfigMaps, extracts the <code>apiVersion</code> and
<code>kind</code> from each document, and registers a dynamic informer for the resulting GVRs. The
set of watched kinds is derived at startup, so adding a new kind to the template requires
a controller restart before its informer is registered.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="applying-server-side-apply-in-dependency-order">Applying: server-side apply, in dependency order<a href="https://blog.djovap.com/multi-tenant-workspace-operator#applying-server-side-apply-in-dependency-order" class="hash-link" aria-label="Direct link to Applying: server-side apply, in dependency order" title="Direct link to Applying: server-side apply, in dependency order" translate="no">​</a></h2>
<p>Rendering produces a map of YAML documents. The operator parses them into unstructured
objects, sorts them by kind — Namespace first, then quota, limit range, service account,
roles, role bindings, ConfigMaps, secrets, and network policies — and applies each one
with server-side apply.</p>
<div class="language-go codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-go codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token plain">labels</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"app.kubernetes.io/managed-by"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"workspace-controller"</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">labels</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"workspace.kappsul.io/name"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> workspace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Name</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">labels</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token string" style="color:rgb(255, 121, 198)">"workspace.kappsul.io/uid"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">string</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">workspace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">UID</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// ...and then:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">options </span><span class="token operator">:=</span><span class="token plain"> metav1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">PatchOptions</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    FieldManager</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"workspace-controller"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    Force</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain">        </span><span class="token function" style="color:rgb(80, 250, 123)">boolPtr</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token boolean">true</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token boolean">_</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> err </span><span class="token operator">:=</span><span class="token plain"> resourceClient</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Patch</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> resource</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">GetName</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    types</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">ApplyPatchType</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> data</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> options</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>Server-side apply is the right primitive here for three reasons. It is idempotent, so a
retry is always safe. It avoids read-modify-write races. And it records <strong>field
ownership</strong>: the API server knows that <code>workspace-controller</code> owns these fields. The
<code>Force</code> flag makes the operator win when someone else has touched a field it owns, which
is exactly what self-heal needs.</p>
<p>The applier adds three labels and one annotation to every resource. The labels identify
the owning workspace; the annotation stores a hash of the rendered <code>spec</code> and <code>data</code>. That
hash is useful for bookkeeping and debugging, but it is not what drives drift detection.
A hash can tell you that something changed. It cannot tell you <strong>who</strong> changed it.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="self-heal-who-wrote-this-field">Self-heal: who wrote this field?<a href="https://blog.djovap.com/multi-tenant-workspace-operator#self-heal-who-wrote-this-field" class="hash-link" aria-label="Direct link to Self-heal: who wrote this field?" title="Direct link to Self-heal: who wrote this field?" translate="no">​</a></h2>
<p>This is the first behaviour we needed from the GitOps toolbox.</p>
<p>The student has <code>admin</code> inside their namespace. That is intentional: they need to deploy
applications, create services, manage their own resources. But the same permission lets
them edit the quota, the limit range, the RBAC bindings, and the network policy. Those are
platform-owned invariants, not student-owned resources.</p>
<p>The hard question is how to tell a student's edit apart from the operator's own write. A
hash comparison tells you that the object no longer matches the last thing you wrote —
but your own reconciliation changes it too, so the next event looks exactly like drift.
And re-applying on every event makes it worse: your own writes trigger more events.</p>
<p>The answer is <code>managedFields</code>. Every write records a field manager. The drift watcher
looks at the most recent entry and asks a simple question: was this <code>workspace-controller</code>
or someone else?</p>
<div class="language-go codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-go codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token plain">lastModifier </span><span class="token operator">:=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">getLastModifier</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">newObj</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">GetManagedFields</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> lastModifier </span><span class="token operator">==</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"workspace-controller"</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token comment" style="color:rgb(98, 114, 164)">// Our own reconciliation. Not drift.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// Someone else changed a resource we own.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token function" style="color:rgb(80, 250, 123)">setDriftAnnotation</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">workspaceName</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token function" style="color:rgb(80, 250, 123)">enqueueWorkspace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">workspaceName</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><br></div></code></pre></div></div>
<p>The reconciler sees the annotation, force-applies the desired state, and removes the
annotation. The student's change disappears. The cycle is intentional and idempotent.</p>
<!-- -->
<p>There is an important nuance here. <strong>Self-heal is a corrective control, not the security
boundary.</strong> The security boundary is the namespace, RBAC, and NetworkPolicy: they decide
what a student can reach. Self-heal protects the invariants <em>inside</em> the tenant's
namespace, where the student is legitimately an admin. It is the difference between "you
are allowed to edit this" and "this value is not yours to change".</p>
<p>It is also a heuristic, not a proof. An actor that writes with a manager name of
<code>workspace-controller</code> would be indistinguishable from us. Our threat model is a student
with <code>kubectl</code>, not a compromised controller identity.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="propagation-one-config-every-workspace">Propagation: one config, every workspace<a href="https://blog.djovap.com/multi-tenant-workspace-operator#propagation-one-config-every-workspace" class="hash-link" aria-label="Direct link to Propagation: one config, every workspace" title="Direct link to Propagation: one config, every workspace" translate="no">​</a></h2>
<p>This is the second behaviour we needed.</p>
<p>Changing the template is the highest-leverage operation in the system. We want it to be
fast, visible, and rate-limited.</p>
<p>The ConfigMap informer stores the new <code>resourceVersion</code> in an in-memory cache and lists
the workspaces from the informer cache — not from the API server. For each workspace that
uses this template, it adds one item to the work queue. That is cheap. The queue, not the
event handler, is the buffer.</p>
<div class="language-go codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-go codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">func</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">c </span><span class="token operator">*</span><span class="token plain">WorkspaceController</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">handleConfigMapChange</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">obj </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">interface</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    cm </span><span class="token operator">:=</span><span class="token plain"> obj</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token operator">*</span><span class="token plain">corev1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">ConfigMap</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">templateVersionCache</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Store</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">cm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Name</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> cm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">ResourceVersion</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    workspaces</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">_</span><span class="token plain"> </span><span class="token operator">:=</span><span class="token plain"> c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">workspacesLister</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token function" style="color:rgb(80, 250, 123)">Workspaces</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">WorkspacesNamespace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token function" style="color:rgb(80, 250, 123)">List</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">labels</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Everything</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> </span><span class="token boolean">_</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> ws </span><span class="token operator">:=</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">range</span><span class="token plain"> workspaces </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        templateName </span><span class="token operator">:=</span><span class="token plain"> ws</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Spec</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">TemplateConfigMap</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> templateName </span><span class="token operator">==</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">""</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            templateName </span><span class="token operator">=</span><span class="token plain"> c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">DefaultTemplateConfigMap</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> templateName </span><span class="token operator">==</span><span class="token plain"> cm</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Name </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">enqueueWorkspace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ws</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// one queue item; no API call here</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></div></code></pre></div></div>
<p>The reconciler then compares <code>status.lastTemplateVersion</code> with the cached version. If they
differ, it reconciles and records the new version. At 50 workspaces per second, ten
thousand workspaces converge in roughly three minutes.</p>
<!-- -->
<p>This is the GitOps behaviour we wanted: one configuration in Git, propagated to every
tenant. No per-tenant Application, no sync loop per workspace. The operator is the
fan-out engine.</p>
<p>The lesson is not "fan out faster". It is "fan out into a queue". A loop that calls the
API server ten thousand times is a denial-of-service attack you wrote yourself. A loop
that enqueues ten thousand items and lets a rate-limited worker pool drain them is a
rollout.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="lifecycle-create-and-delete">Lifecycle: create and delete<a href="https://blog.djovap.com/multi-tenant-workspace-operator#lifecycle-create-and-delete" class="hash-link" aria-label="Direct link to Lifecycle: create and delete" title="Direct link to Lifecycle: create and delete" translate="no">​</a></h2>
<p>Creation and deletion are plumbing around the two behaviours, but they still need care.</p>
<p><strong>Creation.</strong> The operator adds a finalizer, generates a <code>userCheckSum</code> if it is missing,
renders the template, applies the resources, and marks the workspace <code>Active</code>. The
checksum and finalizer follow a "one step per pass" pattern: write, return, and let the
update event bring the workspace back. Reconcile is a sequence of observations, not a
single function call.</p>
<p><strong>Deletion.</strong> There are two paths. Resources removed from the template are diffed against
<code>managedResources</code>, added to <code>status.pendingDeletions</code>, and deleted asynchronously at a
conservative five deletions per second with twenty retries and exponential backoff. The
namespace itself is deleted with foreground propagation; the finalizer keeps the Workspace
CR around until the namespace is gone.</p>
<p>The Namespace is explicitly excluded from the template diff. Deleting a namespace is the
workspace-deletion path's job, not a template-diff side effect.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="protecting-the-control-plane">Protecting the control plane<a href="https://blog.djovap.com/multi-tenant-workspace-operator#protecting-the-control-plane" class="hash-link" aria-label="Direct link to Protecting the control plane" title="Direct link to Protecting the control plane" translate="no">​</a></h2>
<p>Everything above is a lot of writes. The design is shaped as much by what we refuse to do
as by what we do.</p>
<table><thead><tr><th>Boundary</th><th>Setting</th><th>Why</th></tr></thead><tbody><tr><td>Informer resync</td><td>30s</td><td>safety net for missed events</td></tr><tr><td>Reconcile queue</td><td>50 QPS, burst 300</td><td>fair progress during a template rollout</td></tr><tr><td>Deletion queue</td><td>5 QPS, burst 10, 1 worker</td><td>a deletion storm is worse than a slow delete</td></tr><tr><td>Status updates</td><td>50 QPS, batched every 30s</td><td>etcd writes are the expensive ones</td></tr><tr><td>Kubernetes client</td><td>50 QPS, burst 300</td><td>protect the API server from us</td></tr><tr><td>Deletion retries</td><td>20, 5ms to 1000s</td><td>eventual success without hot loops</td></tr></tbody></table>
<p>The operator runs as a <strong>single replica</strong>, and that is deliberate. It does not implement
leader election. Running two replicas would mean two reconcilers writing the same status,
two sets of duplicate API calls, and a class of conflicts that optimistic locking can only
paper over. Instead we use a PodDisruptionBudget with <code>maxUnavailable: 0</code> and a high
priority class so that maintenance does not casually evict it. A pod restart costs ten to
thirty seconds of control-plane work; the resync and the queue make that a non-event.</p>
<p>If you need true high availability, the answer is leader election with a
<code>coordination.k8s.io</code> Lease, not more replicas behind a Service. We have not needed it yet.
The operator is not on the real-time path, and the work is idempotent.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-bill">The bill<a href="https://blog.djovap.com/multi-tenant-workspace-operator#the-bill" class="hash-link" aria-label="Direct link to The bill" title="Direct link to The bill" translate="no">​</a></h2>
<p>This design is not free. Here is what we pay for it.</p>
<ul>
<li class=""><strong>Self-heal is corrective, not preventive.</strong> A student can still make the change; the
operator reverts it. If you need to prevent the write entirely, use RBAC and admission
policies. Self-heal is the safety net for the resources the student is allowed to touch.</li>
<li class=""><strong>The template is a shared blast radius.</strong> A bad template change reaches every workspace
at fifty per second. It is also one line in Git, which makes it look harmless. Treat a
template change like a production rollout: stage it, watch the first workspaces, and
have a rollback ready.</li>
<li class=""><strong>Two template layers are powerful and fiddly.</strong> The Helm escaping is ugly, and a
missing pair of braces fails at render time, not at authoring time. The alternative —
raw manifests baked into the operator image — loses the GitOps boundary. We chose the
flexibility and accepted the syntax.</li>
<li class=""><strong>Drift detection trusts <code>managedFields</code>.</strong> It is a very good signal, but it is a
signal, not an authorization system. An actor that can write with our field manager name
can evade it.</li>
<li class=""><strong>You own the operator.</strong> That is the portability trade-off. You no longer depend on
ArgoCD in the cluster, but you now maintain the controller, its CRD, and its failure
modes.</li>
<li class=""><strong>There is no per-tenant Git audit trail.</strong> The CR status is the state, not a commit
per workspace. If you need an audit trail of every tenant change, you need to add one.</li>
</ul>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="takeaways">Takeaways<a href="https://blog.djovap.com/multi-tenant-workspace-operator#takeaways" class="hash-link" aria-label="Direct link to Takeaways" title="Direct link to Takeaways" translate="no">​</a></h2>
<ul>
<li class=""><strong>Start with the isolation model.</strong> A workspace is a namespace plus quota, limit range,
RBAC, network policy, and pod security. The operator exists to make that envelope
consistent.</li>
<li class=""><strong>Keep the runtime portable.</strong> Do not make an external tool a runtime dependency for a
core product operation. GitOps for delivery, an operator for runtime.</li>
<li class=""><strong>Take only the GitOps behaviours you need.</strong> Self-heal and propagation were enough.
You do not need a full sync engine to get them.</li>
<li class=""><strong>Use server-side apply.</strong> Idempotent writes, no read-modify-write races, and field
ownership that means something.</li>
<li class=""><strong>Detect drift by who, not what.</strong> <code>managedFields</code> tells you whether the change came
from you or from someone else; a hash cannot.</li>
<li class=""><strong>Fan out into a queue, not into API calls.</strong> The queue is the buffer that turns a
large template change into a rollout.</li>
<li class=""><strong>Separate the security boundary from the corrective control.</strong> RBAC and NetworkPolicy
decide what a student can reach. Self-heal protects the invariants inside their
namespace.</li>
</ul>]]></content:encoded>
            <category>Kubernetes</category>
            <category>GitOps</category>
            <category>Distributed systems</category>
        </item>
        <item>
            <title><![CDATA[The migration that deleted every workspace: a Kubernetes war story]]></title>
            <link>https://blog.djovap.com/the-migration-that-deleted-every-workspace</link>
            <guid>https://blog.djovap.com/the-migration-that-deleted-every-workspace</guid>
            <pubDate>Mon, 02 Nov 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[We migrated our student workspaces from ArgoCD Applications to a custom operator. The rehearsal passed, production ran green, and by the end of the run every workspace created after the first two hundred had been wiped — silently. This is the full story: the mechanics of the failure, the weekend of restores, and what we now do differently.]]></description>
            <content:encoded><![CDATA[<p>Kappsul is a platform we built at the University of Lorraine: from a web interface, any
student can get a private, isolated environment on a shared Kubernetes cluster and deploy
their own applications in it. This is the story of the day we switched how those
environments are managed — a migration that ran green from start to finish, and deleted
every workspace created after the first two hundred, along with everything inside them.</p>
<p>It is also a story with a happy ending, and the ending has a name: Velero.</p>
<!-- -->
<p>If you have read <a class="" href="https://blog.djovap.com/multi-tenant-workspace-operator">Multi-tenant Kubernetes workspaces: isolation, self-heal, and one
config</a>, you know the destination of this migration and
why we chose it. This post is the part of the story that article deliberately skips: the
day we switched over, and the two hours during which everything went wrong. It is
written to be read on its own.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="what-kappsul-is-and-why-isolation-is-the-product">What Kappsul is, and why isolation is the product<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#what-kappsul-is-and-why-isolation-is-the-product" class="hash-link" aria-label="Direct link to What Kappsul is, and why isolation is the product" title="Direct link to What Kappsul is, and why isolation is the product" translate="no">​</a></h2>
<p>Universities have a specific Kubernetes problem. Courses and student projects need
somewhere to run an application — a web app, an API, a database — but you cannot give
every student their own cluster, and you cannot give untrusted users access to a shared
one. So the platform does something in between: a student clicks a button, and gets a
<strong>namespace</strong> on the shared cluster, with everything configured so they can work safely
inside it.</p>
<p>That sounds trivial. It is the whole product. The hard part is not creating a namespace —
<code>kubectl create namespace</code> is one command. The hard part is the <strong>contract</strong>:</p>
<blockquote>
<p>A private namespace with a hard boundary, and enough freedom inside it to be useful.</p>
</blockquote>
<p>"Enough freedom" means the student is <code>admin</code> inside their namespace: they can deploy
with Helm, write manifests, expose services, manage their own secrets. "Hard boundary"
means none of that freedom can leak out — onto other students, or onto the platform
itself. In practice, every workspace is an envelope of Kubernetes primitives:</p>
<table><thead><tr><th>Layer</th><th>What the student gets</th><th>What the platform is protected from</th></tr></thead><tbody><tr><td>Namespace</td><td>A private place to deploy</td><td>Name collisions, cross-tenant access</td></tr><tr><td>ResourceQuota</td><td>A predictable CPU/memory/storage budget</td><td>One workspace starving the whole cluster</td></tr><tr><td>LimitRange</td><td>Default requests and limits on containers</td><td>Pods without limits slipping through</td></tr><tr><td>RBAC</td><td><code>admin</code> inside their own namespace only</td><td>Students acting outside their workspace</td></tr><tr><td>NetworkPolicy</td><td>Default-deny, explicit ingress/egress</td><td>Cross-namespace traffic, unexpected egress</td></tr><tr><td>Pod Security Admission</td><td><code>baseline</code> enforcement</td><td>Privileged or unsafe pods</td></tr></tbody></table>
<p>One more thing matters for this story: <strong>the tenants are students</strong>. They are learning
Kubernetes. They will delete things, edit things, break things — sometimes on purpose,
just to see what happens. The platform cannot prevent every mistake inside the
workspace; it has to <em>put the workspace back</em> when it happens. Remember that: it is why
self-healing was a requirement, and it is how a deletion tool ended up holding a
loaded weapon.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-argocd-era-a-good-tool-used-well">The ArgoCD era: a good tool, used well<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#the-argocd-era-a-good-tool-used-well" class="hash-link" aria-label="Direct link to The ArgoCD era: a good tool, used well" title="Direct link to The ArgoCD era: a good tool, used well" translate="no">​</a></h2>
<p>For the first year, the lifecycle of every workspace was managed by <strong>ArgoCD</strong>. Creating
a workspace meant creating an ArgoCD <code>Application</code> — one per workspace — pointing at a
Git repository that contained a small Helm chart: the envelope from the table above.
ArgoCD watched Git, rendered the chart, and applied the resources into the student's
namespace. Change the chart in Git, and every workspace converged to the new state.</p>
<!-- -->
<p>I want to be fair to this design, because it was <strong>good</strong>:</p>
<ul>
<li class=""><strong>Declarative and auditable.</strong> Every workspace's desired state lived in Git.</li>
<li class=""><strong>Self-healing for free.</strong> ArgoCD's whole job is noticing drift and correcting it —
a student edits the quota, ArgoCD puts it back. We got our hardest requirement from a
tool we didn't have to write.</li>
<li class=""><strong>Battle-tested.</strong> Reconciliation, retries, sync semantics: solved problems, not our
problems.</li>
<li class=""><strong>Zero custom controller code.</strong> Nothing to maintain, nothing to debug at 2 a.m.</li>
</ul>
<p>So why did we leave? Four reasons, in increasing order of importance:</p>
<ol>
<li class=""><strong>It sat on the critical path.</strong> Creating a workspace — the most basic operation of
the product — required ArgoCD <em>and</em> GitLab to be up. When either had an outage,
students could not get workspaces at all. A dependency that had nothing to do with
the product could take the product down.</li>
<li class=""><strong>It killed portability.</strong> The platform could not run on a cluster without ArgoCD and
a Git server. We wanted Kappsul to run on any Kubernetes cluster.</li>
<li class=""><strong>It was overkill.</strong> We were using a full delivery platform to reconcile six
resources per tenant. No sync dashboards per workspace, no app-of-apps, no
multi-cluster — none of that was ever going to be used.</li>
<li class=""><strong>Scale mismatch.</strong> Thousands of tiny Applications, each refreshed forever by the
ArgoCD controller, for what was ultimately a namespace with four policy objects in
it.</li>
</ol>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-destination-one-crd-one-operator-two-behaviors">The destination: one CRD, one operator, two behaviors<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#the-destination-one-crd-one-operator-two-behaviors" class="hash-link" aria-label="Direct link to The destination: one CRD, one operator, two behaviors" title="Direct link to The destination: one CRD, one operator, two behaviors" translate="no">​</a></h2>
<p>The replacement is deliberately small. A <code>Workspace</code> custom resource is the tenant
record — who owns it, which namespace, which guests. A small operator watches those CRs
and does exactly two things, which turn out to be the only two things we ever needed
from the GitOps toolbox:</p>
<ul>
<li class=""><strong>Self-heal</strong>: if a student edits a platform-owned resource, put it back — detected
not by comparing hashes, but by asking <code>managedFields</code> <em>who</em> made the last write;</li>
<li class=""><strong>Propagation</strong>: the envelope is defined once, in a Git-managed template ConfigMap;
change it once, and the operator fans it out to every workspace.</li>
</ul>
<p>GitOps stays for <strong>delivery</strong> — the template lives in Git and reaches the cluster by
whatever tool the cluster uses. The <strong>runtime</strong> needs nothing but the Kubernetes API and
the CRD. That is the whole argument: <em>GitOps for delivery, an operator for runtime</em> —
and it is covered in depth in <a class="" href="https://blog.djovap.com/multi-tenant-workspace-operator">the operator article</a>.</p>
<!-- -->
<p>What the diagram does not show is the part that matters for the rest of this story:
<strong>deletion</strong>. When a Workspace CR is deleted, the operator deletes the student's
namespace — foreground propagation, finalizer held until the namespace is gone. The
operator owns the namespace lifecycle now. Remember that too.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-plan-a-migration-job">The plan: a migration job<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#the-plan-a-migration-job" class="hash-link" aria-label="Direct link to The plan: a migration job" title="Direct link to The plan: a migration job" translate="no">​</a></h2>
<p>So: several thousand live workspaces, each currently an ArgoCD Application, had to
become Workspace CRs. No downtime, no data loss, no "we'll fix it as we go". The
operator was ready; the switch was the problem.</p>
<p>Two properties of the old world shaped the whole design, and you need both to
understand the failure:</p>
<p><strong>Fact 1 — the cascade finalizer.</strong> Kubernetes finalizers are strings on an object that
mean <em>"do not actually delete me until the controller that owns this string says so"</em>.
Every workspace Application carried
<code>resources-finalizer.argocd.argoproj.io</code>, which is ArgoCD's promise: <em>when this
Application is deleted, delete everything it ever created</em> — the quota, the RBAC, the
network policies, <strong>and the namespace itself</strong>. Deleting a namespace deletes everything
inside it: every student Deployment, Service, ConfigMap, Secret, and every PVC with its
data. That finalizer is why the migration could not just delete the Applications and
hope.</p>
<p><strong>Fact 2 — the Applications were alive.</strong> ArgoCD's application controller refreshes
every Application on its reconciliation cycle, rewriting status fields each pass. Every
one of those writes bumps the object's <code>resourceVersion</code> — the concurrency token that
Kubernetes uses to reject writes based on stale data.</p>
<p>The migration job's logic, per Application:</p>
<ol>
<li class=""><strong>Convert</strong> the ArgoCD Application into a <code>Workspace</code> CR (same name, same target
namespace, user and guests extracted from the Helm values);</li>
<li class=""><strong>Create</strong> the Workspace CR and <strong>wait</strong> for the operator to reconcile it — poll
until the workspace reports <code>Active</code> with all envelope resources created;</li>
<li class=""><strong>Strip the cascade finalizer</strong> from the Application, so that deleting it deletes
only the Application object;</li>
<li class=""><strong>Delete</strong> the ArgoCD Application.</li>
</ol>
<p>The student's own deployments, services and volumes were never ArgoCD's business — they
were installed by our application service through Helm, directly in the namespace. On
paper, the handover was clean: the operator takes over the envelope, the student's stuff
was never ArgoCD's to delete.</p>
<!-- -->
<p>And the job itself was genuinely careful. It processed Applications in batches with
delays between them, rate-limited its API calls, tracked progress in a file on a
persistent volume so it could resume, supported a dry-run mode, and refused to delete an
Application before its Workspace CR was fully reconciled. We were proud of it. That is
not sarcasm — every one of those decisions was correct. The bug was elsewhere.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-rehearsal">The rehearsal<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#the-rehearsal" class="hash-link" aria-label="Direct link to The rehearsal" title="Direct link to The rehearsal" translate="no">​</a></h2>
<p>We tested the job in preprod first. Preprod had a handful of workspaces.</p>
<p>The run took minutes. Every Application was converted, every Workspace CR reconciled
almost instantly, every finalizer stripped, every Application deleted. We compared
namespaces before and after: identical, envelope and all. We let the operator run for a
while, checked the workspaces were self-healing, and called it done.</p>
<p>It <em>was</em> done — for a platform the size of preprod. What we had actually tested was the
code. What we had not tested was the clock. Hold that thought.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="production-thursday">Production, Thursday<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#production-thursday" class="hash-link" aria-label="Direct link to Production, Thursday" title="Direct link to Production, Thursday" translate="no">​</a></h2>
<p>In early December 2025, we ran the migration against production. The configuration was
the one designed for our scale: all workspaces (<code>WORKSPACES=*</code>), deletion enabled
(<code>DELETE_ARGOCD=true</code>), batches of 25 Applications with a 180-second pause between
batches, five concurrent migrations, and a 15-minute timeout for the operator to
reconcile each Workspace.</p>
<p>The run started green. The first Applications converted, reconciled, and deleted exactly
like in preprod. The job churned through batch after batch, logging a steady stream of
checkmarks.</p>
<p>The first two hundred or so workspaces migrated perfectly.</p>
<p>Then, without anything changing — no error, no restart, no threshold crossed — the
workspaces behind the checkmarks started to die.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-race-we-didnt-know-we-were-running">The race we didn't know we were running<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#the-race-we-didnt-know-we-were-running" class="hash-link" aria-label="Direct link to The race we didn't know we were running" title="Direct link to The race we didn't know we were running" translate="no">​</a></h2>
<p>Here is the mechanism, and it is embarrassingly simple once you see it.</p>
<p>The job fetched <strong>all</strong> Applications in a single <code>LIST</code> at the very beginning of the
run. From that moment on, every Application object it held in memory started aging.
Meanwhile, ArgoCD's application controller kept doing its job, refreshing every
Application on its cycle and bumping each object's <code>resourceVersion</code> as it rewrote
status.</p>
<p>For each Application, the job then:</p>
<ol>
<li class="">created the Workspace CR,</li>
<li class="">waited — up to fifteen minutes — for the operator to reconcile it,</li>
<li class="">and only then tried to strip the cascade finalizer, using a <strong>full-object <code>UPDATE</code></strong>
of the object it had been holding since the start of the run.</li>
</ol>
<p>A full-object <code>UPDATE</code> is a read-modify-write with optimistic concurrency: the write
carries the <code>resourceVersion</code> you read, and if the live object has moved on since, the
API server rejects it with a <code>409 Conflict</code>. By the time the job reached an Application —
after its Workspace CR had been created and reconciled, after the batches and the delays
— the object it was holding was <strong>minutes to hours old</strong>. ArgoCD had rewritten it many
times over.</p>
<div class="language-go codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-go codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)">// the strip step, reduced to its essence</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">finalizers</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">_</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">_</span><span class="token plain"> </span><span class="token operator">:=</span><span class="token plain"> unstructured</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">NestedStringSlice</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">app</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Object</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"metadata"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"finalizers"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// ...remove the ArgoCD finalizer from the in-memory copy...</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token boolean">_</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> err </span><span class="token operator">:=</span><span class="token plain"> dynamicClient</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Resource</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">applicationsGVR</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token function" style="color:rgb(80, 250, 123)">Namespace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">app</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">GetNamespace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token function" style="color:rgb(80, 250, 123)">Update</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> app</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> metav1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">UpdateOptions</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// full-object write of a stale object</span><br></div></code></pre></div></div>
<p>For the first Applications, processed while the run was young, the in-memory object was
still fresh enough that the write won the race. The finalizer was stripped, the
Application was deleted cleanly, and the operator took over a fully intact namespace.</p>
<p>For every Application after that, the write lost the race. The API server rejected it.
The finalizer survived. And the job — which treated a failed strip as a reason to log
and move on, not to stop — deleted the Application anyway.</p>
<!-- -->
<p>And the cascade finalizer was a promise. ArgoCD deleted every resource that Application
had ever created — the ResourceQuota, the LimitRange, the NetworkPolicies, the RBAC
bindings, and the <strong>namespace itself</strong>. Deleting a namespace deletes everything in it:
every student Deployment, StatefulSet, Service, ConfigMap, Secret, and every PVC with
its data.</p>
<p>The staleness curve explains the boundary we observed. Batch <em>k</em> ran at roughly
<code>T0 + k × (reconcile wait + batch delay)</code>. The probability that an Application had been
touched by ArgoCD since <code>T0</code> climbs from "possible" to "certain" over the first couple
of hours. Two hundred workspaces in, the job had crossed from "usually wins the race" to
"always loses it". Nothing in the code changed at that point. Only the clock moved.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-silence">The silence<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#the-silence" class="hash-link" aria-label="Direct link to The silence" title="Direct link to The silence" translate="no">​</a></h2>
<p>Here is the part I still find the most unsettling: <strong>nothing failed</strong>.</p>
<p>The migration job reported success, because from where it stood, everything had
succeeded. The Workspace CR was created — true. The Application was deleted — also
true. The job finished, printed its summary, and exited zero.</p>
<p>ArgoCD reported nothing, because deleting the resources behind an Application with the
cascade finalizer is not an error — it is the contract. ArgoCD was not even misbehaving
that day: somewhere in the cluster, a controller was faithfully honoring a promise we
had made a year earlier and forgotten.</p>
<p>Our operator reported nothing either. Its reconciliation loop saw namespaces that no
longer existed, and did exactly what it is designed to do: it recreated the envelope —
namespace, quota, limit range, network policies, RBAC — with server-side apply. The
platform looked <em>alive</em>. Workspaces were up, reachable, and hollow.</p>
<p>And our logs? The run produced no warnings that reached anyone. The one component that
could have flagged the failure mode — the strip step — was written to survive its own
guard failing: log, and continue. A guard that does not stop the run is not a guard; it
is a disclaimer. The post-mortem found it in the code within minutes. Finding it in the
logs, after the fact, among a million green lines, was another matter.</p>
<p>The first reports came from users: workspaces that were up, reachable, and empty.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-weekend">The weekend<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#the-weekend" class="hash-link" aria-label="Direct link to The weekend" title="Direct link to The weekend" translate="no">​</a></h2>
<p>This is the part of the story where I get to be grateful instead of clever.</p>
<p>We had Velero backups. Of everything — Kubernetes objects and volume data. Not because
we are prophets, but because "the cluster eats a namespace" had been on our risk list
since day one, and Velero was the answer we had set up and then mostly stopped thinking
about.</p>
<p>The team spent the weekend restoring. Every workspace namespace, every object, every PVC
and the data inside it, back to the state they were in before the run. It was long,
unglamorous work: checking restore order, reconciling what belonged to what, verifying
that each workspace came back whole. But it was <em>possible</em>, and by Monday the platform
was back, and the incident became a story instead of an ending.</p>
<!-- -->
<p>Two things made the difference:</p>
<ul>
<li class=""><strong>The backups covered the right things.</strong> Kubernetes objects <em>and</em> persistent volume
data. A backup of one without the other would have restored the letter and lost the
spirit.</li>
<li class=""><strong>Nobody had to improvise.</strong> The restore procedure existed, the tool was installed,
the backups were there. The weekend was spent executing, not designing.</li>
</ul>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-fix-we-almost-didnt-need">The fix we almost didn't need<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#the-fix-we-almost-didnt-need" class="hash-link" aria-label="Direct link to The fix we almost didn't need" title="Direct link to The fix we almost didn't need" translate="no">​</a></h2>
<p>For completeness: the job did get fixed. The strip step was replaced by a delete that
asks Kubernetes to orphan dependents, and — more importantly — the job learned to count.
Before deleting an Application it counted the Deployments, StatefulSets and Services in
the target namespace; after the delete, it counted again, and hard-failed the entry if
the number dropped. A canary per namespace, cheap and brutal.</p>
<p>It did not matter much, because by then there was nothing left to migrate: the cascade
had done the conversion in the most literal sense. Every workspace <em>was</em> managed by the
operator now. That is the darkest line in this story, and the truest: <strong>the migration
succeeded. We had just planned for it to complete by conversion, not by deletion.</strong></p>
<p>A few days later we deleted the migration job from the repository entirely. New
workspaces are born as Workspace CRs. Nobody will ever run that job again, and the next
platform will never have to.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="what-we-learned">What we learned<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#what-we-learned" class="hash-link" aria-label="Direct link to What we learned" title="Direct link to What we learned" translate="no">​</a></h2>
<p><strong>1. Scale is a test dimension.</strong> We tested the job on a handful of workspaces and it
passed, because it was correct <em>for a handful of workspaces</em>. The failure mode only
exists at runtimes longer than the freshness window of the objects being migrated. The
fix to our test strategy: ramp deliberately — 10, then 100, then 1,000, then all — and
treat a passing run at one scale as evidence about <em>that</em> scale only.</p>
<p><strong>2. Time is a test dimension.</strong> Preprod was green because it was <em>small</em>: the whole run
fit inside the window where nothing had gone stale. Production ran for hours, and the
bug lives in the hours. Testing in real conditions does not mean testing on
production-sized data — it means testing with production-shaped <em>durations</em>, or
accepting that a time-dependent bug is invisible to a short run.</p>
<p><strong>3. Never read-modify-write stale state on a destructive path.</strong> The strip step held an
object for minutes-to-hours and then wrote the whole thing back. The correct patterns
are boring and well-known: patch the single field you care about, or re-read the object
and retry on conflict, or use server-side apply with a dedicated field manager. We knew
these. The operator itself is built on server-side apply precisely because it avoids
read-modify-write races. The migration job was not held to the same standard as the
thing it was migrating <em>to</em>. That asymmetry is the real root cause.</p>
<p><strong>4. A guard that doesn't stop the run is not a guard.</strong> The finalizer strip could fail,
log, and let the deletion proceed. On a path that ends in <code>DELETE</code>, every failed
precondition must abort — loudly, and ideally at the batch level, not the item level.
"Warn and continue" is a reasonable default for retries and a catastrophic one for
irreversible steps.</p>
<p><strong>5. Measure outcomes, not steps.</strong> The job verified its own actions: CR created,
Application deleted. Both true. Neither was the thing that mattered. The fixed job
counted resources in the namespace before and after — a canary that would have turned
the silent cascade into a hard failure within the first batch. Ten lines of code, had it
existed then.</p>
<p><strong>6. Finalizers are loaded weapons.</strong>
<code>resources-finalizer.argocd.argoproj.io</code> is not an implementation detail; it is a
standing order to destroy everything the object ever created, namespace included. Before
any migration that deletes objects, audit what each finalizer will cascade to. We had
written that order ourselves, a year earlier, and forgotten it was still standing.</p>
<p><strong>7. The most dangerous failure is every component working as designed.</strong> The job
succeeded. ArgoCD honored its contract. The operator healed the envelopes. Nobody lied,
nobody crashed, nothing alerted — and several thousand workspaces were gone. When every
layer reports success, you need a layer that measures <em>reality</em>, not intent.</p>
<p><strong>8. Backups are the difference between a story and an ending.</strong> Velero, covering
objects and volumes, with a restore procedure someone had actually thought through. The
weekend was long; without the backups it does not bear thinking about. Restore drills
are cheap insurance against the worst day of your quarter.</p>
<p><strong>9. Dangerous defaults outlive their context.</strong> The job's chart shipped with
<code>DELETE_ARGOCD=true</code> and <code>WORKSPACES=*</code> baked in — the configuration the incident ran
with. Every default on a destructive tool should be the safe one, with the dangerous
setting requiring an explicit, deliberate override.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="takeaways">Takeaways<a href="https://blog.djovap.com/the-migration-that-deleted-every-workspace#takeaways" class="hash-link" aria-label="Direct link to Takeaways" title="Direct link to Takeaways" translate="no">​</a></h2>
<p>The operator has been running the workspaces since — self-healing the envelopes,
propagating the template, doing the two GitOps behaviors we actually needed. It has
never deleted anything it should not have, partly because its design refuses
read-modify-write races, and partly because we now know exactly what our destructive
paths can cascade into.</p>
<p>The migration is the part of the story I would least like to repeat and the part I am
most glad is written down. If you are planning a migration between two management
systems, with thousands of live objects and a deletion step in the middle: test the
clock, not just the code. Patch, don't rewrite. Abort on guard failure. Count what
exists before and after. And keep your backups boring, complete, and rehearsed.</p>
<p>The alternative is a very quiet log stream, a green summary line, and a Monday morning
full of empty namespaces.</p>]]></content:encoded>
            <category>Kubernetes</category>
            <category>GitOps</category>
            <category>Building</category>
        </item>
        <item>
            <title><![CDATA[Kubernetes watchers at scale: one watch, thousands of dashboards]]></title>
            <link>https://blog.djovap.com/kubernetes-watcher-at-scale</link>
            <guid>https://blog.djovap.com/kubernetes-watcher-at-scale</guid>
            <pubDate>Wed, 28 Oct 2026 00:00:00 GMT</pubDate>
            <description><![CDATA[How we built a live, Kubernetes-backed interface for thousands of concurrent users without multiplying watches on the API server — scoped watches, Redis ownership, demand-driven leases, and event fan-out.]]></description>
            <content:encoded><![CDATA[<p>Kappsul is an educational platform built at the University of Lorraine. A student
picks a service from a catalog, gets a Kubernetes namespace, and a dashboard shows
what is running inside it. That dashboard is the product: it has to be live. It also
has to survive being open in three thousand tabs at the same time.</p>
<p>This is the story of the small distributed system we put between the browser and the
Kubernetes API so that those two statements could both be true.</p>
<p><img decoding="async" loading="lazy" alt="A container ship fully loaded with shipping containers" src="https://blog.djovap.com/assets/images/2026-10-28-kubernetes-watcher-at-scale-899d1a15e164a412abd7cd8dbbf4a4bf.webp" width="4988" height="3325" class="img_kvSm"></p>
<!-- -->
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-trap-a-session-is-a-terrible-unit-of-scale">The trap: a session is a terrible unit of scale<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#the-trap-a-session-is-a-terrible-unit-of-scale" class="hash-link" aria-label="Direct link to The trap: a session is a terrible unit of scale" title="Direct link to The trap: a session is a terrible unit of scale" translate="no">​</a></h2>
<p>"Live" sounds like a UI concern. In a Kubernetes-backed product it is an API server
concern, and the obvious implementations fail in two very different ways.</p>
<p><strong>Polling.</strong> Each dashboard asks the backend for the current state every few seconds.
With three thousand open dashboards and a five-second poll, that is roughly six hundred
requests per second before anyone clicks anything. Every response is a full read: list
the deployments, the statefulsets, the pods, the resource quota. The payload grows with
the number of objects, and the data is stale by definition. You have built a load
generator against your own control plane, and you still get to show users numbers that
are a few seconds wrong.</p>
<p><strong>One watch per browser.</strong> Kubernetes gives you a streaming API, so the reflex is to
open one stream per tab. A single dashboard needs more than one stream: deployments and
statefulsets for a workspace, or pods for a specific app, or namespaces for the admin
view. Three thousand users becomes six to nine thousand long-lived streams, each one a
connection the API server has to hold, buffer, and fan out to. The API server is a
shared, scarce resource: every stream it serves for your UI is capacity it cannot spend
running the cluster.</p>
<p>There is a third, subtler trap: running one watch per backend pod without coordination.
You scale the service to ten replicas, and now every replica opens its own watch for the
same namespace. The UI looks fine. The API server sees ten identical streams. Scaling
your own service multiplies load on the component you were trying to protect.</p>
<p>The mistake in all three cases is the same: <strong>we made the watch a property of the user.</strong>
A watch is a property of the data. If a thousand people are looking at the same
workspace, there is no reason for the API server to know about more than one of them.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="watch-101-in-one-page">Watch 101, in one page<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#watch-101-in-one-page" class="hash-link" aria-label="Direct link to Watch 101, in one page" title="Direct link to Watch 101, in one page" translate="no">​</a></h2>
<p>A Kubernetes watch is a streaming delta feed over a collection. You first list the
collection to get a <code>metadata.resourceVersion</code>, then you watch from that version. The
server sends <code>ADDED</code>, <code>MODIFIED</code>, <code>DELETED</code> and <code>ERROR</code> events; every object carries the
<code>resourceVersion</code> you need to resume. The watch is bounded by a timeout, and when the
server closes it — on timeout, on error, or because the underlying history expired — the
client is expected to rewind and watch again from the last version it saw.</p>
<p>Two details do most of the work in a design like ours:</p>
<ul>
<li class=""><strong>Selectors.</strong> A watch is only as expensive as the set it observes. Namespace scoping
and label selectors (<code>app.kubernetes.io/instance=my-app</code>,
<code>meta.kappsul.io/user-ns=yes</code>) cut the stream down to exactly the slice the UI needs.</li>
<li class=""><strong>Bounded duration.</strong> A watch that runs forever is a watch nobody re-evaluates. A
bounded watch is a natural checkpoint: when the window closes, you can ask whether
anyone still cares before opening the next one.</li>
</ul>
<p>If you have written a controller, you know the standard answer: an informer. One watch
per resource type per process, an in-memory cache, and handlers fanning out to whoever
needs the data. Informers are the right building block for a controller. They are not a
complete answer for a multi-tenant web app, because an informer is process-local. Ten
replicas still mean ten informers, and you do not want one informer per end user either.
The thing you actually need to coordinate is the <strong>watch</strong>, and the unit of coordination
is not the connection or the process. It is the scope.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-unit-of-sharing-a-scope">The unit of sharing: a scope<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#the-unit-of-sharing-a-scope" class="hash-link" aria-label="Direct link to The unit of sharing: a scope" title="Direct link to The unit of sharing: a scope" translate="no">​</a></h2>
<p>A scope is the slice of the cluster that a group of viewers has in common. In Kappsul we
have three:</p>
<table><thead><tr><th>Scope</th><th>Kubernetes objects</th><th>Selector</th><th>Who looks at it</th></tr></thead><tbody><tr><td><code>allWorkspaces</code></td><td>Namespaces</td><td><code>meta.kappsul.io/user-ns=yes</code></td><td>Admin views</td></tr><tr><td><code>workspace:{name}</code></td><td>Deployments, StatefulSets</td><td>namespace-scoped</td><td>Everyone on one workspace dashboard</td></tr><tr><td><code>app:{workspace}:{app}</code></td><td>Pods</td><td><code>app.kubernetes.io/instance={app}</code></td><td>Everyone on one app detail page</td></tr></tbody></table>
<p>That is the whole trick, expressed as a table. A scope is a promise: <em>these viewers are
looking at the same data, so they can share the same stream.</em> The number of watches the
API server sees becomes the number of active scopes, not the number of users.</p>
<p>Back-of-the-envelope: if each dashboard needed three streams and every user got their
own, three thousand users would mean nine thousand watches. If those users are spread
over six hundred workspaces, and the admin view collapses to a single global scope, the
system needs roughly six hundred and one. The users are a dimension of the UI, not of the
watch.</p>
<!-- -->
<p>Notice what is not in the diagram: an arrow from each browser to the API server. The
only thing that touches Kubernetes is the single owner of the scope.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="one-owner-per-scope">One owner per scope<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#one-owner-per-scope" class="hash-link" aria-label="Direct link to One owner per scope" title="Direct link to One owner per scope" translate="no">​</a></h2>
<p>The coordination layer is a small Go package, <code>watch-manager</code>, shared by the admin,
workspace, and application services. It does not know anything about Kubernetes. It
implements a lease: exactly one container instance owns a scope at a time, and the lease
is stored in Redis under a key derived from the scope.</p>
<div class="language-text codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-text codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token plain">watch-all-workspaces</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">watch-workspace:{workspace}</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">watch-app:{workspace}:{app}</span><br></div></code></pre></div></div>
<p>Starting a watch is a three-way decision: join, claim, or take over.</p>
<!-- -->
<p>The join path is the one that makes thousands of users cheap. A second viewer does not
create a second watch; it proves that the scope is still wanted by pushing the lease
forward, and returns the existing owner.</p>
<div class="language-go codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-go codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)">// watch-manager/manager.go (trimmed)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">watchKey </span><span class="token operator">:=</span><span class="token plain"> handler</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">GenerateWatchKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">watchCtx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">currentWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">_</span><span class="token plain"> </span><span class="token operator">:=</span><span class="token plain"> c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">redisClient</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Get</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> watchKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Result</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> currentWatcher </span><span class="token operator">!=</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">""</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    alive</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">_</span><span class="token plain"> </span><span class="token operator">:=</span><span class="token plain"> c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">isContainerStillWatching</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> currentWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> alive </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token comment" style="color:rgb(98, 114, 164)">// Someone already watches this scope: join it, do not duplicate the stream.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        ttl </span><span class="token operator">:=</span><span class="token plain"> time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Duration</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">WatchRedisTTL</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain"> time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Second</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">redisClient</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Set</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> watchKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> currentWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> ttl</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token operator">&amp;</span><span class="token plain">WatchResult</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain">Success</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token boolean">true</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> WatchingContainerID</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> currentWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">nil</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token comment" style="color:rgb(98, 114, 164)">// The owner is gone: exactly one contender wins the takeover.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    taken</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">_</span><span class="token plain"> </span><span class="token operator">:=</span><span class="token plain"> c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">attemptTakeover</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> currentWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> watchKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token operator">!</span><span class="token plain">taken </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token operator">&amp;</span><span class="token plain">WatchResult</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain">Success</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token boolean">false</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> Message</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"takeover lost"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">nil</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// Nobody owns the scope: claim it, then start the actual watch.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">ttl </span><span class="token operator">:=</span><span class="token plain"> time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Duration</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">WatchRedisTTL</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain"> time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Second</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">redisClient</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Set</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> watchKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">containerID</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> ttl</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">startHeartbeat</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">watchKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> err </span><span class="token operator">:=</span><span class="token plain"> handler</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">StartWatch</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> watchCtx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> err </span><span class="token operator">!=</span><span class="token plain"> </span><span class="token boolean">nil</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">stopHeartbeat</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">watchKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">redisClient</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Del</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> watchKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token boolean">nil</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> err</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></div></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_tQxV" id="proving-the-owner-is-alive">Proving the owner is alive<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#proving-the-owner-is-alive" class="hash-link" aria-label="Direct link to Proving the owner is alive" title="Direct link to Proving the owner is alive" translate="no">​</a></h3>
<p>Ownership alone is not enough: a pod can die while still holding the key. The owner
proves liveness with a heartbeat, and every contender checks it before trusting the
owner. The heartbeat is written every thirty seconds with a sixty-second TTL, and a
heartbeat older than sixty seconds is treated as dead.</p>
<div class="language-go codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-go codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)">// A per-container heartbeat, refreshed by the owner's active watch loops.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">heartbeatKey </span><span class="token operator">:=</span><span class="token plain"> config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">HeartbeatPrefix </span><span class="token operator">+</span><span class="token plain"> c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">containerID</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">ticker </span><span class="token operator">:=</span><span class="token plain"> time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">NewTicker</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Duration</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">HeartbeatInterval</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain"> time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Second</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">go</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">func</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">range</span><span class="token plain"> ticker</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">C </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        ts </span><span class="token operator">:=</span><span class="token plain"> strconv</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">FormatInt</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Now</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Unix</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token number">10</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        c</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">redisClient</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Set</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> heartbeatKey</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> ts</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            </span><span class="token number">2</span><span class="token operator">*</span><span class="token plain">time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Duration</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">HeartbeatInterval</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">*</span><span class="token plain">time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Second</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// ...and on the reader side:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">timeSinceHeartbeat </span><span class="token operator">:=</span><span class="token plain"> time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Now</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Unix</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">-</span><span class="token plain"> timestamp</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> timeSinceHeartbeat </span><span class="token operator">&lt;</span><span class="token plain"> </span><span class="token number">60</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">nil</span><br></div></code></pre></div></div>
<h3 class="anchor anchorTargetStickyNavbar_tQxV" id="taking-over-without-a-stampede">Taking over without a stampede<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#taking-over-without-a-stampede" class="hash-link" aria-label="Direct link to Taking over without a stampede" title="Direct link to Taking over without a stampede" translate="no">​</a></h3>
<p>When the owner is declared dead, every replica that notices may try to take over. The
claim has to be atomic, or you get the thing you were trying to avoid: several pods each
convinced they own the scope, each opening a watch. We do the compare-and-swap in Redis
with a Lua script, so the check and the write happen as one operation.</p>
<div class="language-lua codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-lua codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)">-- takeover.lua: only replace the owner we all agreed was dead.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">local</span><span class="token plain"> current </span><span class="token operator">=</span><span class="token plain"> redis</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">call</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">'get'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> KEYS</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> current </span><span class="token operator">==</span><span class="token plain"> ARGV</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">then</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    redis</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">call</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">'set'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> KEYS</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> ARGV</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">2</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'EX'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> ARGV</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">3</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token number">1</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">else</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token number">0</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">end</span><br></div></code></pre></div></div>
<p>One contending container gets <code>1</code>, starts the watch, and begins a heartbeat. The others
get <code>0</code> and back off. The winner also deletes the dead owner's heartbeat key, so the next
contender sees a clean state.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-lease-is-the-source-of-truth-not-the-watch">The lease is the source of truth, not the watch<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#the-lease-is-the-source-of-truth-not-the-watch" class="hash-link" aria-label="Direct link to The lease is the source of truth, not the watch" title="Direct link to The lease is the source of truth, not the watch" translate="no">​</a></h2>
<p>The most useful shift in the whole design was to stop thinking of the watch as a resource
that is started and stopped, and to think of it as a lease that is kept alive.</p>
<p>Stopping a watch does <strong>not</strong> delete the key. It lowers the TTL to sixty seconds and
stops the heartbeat. That grace period matters for three reasons:</p>
<ul>
<li class="">another user may still be viewing the same scope;</li>
<li class="">the same user may be reloading the page or reconnecting through a flaky network;</li>
<li class="">killing the stream immediately would guarantee a cold start on the next request.</li>
</ul>
<p>The frontend is the other half of the lease. A React hook starts the watch on mount,
renews it every two minutes while the page is open, and stops it on unmount. The renewal
is not a new watch; it is the same "join" path the backend already supports, which
extends the lease if the owner is alive and takes over if it is not.</p>
<div class="language-ts codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-ts codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)">// useWatch.ts (trimmed)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"> enabled </span><span class="token operator">=</span><span class="token plain"> </span><span class="token boolean">true</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> renewalInterval </span><span class="token operator">=</span><span class="token plain"> </span><span class="token number">2</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain"> </span><span class="token number">60</span><span class="token plain"> </span><span class="token operator">*</span><span class="token plain"> </span><span class="token number">1000</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"> </span><span class="token operator">=</span><span class="token plain"> options</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token function" style="color:rgb(80, 250, 123)">useEffect</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token operator">!</span><span class="token plain">enabled</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token function" style="color:rgb(80, 250, 123)">startWatch</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// claim or join the lease</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">const</span><span class="token plain"> timer </span><span class="token operator">=</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">setInterval</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">renewWatch</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> renewalInterval</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// keep the lease fresh</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token function" style="color:rgb(80, 250, 123)">clearInterval</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">timer</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token function" style="color:rgb(80, 250, 123)">stopWatch</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// drop the lease, not the stream</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">enabled</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> config</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">type</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></div></code></pre></div></div>
<p>That gives the system three independent clocks, and it is worth being explicit about what
each one protects:</p>
<table><thead><tr><th>Clock</th><th>Value</th><th>Where it lives</th><th>What it protects</th></tr></thead><tbody><tr><td>Kubernetes watch window</td><td>90s</td><td>stream to the API server</td><td>bounds each stream and forces a rewind</td></tr><tr><td>Heartbeat interval</td><td>30s</td><td>owner pod to Redis</td><td>proves the owner is alive</td></tr><tr><td>Heartbeat staleness</td><td>60s</td><td>Redis / reader</td><td>declares the owner dead</td></tr><tr><td>Lease TTL</td><td>180s</td><td>Redis</td><td>keeps ownership while the scope is viewed</td></tr><tr><td>Client renewal</td><td>120s</td><td>browser</td><td>keeps the lease fresh while the page is open</td></tr><tr><td>Graceful stop</td><td>60s</td><td>Redis</td><td>lets reconnects and other viewers win</td></tr></tbody></table>
<p>The ordering is not accidental. The client renews before the lease expires. The heartbeat
is much more frequent than the lease, so a dead owner is noticed long before the lease
itself runs out. The Kubernetes watch window is independent of both: it is there to make
the stream finite, not to keep ownership.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="the-watch-itself-bounded-rewound-and-deliberately-boring">The watch itself: bounded, rewound, and deliberately boring<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#the-watch-itself-bounded-rewound-and-deliberately-boring" class="hash-link" aria-label="Direct link to The watch itself: bounded, rewound, and deliberately boring" title="Direct link to The watch itself: bounded, rewound, and deliberately boring" translate="no">​</a></h2>
<p>The <code>watch-manager</code> decides <em>who</em> watches. The service decides <em>what</em> to watch, and it
does so with a manual rewatch loop. For a workspace, that means deployments and
statefulsets in one namespace, each rewound from the last <code>resourceVersion</code> it saw.</p>
<div class="language-go codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-go codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)">// workspace-service, WatchWorkspaceResources (trimmed)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    deploymentWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">_</span><span class="token plain"> </span><span class="token operator">:=</span><span class="token plain"> ws</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">k8sClient</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">WatchDeployments</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> workspace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> metav1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">ListOptions</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        ResourceVersion</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> deploymentResourceVersion</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        TimeoutSeconds</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain">  </span><span class="token operator">&amp;</span><span class="token plain">timeout</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// 90 seconds</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    statefulSetWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token boolean">_</span><span class="token plain"> </span><span class="token operator">:=</span><span class="token plain"> ws</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">k8sClient</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">WatchStatefulSets</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> workspace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> metav1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">ListOptions</span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        ResourceVersion</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"> statefulSetResourceVersion</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        TimeoutSeconds</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain">  </span><span class="token operator">&amp;</span><span class="token plain">timeout</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">watchLoop</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">for</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">select</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">case</span><span class="token plain"> event</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> ok </span><span class="token operator">:=</span><span class="token plain"> </span><span class="token operator">&lt;-</span><span class="token plain">deploymentWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">ResultChan</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token operator">!</span><span class="token plain">ok </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">                statefulSetWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Stop</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">                </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">break</span><span class="token plain"> watchLoop </span><span class="token comment" style="color:rgb(98, 114, 164)">// window closed: rewind with the last version</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> d</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> ok </span><span class="token operator">:=</span><span class="token plain"> event</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Object</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token operator">*</span><span class="token plain">appsv1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Deployment</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"> ok </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">                deploymentResourceVersion </span><span class="token operator">=</span><span class="token plain"> d</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">ResourceVersion</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            ws</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">emitWorkspaceChanged</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> workspace</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">"watch-deployment"</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">case</span><span class="token plain"> event</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> ok </span><span class="token operator">:=</span><span class="token plain"> </span><span class="token operator">&lt;-</span><span class="token plain">statefulSetWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">ResultChan</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            </span><span class="token comment" style="color:rgb(98, 114, 164)">// same shape for StatefulSets</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">case</span><span class="token plain"> </span><span class="token operator">&lt;-</span><span class="token plain">ctx</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Done</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">:</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            deploymentWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Stop</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            statefulSetWatcher</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Stop</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">            </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">        </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">Sleep</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">time</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token plain">Second</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token comment" style="color:rgb(98, 114, 164)">// small backoff before rewinding</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><br></div></code></pre></div></div>
<p>Two things are worth calling out.</p>
<p><strong>The rewind is where you handle the ugly parts.</strong> A watch can close for an ordinary
reason (timeout) or a bad one (<code>ERROR</code>, expired history). Carrying the last
<code>resourceVersion</code> across the rewind avoids gaps in the common case. If the API server
answers that the version is too old — the <code>410 Gone</code> case — the correct recovery is not
to retry the same version forever but to list again and start fresh. The bounded window
exists partly to make that decision cheap: you are never more than ninety seconds away
from a clean restart.</p>
<p><strong>The selectors are part of the contract, not an optimization.</strong> The admin watcher lists
namespaces with <code>meta.kappsul.io/user-ns=yes</code>. The app watcher lists pods with
<code>app.kubernetes.io/instance={app}</code> and watches from the list's <code>resourceVersion</code>. The
workspace watcher stays inside one namespace. If you find yourself watching a broad
collection and filtering in memory, you have moved the cost from the API server to your
own process and kept the worst part of the stream.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="fan-out-send-signals-not-objects">Fan-out: send signals, not objects<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#fan-out-send-signals-not-objects" class="hash-link" aria-label="Direct link to Fan-out: send signals, not objects" title="Direct link to Fan-out: send signals, not objects" translate="no">​</a></h2>
<p>At this point the Kubernetes side is settled: one stream per active scope. The remaining
question is how one stream becomes updates for three thousand browsers.</p>
<p>The short answer is that the browser never sees a Kubernetes object. It sees a small
signal, and then it reads the truth through the normal API.</p>
<!-- -->
<p>The service emits a topic event with a routing key in the <code>client.*</code> namespace. The
gateway owns a durable <code>client</code> queue bound to <code>client.#</code> on the <code>events</code> exchange. It
consumes the event, turns the routing key into a GraphQL subscription name, and publishes
to Redis Pub/Sub. Redis is what makes the gateway horizontally scalable: a WebSocket may
be connected to any replica, and the event still reaches it.</p>
<div class="language-ts codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-ts codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)">// gateway: routing key -&gt; subscription name</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">private</span><span class="token plain"> </span><span class="token function" style="color:rgb(80, 250, 123)">getSubscriptionName</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">routingKey</span><span class="token operator">:</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token operator">:</span><span class="token plain"> </span><span class="token builtin" style="color:rgb(189, 147, 249)">string</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">return</span><span class="token plain"> routingKey</span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">split</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token regex regex-delimiter">/</span><span class="token regex regex-source language-regex char-class char-class-punctuation punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token regex regex-source language-regex char-class">._-</span><span class="token regex regex-source language-regex char-class char-class-punctuation punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token regex regex-delimiter">/</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">filter</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">part</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">=&gt;</span><span class="token plain"> part </span><span class="token operator">!==</span><span class="token plain"> </span><span class="token string" style="color:rgb(255, 121, 198)">'client'</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">map</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">part</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> i</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">i </span><span class="token operator">===</span><span class="token plain"> </span><span class="token number">0</span><span class="token plain"> </span><span class="token operator">?</span><span class="token plain"> part </span><span class="token operator">:</span><span class="token plain"> part</span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token number">0</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">toUpperCase</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">+</span><span class="token plain"> part</span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">slice</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token number">1</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token punctuation" style="color:rgb(248, 248, 242)">.</span><span class="token function" style="color:rgb(80, 250, 123)">join</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token string" style="color:rgb(255, 121, 198)">''</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token comment" style="color:rgb(98, 114, 164)">// client.workspace-resources.changed -&gt; workspaceResourcesChanged</span><br></div></code></pre></div></div>
<p>The payload is intentionally tiny: a workspace name, an app name, and a phase such as
<code>initial</code>, <code>watch-deployment</code>, or <code>watch-statefulset</code>. It is a cache invalidation
message, not a data transfer. The frontend subscribes, filters by the arguments
(<code>workspace</code>, and for apps also <code>app</code>), and refreshes through the same query it would
have used on first load.</p>
<div class="language-ts codeBlockContainer_j5cZ theme-code-block" style="--prism-color:#F8F8F2;--prism-background-color:#282A36"><div class="codeBlockContent_Nbbu"><pre tabindex="0" class="prism-code language-ts codeBlock_BIxk thin-scrollbar" style="color:#F8F8F2;background-color:#282A36"><code class="codeBlockLines_Esqw"><div class="token-line" style="color:#F8F8F2"><span class="token comment" style="color:rgb(98, 114, 164)">// The event is a signal. Read the truth through the API.</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token function" style="color:rgb(80, 250, 123)">useEffect</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token operator">=&gt;</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token keyword" style="color:rgb(189, 147, 249);font-style:italic">if</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token plain">subscriptionData</span><span class="token operator">?.</span><span class="token plain">workspaceResourcesChanged</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">    </span><span class="token function" style="color:rgb(80, 250, 123)">refetch</span><span class="token punctuation" style="color:rgb(248, 248, 242)">(</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain">  </span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token plain"></span><br></div><div class="token-line" style="color:#F8F8F2"><span class="token plain"></span><span class="token punctuation" style="color:rgb(248, 248, 242)">}</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:rgb(248, 248, 242)">[</span><span class="token plain">subscriptionData</span><span class="token punctuation" style="color:rgb(248, 248, 242)">,</span><span class="token plain"> refetch</span><span class="token punctuation" style="color:rgb(248, 248, 242)">]</span><span class="token punctuation" style="color:rgb(248, 248, 242)">)</span><span class="token punctuation" style="color:rgb(248, 248, 242)">;</span><br></div></code></pre></div></div>
<p>This choice buys three things:</p>
<ul>
<li class=""><strong>Bandwidth and coupling.</strong> Kubernetes objects are large and versioned; a change
signal is neither. The browser does not need to understand Deployments to know that
the deployments changed.</li>
<li class=""><strong>Authorization stays on the read path.</strong> The event says "something changed"; the
refetch goes through the normal authenticated GraphQL query, with the normal access
rules.</li>
<li class=""><strong>Consistency stays where it already lives.</strong> The query path knows how to assemble the
dashboard from the database and the cluster. The event path does not have to
reimplement that assembly.</li>
</ul>
<p>The cost is refetch amplification: one real change can trigger one refetch per connected
viewer. That is usually the right trade at this scale — a refetch is a cheap, cacheable
read, while a stream per user is not — but it is a cost you should watch, not ignore.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="failure-is-the-normal-case">Failure is the normal case<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#failure-is-the-normal-case" class="hash-link" aria-label="Direct link to Failure is the normal case" title="Direct link to Failure is the normal case" translate="no">​</a></h2>
<p>The design assumes pods die. When the owner of a scope dies, nothing is deleted; the
lease simply stops being refreshed.</p>
<!-- -->
<p>A heartbeat older than sixty seconds is enough for another replica to attempt a
takeover, and the Lua compare-and-swap makes sure only one succeeds. The practical
failure window is on the order of a minute, plus however long it takes for a renewal or a
startup scan to notice.</p>
<p>Startup recovery uses the same primitives. Each service scans its own prefix in Redis,
restores the scopes it already owns, and attempts to take over scopes whose owner is
dead. That is why a rolling restart does not produce a thundering herd of cold starts:
the new pod does not blindly claim every scope it sees; it checks the heartbeat first.</p>
<p>The system is deliberately <strong>eventually consistent</strong> during a failover. For a short
window, two watchers can observe the same scope. That is acceptable because the events
are idempotent invalidation signals: the UI refetches and converges. A brief duplicate
watch is a much smaller problem than a coordination protocol that blocks on a dead
process.</p>
<!-- -->
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="what-this-design-costs">What this design costs<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#what-this-design-costs" class="hash-link" aria-label="Direct link to What this design costs" title="Direct link to What this design costs" translate="no">​</a></h2>
<p>It would be dishonest to present this as free. The API server load did not disappear; it
moved, and the new places are cheaper and easier to scale independently, but they are
still places.</p>
<ul>
<li class=""><strong>Redis is in the hot path.</strong> Every start, renewal and liveness check is a Redis
round-trip. It is a small number of operations per scope, not per user, but Redis is now
part of your availability story. Treat it as such: Sentinel or a managed deployment,
and a plan for what happens when it is slow.</li>
<li class=""><strong>Liveness depends on clocks.</strong> The heartbeat check compares timestamps written by
different pods. Reasonable clock discipline is enough, but a pod with a badly skewed
clock can look dead or immortal. A purely TTL-based liveness signal is a possible
hardening step.</li>
<li class=""><strong>Refetches can stampede.</strong> A burst that touches five hundred pods produces a burst of
invalidations, and then a burst of refetches. Debouncing or coalescing per scope, and
jittering client refreshes, are the obvious next levers.</li>
<li class=""><strong>Duplicate watches exist briefly.</strong> Failover is not atomic across the API server.
Accept it, and make sure duplicate streams cannot cause duplicate side effects.</li>
<li class=""><strong>There is more surface to operate.</strong> Redis keys, heartbeats, lease TTLs, a message
broker, a pub/sub layer and WebSocket replicas. Each one is simple; the combination is a
system you have to observe.</li>
</ul>
<p>The trade is still strongly in favor of this design, because the resource it protects —
the Kubernetes API server — is the one shared by everything else in the cluster.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="tuning-the-knobs">Tuning the knobs<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#tuning-the-knobs" class="hash-link" aria-label="Direct link to Tuning the knobs" title="Direct link to Tuning the knobs" translate="no">​</a></h2>
<p>The numbers are not magic. They encode a few simple relationships:</p>
<table><thead><tr><th>Knob</th><th>Kappsul value</th><th>Constraint</th></tr></thead><tbody><tr><td><code>WatchExpirationTime</code></td><td>90s</td><td>the window between rewinds</td></tr><tr><td><code>WatchRedisTTL</code></td><td>180s</td><td>must exceed the client renewal interval</td></tr><tr><td><code>HeartbeatInterval</code></td><td>30s</td><td>must be well under the staleness threshold</td></tr><tr><td>Heartbeat staleness</td><td>60s</td><td>must be under the lease TTL</td></tr><tr><td>Client renewal</td><td>120s</td><td>must be under <code>WatchRedisTTL</code></td></tr><tr><td>Graceful stop TTL</td><td>60s</td><td>short enough to free the scope, long enough to survive a reload</td></tr></tbody></table>
<p>If you take one rule from this table, take this one: <strong>the client must renew before the
lease expires, and the heartbeat must expire before the lease does.</strong> Get that ordering
wrong and you will either drop live watches or keep dead ones.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="what-i-would-measure-next">What I would measure next<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#what-i-would-measure-next" class="hash-link" aria-label="Direct link to What I would measure next" title="Direct link to What I would measure next" translate="no">​</a></h2>
<p>The design is only defensible with numbers, and the numbers that matter are mostly on the
API server side:</p>
<ul>
<li class=""><strong>Watches per resource type</strong>, and how that number tracks active scopes rather than
connected users. This is the core claim of the design.</li>
<li class=""><strong>List and watch QPS</strong>, plus the rate of <code>410 Gone</code> responses. A rising 410 rate means
your rewind is too slow or your windows are too long.</li>
<li class=""><strong>Event delivery latency</strong>, p50 and p99, from the Kubernetes event to the browser
refetch. This is what users actually feel.</li>
<li class=""><strong>Refetch rate per scope.</strong> This is where a burst turns into a stampede.</li>
<li class=""><strong>Failover time</strong>, from owner death to a new owner watching. The heartbeat and lease
values above imply a target; measure whether you hit it.</li>
</ul>
<p>If I were hardening this further, I would start with per-scope debouncing of invalidation
signals, jitter on client renewals, and a stricter liveness check that does not depend on
comparing wall clocks across pods.</p>
<h2 class="anchor anchorTargetStickyNavbar_tQxV" id="takeaways">Takeaways<a href="https://blog.djovap.com/kubernetes-watcher-at-scale#takeaways" class="hash-link" aria-label="Direct link to Takeaways" title="Direct link to Takeaways" translate="no">​</a></h2>
<ul>
<li class=""><strong>Make the watch a property of the data, not the user.</strong> Define scopes — resource set
plus selector plus namespace — and share one stream per scope.</li>
<li class=""><strong>Elect one owner per scope.</strong> A Redis key with a TTL is enough, provided you add a
heartbeat so a dead owner does not hold it forever.</li>
<li class=""><strong>Make takeover atomic.</strong> A Lua compare-and-swap prevents the failover stampede that
would otherwise recreate the problem you solved.</li>
<li class=""><strong>Treat the watch as a lease.</strong> Start on view, renew while viewed, lower the TTL on
stop, and let it expire naturally.</li>
<li class=""><strong>Bound and rewind the stream.</strong> A finite window forces you to re-evaluate demand and
gives you a clean place to handle expired <code>resourceVersion</code>s.</li>
<li class=""><strong>Fan out signals, not objects.</strong> Let one Kubernetes event become one small
invalidation, and let each client read the truth through the normal API.</li>
<li class=""><strong>Assume failure.</strong> Recover scopes on startup, accept brief overlap, and make duplicate
events harmless.</li>
<li class=""><strong>Protect the API server first.</strong> It is the shared resource; everything else in this
design exists to keep it out of the hot path.</li>
</ul>]]></content:encoded>
            <category>Kubernetes</category>
            <category>Real time</category>
            <category>Distributed systems</category>
        </item>
    </channel>
</rss>