# ez-api Deployment (Control Plane)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ez-api
  namespace: ez-api-test-dev
  labels:
    app.kubernetes.io/name: ez-api-test-dev
    app.kubernetes.io/component: ez-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ez-api
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: ez-api
        app.kubernetes.io/name: ez-api-test-dev
        app.kubernetes.io/component: ez-api
    spec:
      # 等待依赖服务就绪
      initContainers:
        - name: wait-for-postgres
          image: busybox:1.36
          command:
            - sh
            - -c
            - |
              until nc -z postgres 5432; do
                echo "Waiting for postgres..."
                sleep 2
              done
              echo "PostgreSQL is ready!"
        - name: wait-for-log-postgres
          image: busybox:1.36
          command:
            - sh
            - -c
            - |
              until nc -z log-postgres 5432; do
                echo "Waiting for log-postgres..."
                sleep 2
              done
              echo "Log PostgreSQL is ready!"
        - name: wait-for-redis
          image: busybox:1.36
          command:
            - sh
            - -c
            - |
              until nc -z redis 6379; do
                echo "Waiting for redis..."
                sleep 2
              done
              echo "Redis is ready!"
      containers:
        - name: ez-api
          image: harbor.rcfortress.site/library/ez-api:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
              name: http
          env:
            - name: EZ_API_PORT
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_API_PORT
            # PostgreSQL DSN
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: ez-api-secrets
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ez-api-secrets
                  key: POSTGRES_PASSWORD
            - name: POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: POSTGRES_DB
            - name: EZ_PG_DSN
              value: "host=postgres user=$(POSTGRES_USER) password=$(POSTGRES_PASSWORD) dbname=$(POSTGRES_DB) port=5432 sslmode=disable"
            # Log PostgreSQL DSN
            - name: LOG_POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: ez-api-secrets
                  key: LOG_POSTGRES_USER
            - name: LOG_POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ez-api-secrets
                  key: LOG_POSTGRES_PASSWORD
            - name: LOG_POSTGRES_DB
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: LOG_POSTGRES_DB
            - name: EZ_LOG_PG_DSN
              value: "host=log-postgres user=$(LOG_POSTGRES_USER) password=$(LOG_POSTGRES_PASSWORD) dbname=$(LOG_POSTGRES_DB) port=5432 sslmode=disable"
            # Redis
            - name: EZ_REDIS_ADDR
              value: "redis:6379"
            - name: EZ_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: ez-api-secrets
                  key: REDIS_PASSWORD
            - name: EZ_REDIS_DB
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_REDIS_DB
            # Admin
            - name: EZ_ADMIN_TOKEN
              valueFrom:
                secretKeyRef:
                  name: ez-api-secrets
                  key: EZ_ADMIN_TOKEN
            - name: EZ_INTERNAL_STATS_TOKEN
              valueFrom:
                secretKeyRef:
                  name: ez-api-secrets
                  key: EZ_INTERNAL_STATS_TOKEN
            # Other configs
            - name: EZ_CORS_ALLOW_ORIGINS
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_CORS_ALLOW_ORIGINS
            - name: EZ_LOG_RETENTION_DAYS
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_LOG_RETENTION_DAYS
            - name: EZ_LOG_MAX_RECORDS
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_LOG_MAX_RECORDS
            - name: EZ_LOG_PARTITIONING
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_LOG_PARTITIONING
            # Sync Outbox
            - name: EZ_SYNC_OUTBOX_ENABLED
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_SYNC_OUTBOX_ENABLED
            - name: EZ_SYNC_OUTBOX_INTERVAL_SECONDS
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_SYNC_OUTBOX_INTERVAL_SECONDS
            - name: EZ_SYNC_OUTBOX_BATCH_SIZE
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_SYNC_OUTBOX_BATCH_SIZE
            - name: EZ_SYNC_OUTBOX_MAX_RETRIES
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_SYNC_OUTBOX_MAX_RETRIES
            # Swagger Host
            - name: EZ_SWAGGER_HOST
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_SWAGGER_HOST
          resources:
            requests:
              memory: "128Mi"
              cpu: "50m"
            limits:
              memory: "512Mi"
              cpu: "500m"
          livenessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /health
              port: 8080
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 5
            failureThreshold: 3
---
# ez-api Service (LoadBalancer)
apiVersion: v1
kind: Service
metadata:
  name: ez-api
  namespace: ez-api-test-dev
  labels:
    app.kubernetes.io/name: ez-api-test-dev
    app.kubernetes.io/component: ez-api
spec:
  type: LoadBalancer
  ports:
    - port: 8080
      targetPort: 8080
      protocol: TCP
      name: http
  selector:
    app: ez-api
