# k8s/armature.yaml
# ============================================
# Armature Core - Deployment
# ============================================
# Secrets:
#   armature-db-credentials  - POSTGRES_USER, POSTGRES_PASSWORD
#   armature-admin           - username, password, email (optional)
#   armature-encryption      - ENCRYPTION_KEY (required for v0.9.4+)
#
# PVC:
#   armature-storage         - Extraction scratch (always needed)
#   Requires ReadWriteMany (RWX) — see k8s/storage-pvc.yaml
#   With S3 backend, PVC is small (extraction status only, not blobs)
#
# Variables (Gitea CI):
#   STORAGE_CLASS               - StorageClass for PVC (e.g. "cephfs")
#   STORAGE_SIZE                - PVC size (e.g. "10Gi" for PVC, "1Gi" for S3)
#   STORAGE_BACKEND             - "pvc" or "s3" (default: "pvc")
#
# S3 secrets (Gitea Secrets — only when STORAGE_BACKEND=s3):
#   S3_ENDPOINT                 - e.g. "http://minio:9000" or Ceph RGW URL
#   S3_BUCKET                   - bucket name (must exist before deploy)
#   S3_ACCESS_KEY               - S3 access key
#   S3_SECRET_KEY               - S3 secret key
#
# Admin bootstrap: on every pod start, if ARMATURE_ADMIN_* env vars
# are set, the backend upserts an admin user. Change the secret and
# restart the pod to reset the admin password.
# ============================================
apiVersion: apps/v1
kind: Deployment
metadata:
  name: armature${DEPLOY_SUFFIX}
  namespace: ${NAMESPACE}
  labels:
    app: armature
    env: ${ENVIRONMENT}
spec:
  replicas: ${REPLICAS}
  selector:
    matchLabels:
      app: armature
      env: ${ENVIRONMENT}
  template:
    metadata:
      labels:
        app: armature
        env: ${ENVIRONMENT}
    spec:
      containers:
      - name: armature
        image: ${IMAGE}:${IMAGE_TAG}
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          name: http
        env:
        - name: ENVIRONMENT
          value: "${ENVIRONMENT}"
        - name: GIN_MODE
          value: "${GIN_MODE}"
        - name: PORT
          value: "8080"
        - name: BASE_PATH
          value: "${BASE_PATH}"
        - name: POSTGRES_HOST
          value: "${POSTGRES_HOST}"
        - name: POSTGRES_PORT
          value: "${POSTGRES_PORT}"
        - name: POSTGRES_DB
          value: "${DB_NAME}"
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: armature-db-credentials
              key: POSTGRES_USER
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: armature-db-credentials
              key: POSTGRES_PASSWORD
        - name: DATABASE_URL
          value: "postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):$(POSTGRES_PORT)/$(POSTGRES_DB)?sslmode=disable"
        - name: ARMATURE_ADMIN_USERNAME
          valueFrom:
            secretKeyRef:
              name: armature-admin
              key: username
              optional: true
        - name: ARMATURE_ADMIN_PASSWORD
          valueFrom:
            secretKeyRef:
              name: armature-admin
              key: password
              optional: true
        - name: ARMATURE_ADMIN_EMAIL
          valueFrom:
            secretKeyRef:
              name: armature-admin
              key: email
              optional: true
        - name: SEED_USERS
          valueFrom:
            secretKeyRef:
              name: armature-seed-users
              key: users
              optional: true
        - name: ENCRYPTION_KEY
          valueFrom:
            secretKeyRef:
              name: armature-encryption
              key: ENCRYPTION_KEY
              optional: true
        # File storage (v0.12.0+)
        - name: STORAGE_BACKEND
          value: "${STORAGE_BACKEND}"
        - name: STORAGE_PATH
          value: "/data/storage"
        # S3 storage (optional — only used when STORAGE_BACKEND=s3)
        - name: S3_ENDPOINT
          valueFrom:
            secretKeyRef:
              name: armature-s3
              key: S3_ENDPOINT
              optional: true
        - name: S3_BUCKET
          valueFrom:
            secretKeyRef:
              name: armature-s3
              key: S3_BUCKET
              optional: true
        - name: S3_ACCESS_KEY
          valueFrom:
            secretKeyRef:
              name: armature-s3
              key: S3_ACCESS_KEY
              optional: true
        - name: S3_SECRET_KEY
          valueFrom:
            secretKeyRef:
              name: armature-s3
              key: S3_SECRET_KEY
              optional: true
        - name: S3_REGION
          valueFrom:
            secretKeyRef:
              name: armature-s3
              key: S3_REGION
              optional: true
        - name: S3_PREFIX
          valueFrom:
            secretKeyRef:
              name: armature-s3
              key: S3_PREFIX
              optional: true
        - name: S3_FORCE_PATH_STYLE
          value: "true"
        # Bundled packages (v0.3.8)
        # Dev: install all (empty = all). Test: install all. Prod: skip (nothing bundled needed yet).
        - name: SKIP_BUNDLED_PACKAGES
          value: "${SKIP_BUNDLED_PACKAGES}"
        - name: BUNDLED_PACKAGES
          value: "${BUNDLED_PACKAGES}"
        volumeMounts:
        - name: storage
          mountPath: /data/storage
        resources:
          requests:
            memory: "${MEMORY_REQUEST}"
            cpu: "${CPU_REQUEST}"
          limits:
            memory: "${MEMORY_LIMIT}"
            cpu: "${CPU_LIMIT}"
        readinessProbe:
          httpGet:
            path: ${BASE_PATH}/health
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 10
          timeoutSeconds: 3
          failureThreshold: 3
        livenessProbe:
          httpGet:
            path: ${BASE_PATH}/health
            port: 80
          initialDelaySeconds: 10
          periodSeconds: 30
          timeoutSeconds: 5
          failureThreshold: 3

      volumes:
      - name: storage
        persistentVolumeClaim:
          claimName: armature-storage${DEPLOY_SUFFIX}
---
apiVersion: v1
kind: Service
metadata:
  name: armature${DEPLOY_SUFFIX}
  namespace: ${NAMESPACE}
  labels:
    app: armature
    env: ${ENVIRONMENT}
spec:
  selector:
    app: armature
    env: ${ENVIRONMENT}
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    name: http
