# VeilBrowser cluster-mode worker fleet on Kubernetes.
#
# Docs:  https://veilbrowser.net/docs/self-hosting/installation#deployment-templates
# Fetch: https://veilbrowser.net/deploy/k8s/worker-cluster.yaml
#
# Scales the on-demand browser workers horizontally. A Service (LoadBalancer)
# fronts them for the `wss://<lb>/launch` entry. For sticky reconnect/VNC across
# many pods, front with the cluster-manager LB (path-encoded /w/<id>/ routing)
# instead of a bare Service, or run one worker per node.
#
#   curl -fsSL https://veilbrowser.net/deploy/k8s/worker-cluster.yaml -o worker-cluster.yaml
#   # replace <org-key-lower> in the image below with your lowercased org key
#   kubectl apply -f worker-cluster.yaml
#   kubectl scale deployment veil-worker --replicas=10
#
# No imagePullSecret: the license gate authorizes by PATH, so the org key in
# the image reference is the whole credential. There is no `docker login` and
# no registry username or password.
#
# Assumes a cluster-manager reachable in-cluster as the release origin (the
# `RELEASE_ORIGIN_URL` below) — workers pull their browser and profile bundles
# from it. This manifest does not deploy one.
---
apiVersion: v1
kind: Secret
metadata:
  name: veil-worker-secrets
type: Opaque
stringData:
  API_KEY: "change-me-worker-inbound-key"
  VEIL_KEY: "veil_org_key_here" # org key for saas-api authorize
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: veil-worker
  labels: { app: veil-worker }
spec:
  replicas: 1
  strategy:
    rollingUpdate:
      maxUnavailable: 100%
  selector:
    matchLabels: { app: veil-worker }
  template:
    metadata:
      labels: { app: veil-worker }
    spec:
      volumes:
        - name: tmpfs
          emptyDir:
            medium: Memory
            sizeLimit: 2Gi
        - name: dshm
          emptyDir:
            medium: Memory
            sizeLimit: 1Gi
      containers:
        - name: worker
          image: registry.veilbrowser.net/<org-key-lower>/local-api:latest
          imagePullPolicy: Always
          env:
            - name: SAAS_API_URL
              value: https://api.veilbrowser.net
            - name: RELEASE_ORIGIN_URL
              value: http://veil-cluster-manager # in-cluster release origin
            - name: VEIL_BOOTSTRAP_RELEASES
              value: "1"
            - name: VEIL_WORKER_ID
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: API_KEY
              valueFrom:
                secretKeyRef: { name: veil-worker-secrets, key: API_KEY }
            - name: VEIL_KEY
              valueFrom:
                secretKeyRef: { name: veil-worker-secrets, key: VEIL_KEY }
          ports:
            - containerPort: 38923
          volumeMounts:
            - { mountPath: /tmp, name: tmpfs }
            - { mountPath: /dev/shm, name: dshm }
          securityContext:
            # Chromium's user-namespace sandbox needs SYS_ADMIN (mirrors the
            # undetect k8s example). Pair with a seccomp profile in prod.
            allowPrivilegeEscalation: true
            capabilities:
              add: ["SYS_ADMIN"]
          readinessProbe:
            httpGet: { path: /health, port: 38923 }
            initialDelaySeconds: 10
            periodSeconds: 10
          lifecycle:
            preStop:
              # Give in-flight CDP sessions a moment to drain before SIGTERM.
              exec: { command: ["sleep", "10"] }
---
apiVersion: v1
kind: Service
metadata:
  name: veil-worker
spec:
  type: LoadBalancer
  selector: { app: veil-worker }
  ports:
    - name: launch
      port: 9222
      targetPort: 38923
