# =============================================================================
# WOOO TECH - Momo Pro System
# 優化版 momo-app Deployment
# 優化重點: 資源配置、健康檢查、滾動更新策略
# =============================================================================
apiVersion: apps/v1
kind: Deployment
metadata:
  name: momo-app
  namespace: momo
spec:
  replicas: 1
  # 滾動更新策略 - 確保零停機
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1
  selector:
    matchLabels:
      app: momo-app
  template:
    metadata:
      labels:
        app: momo-app
    spec:
      # 優雅終止時間
      terminationGracePeriodSeconds: 30

      # Init Container - 複製 Google Drive 認證
      initContainers:
      - name: copy-google-credentials
        image: busybox:1.36
        command: ['sh', '-c', 'cp /secrets/* /config/ && chmod 644 /config/*']
        volumeMounts:
        - name: google-drive-secrets
          mountPath: /secrets
          readOnly: true
        - name: momo-config-volume
          mountPath: /config

      containers:
      - name: momo-app
        image: momo-pro-system:local
        imagePullPolicy: Never
        ports:
        - containerPort: 80

        # 環境變數
        envFrom:
        - configMapRef:
            name: momo-config
        - secretRef:
            name: momo-secrets

        # 資源配置 (平衡穩定性與效能)
        resources:
          requests:
            memory: 512Mi
            cpu: 200m
          limits:
            memory: 2Gi
            cpu: 1000m

        # 存活探測 - 確保應用正常運行
        livenessProbe:
          httpGet:
            path: /health
            port: 80
          initialDelaySeconds: 30
          periodSeconds: 30
          timeoutSeconds: 10
          failureThreshold: 3
          successThreshold: 1

        # 就緒探測 - 確保可接收流量
        readinessProbe:
          httpGet:
            path: /health
            port: 80
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
          successThreshold: 1

        # 啟動探測 - 給予更多啟動時間
        startupProbe:
          httpGet:
            path: /health
            port: 80
          initialDelaySeconds: 5
          periodSeconds: 5
          timeoutSeconds: 5
          failureThreshold: 30  # 5*30=150秒啟動時間
          successThreshold: 1

        volumeMounts:
        - name: momo-data
          mountPath: /app/data
        - name: momo-config-volume
          mountPath: /app/config

      volumes:
      - name: momo-data
        persistentVolumeClaim:
          claimName: momo-data-pvc
      - name: google-drive-secrets
        secret:
          secretName: google-drive-credentials
      - name: momo-config-volume
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: momo-app
  namespace: momo
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: momo-app
