# Dedicated JetStream-enabled NATS cluster for the log bus. Deliberately NOT
# shared with app messaging (streamstack et al. run their own NATS in their own
# repo) — a separate cluster isolates logging blast-radius from app messaging
# and lets us size retention/storage purely for the log outage-buffer + replay
# use-case.
fullnameOverride: nats

config:
  cluster:
    enabled: true
    replicas: 3
  jetstream:
    enabled: true
    fileStore:
      pvc:
        # Sized for 3d retention: ~100 GiB/3d compressed (see
        # nats-stream-limits ConfigMap) + file-store WAL/index/overhead, kept
        # safely above the 130 GiB max_bytes cap. 3 replicas => ~0.5 TiB total
        # provisioned on cephrbd-fast-delete. NB: this is the honest number for
        # the assumed ~1500 events/s; higher real volume needs a bigger PVC +
        # max_bytes together, else discard=old truncates retention below 3d.
        size: 180Gi
        storageClassName: cephrbd-fast-delete
  # Per-user auth with publish/subscribe separation. Passwords are injected as
  # env vars from the Vault-synced nats-auth Secret. The `<< $VAR >>` wrapping is
  # REQUIRED by this chart: it renders the value UNQUOTED in nats.conf so the
  # NATS server expands the env var. A plain `$VAR` is JSON-quoted ("$VAR") and
  # NATS then treats it as a literal string — which broke auth for every client.
  merge:
    authorization:
      users:
        # Bootstrap Job (stream/consumer management) — full JetStream API.
        - user: log-admin
          password: << $NATS_ADMIN_PASSWORD >>
        # Edge publishers (k8s DaemonSet + VM ingest) — publish only.
        - user: log-producer
          password: << $NATS_PRODUCER_PASSWORD >>
          permissions:
            publish:
              allow:
                - "logs.>"
            subscribe:
              allow:
                - "_INBOX.>"
        # Consumers (transform tier + archiver) — pull + ack only, no publish
        # to log subjects.
        - user: log-consumer
          password: << $NATS_CONSUMER_PASSWORD >>
          permissions:
            publish:
              allow:
                - "$JS.API.CONSUMER.>"
                - "$JS.API.STREAM.INFO.>"
                - "$JS.ACK.LOGS.>"
            subscribe:
              allow:
                - "_INBOX.>"

container:
  # Upstream official nats; no DHI variant available.
  image:
    repository: docker.io/library/nats
    tag: 2.14.2-alpine
  env:
    NATS_ADMIN_PASSWORD:
      valueFrom:
        secretKeyRef:
          name: nats-auth
          key: admin_password
    NATS_PRODUCER_PASSWORD:
      valueFrom:
        secretKeyRef:
          name: nats-auth
          key: producer_password
    NATS_CONSUMER_PASSWORD:
      valueFrom:
        secretKeyRef:
          name: nats-auth
          key: consumer_password
  resources:
    requests:
      cpu: 250m
      memory: 512Mi
    limits:
      cpu: "2"
      memory: 4Gi

# Roll the StatefulSet on config changes only; nats-auth is Vault-rotated (VSO)
# so it is deliberately not watched here (no restart on routine key rotation).
podTemplate:
  merge:
    metadata:
      annotations:
        configmap.reloader.stakater.com/auto: "true"

# Config-reloader sidecar image.
reloader:
  image:
    repository: docker.io/natsio/nats-server-config-reloader
    tag: "0.23.0"

natsBox:
  enabled: false
