apiVersion: apps/v1
kind: Deployment
metadata:
  name: local-registry
  namespace: vibecarbon
  labels:
    app: local-registry
spec:
  replicas: 1
  # Recreate (not RollingUpdate) because hostPort: 5000 can only be held by one pod at a time.
  # RollingUpdate would leave the new pod Pending until the old one releases the port.
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: local-registry
  template:
    metadata:
      labels:
        app: local-registry
    spec:
      # Run only on master node — hostPort exposes 5000 on master's private IP
      # for SSH tunnel access, and the hostPath volume below rides the same pin.
      nodeSelector:
        node-role.kubernetes.io/control-plane: "true"
      tolerations:
        - key: node-role.kubernetes.io/control-plane
          operator: Exists
          effect: NoSchedule
      # The hostPath volume is created root-owned; the registry runs as 1000.
      # One-shot chown as root before the registry starts (the main container
      # keeps runAsNonRoot).
      initContainers:
        - name: chown-storage
          image: registry:2
          command: ["/bin/sh", "-c", "chown -R 1000:1000 /var/lib/registry && chmod 700 /var/lib/registry"]
          securityContext:
            runAsUser: 0
            allowPrivilegeEscalation: false
          volumeMounts:
            - name: registry-storage
              mountPath: /var/lib/registry
      containers:
        - name: registry
          image: registry:2
          # ROOT FIX (2026-08-30, run 33332292268): image layers live on the
          # MASTER NODE DISK, never object storage. The registry is a
          # transport cache — every deploy (each k8s-ha cluster separately,
          # every restore re-deploy) builds and pushes its own image, and
          # nodes pull through this pod regardless of backend — so the old S3
          # backend bought no durability anyone consumed while coupling every
          # k8s deploy to the one backend with documented read-after-write
          # weather ("s3aws: NoSuchBucket" completing an upload mid-push →
          # docker push 500s → deploy dead; the class the walg/pulumi
          # surfaces already document). Filesystem is registry:2's default
          # driver when no REGISTRY_STORAGE env is set. Enforced by
          # tests/unit/template/registry-no-object-storage.test.ts.
          securityContext:
            runAsUser: 1000
            runAsNonRoot: true
            allowPrivilegeEscalation: false
          env:
            - name: REGISTRY_STORAGE_CACHE_BLOBDESCRIPTOR
              value: inmemory
            - name: REGISTRY_STORAGE_DELETE_ENABLED
              value: "true"
          ports:
            - containerPort: 5000
              # hostPort exposes the registry on the master's private IP (10.0.1.1:5000).
              # Worker and supabase kubelets use 10.0.1.1:5000 to pull images — kubelet uses
              # node DNS (not cluster DNS) so it can't resolve the "local-registry" service name.
              hostPort: 5000
              protocol: TCP
          volumeMounts:
            - name: registry-storage
              mountPath: /var/lib/registry
          resources:
            requests:
              cpu: 100m
              memory: 128Mi
            limits:
              cpu: 500m
              # 1Gi headroom: large image layers (the backup image is
              # ~300MB) stream through memory during pushes.
              memory: 1Gi
          readinessProbe:
            httpGet:
              path: /v2/
              port: 5000
            initialDelaySeconds: 5
            periodSeconds: 10
            # Default timeoutSeconds=1 is hostile to a registry busy
            # writing a large layer to disk — give it 10s before we
            # declare the probe failed and pull it from the Service.
            timeoutSeconds: 10
            failureThreshold: 5
          livenessProbe:
            httpGet:
              path: /v2/
              port: 5000
            initialDelaySeconds: 10
            periodSeconds: 30
            # Same reasoning as readinessProbe — but with a larger
            # failureThreshold because killing the pod mid-push aborts
            # the upload with `manifest blob unknown`/`EOF` (exactly
            # the failure mode that bit e2e on 2026-04-30).
            timeoutSeconds: 10
            failureThreshold: 6
      volumes:
        - name: registry-storage
          # Master node disk. Survives pod restarts (the old emptyDir did
          # not); a master REPLACEMENT loses it, and that is fine — replacing
          # the master means re-deploying, which rebuilds and re-pushes.
          hostPath:
            path: /var/lib/vibecarbon-registry
            type: DirectoryOrCreate
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: registry-policy
  namespace: vibecarbon
  labels:
    app.kubernetes.io/part-of: vibecarbon
spec:
  podSelector:
    matchLabels:
      app: local-registry
  policyTypes:
    - Ingress
    - Egress
  ingress:
    # Allow image pulls from any pod in the namespace via ClusterIP
    - ports:
        - port: 5000
          protocol: TCP
  # Egress declared with NO rules = default-deny: the filesystem-backed
  # registry makes no outbound calls at all (no S3, and therefore no DNS).
  egress: []
---
# ClusterIP Service for in-cluster push paths (e.g., a CI pod inside the
# cluster running `docker push` to localhost:5000 via kube-proxy). Kubelet
# itself does NOT use this Service — it pulls via hostPort because it
# resolves names against node DNS, not cluster DNS. See registries.yaml
# in cloud-init for the kubelet-side mirror config.
apiVersion: v1
kind: Service
metadata:
  name: local-registry
  namespace: vibecarbon
  labels:
    app: local-registry
spec:
  type: ClusterIP
  ports:
    - port: 5000
      targetPort: 5000
      protocol: TCP
  selector:
    app: local-registry
