# Reference template — NOT used directly by deploy.sh.
# The deployed manifest is generated by: make render-k8s  (render_k8s.go)
# Edit render_k8s.go to change the deployed manifest structure.
apiVersion: v1
kind: Namespace
metadata:
  name: dune-admin
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: dune-admin-config
  namespace: dune-admin
data:
  DB_HOST: "postgres.default.svc.cluster.local"
  DB_PORT: "15432"
  DB_USER: "dune"
  DB_NAME: "dune"
  DB_SCHEMA: "dune"
  LISTEN_ADDR: ":8080"
  CONTROL: "local"
  MARKET_BOT_ENABLED: "true"
  MARKET_BOT_BUY_INTERVAL: "5m"
  MARKET_BOT_LIST_INTERVAL: "30m"
  config.yaml: |
    control: local
    listen_addr: :8080
    market_bot_enabled: true
    market_bot_cache_db: /data/market-bot-cache.db
    market_bot_state: /data/market-bot-state.json
    market_bot_item_data: /app/item-data.json
    market_bot_buy_interval: 5m
    market_bot_list_interval: 30m
    market_bot_buy_threshold: 1.05
    market_bot_max_buys: 50
---
apiVersion: v1
kind: Secret
metadata:
  name: dune-admin-secrets
  namespace: dune-admin
type: Opaque
stringData:
  DB_PASS: "replace-me"
  BROKER_USER: ""
  BROKER_PASS: ""
  BROKER_JWT_SECRET: ""
  MARKET_BOT_REMOTE_TOKEN: ""
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: market-bot-cache
  namespace: dune-admin
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
# Persistent volume for the dune-admin binary and frontend assets.
# The init container seeds this from the image on first deploy; subsequent
# restarts skip the seed and use whatever binary is in the PVC — including
# binaries downloaded by the self-update flow.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dune-admin-app
  namespace: dune-admin
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dune-admin
  namespace: dune-admin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: dune-admin
  template:
    metadata:
      labels:
        app: dune-admin
    spec:
      # Two init containers run before the main container:
      #   seed-binary: copies the binary + assets from the image into the app
      #     PVC on first deploy (skipped when the PVC already has a binary,
      #     e.g. after a self-update). Self-update writes to /app/dune-admin
      #     on the PVC; the next restart uses that binary without re-seeding.
      #   seed-config: copies the ConfigMap config.yaml into a writable emptyDir
      #     so that handleSaveConfig can write back. Changes persist within the
      #     pod lifetime; update the ConfigMap for permanent config changes.
      initContainers:
        - name: seed-binary
          image: ghcr.io/icehunter/dune-admin:latest
          imagePullPolicy: IfNotPresent
          command:
            - sh
            - -c
            - |
              if [ ! -f /app/dune-admin ]; then
                echo "Seeding dune-admin binary and assets from image..."
                cp /usr/local/share/dune-admin-seed/dune-admin /app/
                cp /usr/local/share/dune-admin-seed/tags-data.json /usr/local/share/dune-admin-seed/item-data.json /app/
                cp -r /usr/local/share/dune-admin-seed/dist /app/dist
                chmod 0755 /app/dune-admin
                echo "Seed complete."
              else
                echo "Binary already present — skipping seed (self-updated binary in use)."
              fi
          volumeMounts:
            - name: app-rw
              mountPath: /app
        - name: seed-config
          image: busybox:latest
          command: ['sh', '-c', 'cp /configmap/config.yaml /app-config/config.yaml']
          volumeMounts:
            - name: config-source
              mountPath: /configmap
              readOnly: true
            - name: config-rw
              mountPath: /app-config
      containers:
        - name: dune-admin
          image: ghcr.io/icehunter/dune-admin:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8080
              name: http
          env:
            - name: DB_HOST
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: DB_HOST
            - name: DB_PORT
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: DB_PORT
            - name: DB_USER
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: DB_USER
            - name: DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: DB_NAME
            - name: DB_SCHEMA
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: DB_SCHEMA
            - name: LISTEN_ADDR
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: LISTEN_ADDR
            - name: CONTROL
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: CONTROL
            - name: MARKET_BOT_ENABLED
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: MARKET_BOT_ENABLED
            - name: MARKET_BOT_BUY_INTERVAL
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: MARKET_BOT_BUY_INTERVAL
            - name: MARKET_BOT_LIST_INTERVAL
              valueFrom:
                configMapKeyRef:
                  name: dune-admin-config
                  key: MARKET_BOT_LIST_INTERVAL
            - name: DB_PASS
              valueFrom:
                secretKeyRef:
                  name: dune-admin-secrets
                  key: DB_PASS
            - name: BROKER_USER
              valueFrom:
                secretKeyRef:
                  name: dune-admin-secrets
                  key: BROKER_USER
            - name: BROKER_PASS
              valueFrom:
                secretKeyRef:
                  name: dune-admin-secrets
                  key: BROKER_PASS
            - name: BROKER_JWT_SECRET
              valueFrom:
                secretKeyRef:
                  name: dune-admin-secrets
                  key: BROKER_JWT_SECRET
            - name: MARKET_BOT_REMOTE_TOKEN
              valueFrom:
                secretKeyRef:
                  name: dune-admin-secrets
                  key: MARKET_BOT_REMOTE_TOKEN
                  optional: true
            - name: DUNE_ADMIN_SESSIONS_DB
              value: /data/sessions.db
          volumeMounts:
            - name: app-rw
              mountPath: /app
            - name: market-bot-cache
              mountPath: /data
            - name: config-rw
              mountPath: /root/.dune-admin
          readinessProbe:
            httpGet:
              path: /api/v1/status
              port: http
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /api/v1/status
              port: http
            initialDelaySeconds: 15
            periodSeconds: 30
      volumes:
        - name: app-rw
          persistentVolumeClaim:
            claimName: dune-admin-app
        - name: market-bot-cache
          persistentVolumeClaim:
            claimName: market-bot-cache
        - name: config-source
          configMap:
            name: dune-admin-config
            items:
              - key: config.yaml
                path: config.yaml
        - name: config-rw
          emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: dune-admin
  namespace: dune-admin
spec:
  selector:
    app: dune-admin
  ports:
    - name: http
      port: 8080
      targetPort: http
      nodePort: 30080
  type: NodePort
