# Balancer Deployment (Data Plane / Gateway)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: balancer
  namespace: ez-api-test-dev
  labels:
    app.kubernetes.io/name: ez-api-test-dev
    app.kubernetes.io/component: balancer
spec:
  replicas: 1
  selector:
    matchLabels:
      app: balancer
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: balancer
        app.kubernetes.io/name: ez-api-test-dev
        app.kubernetes.io/component: balancer
    spec:
      # 等待 Redis 就绪
      initContainers:
        - 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: balancer
          image: harbor.rcfortress.site/library/balancer:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8081
              name: http
          env:
            - name: EZ_BALANCER_PORT
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_PORT
            # 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
            # Log Sink
            - name: EZ_BALANCER_LOG_SINK_ENABLED
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_LOG_SINK_ENABLED
            - name: EZ_BALANCER_LOG_SINK_BASE_URL
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_LOG_SINK_BASE_URL
            - name: EZ_BALANCER_LOG_SINK_TIMEOUT_SECONDS
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_LOG_SINK_TIMEOUT_SECONDS
            # Stats Flush
            - name: EZ_BALANCER_STATS_FLUSH_ENABLED
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_STATS_FLUSH_ENABLED
            - name: EZ_BALANCER_STATS_FLUSH_BASE_URL
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_STATS_FLUSH_BASE_URL
            - name: EZ_BALANCER_STATS_FLUSH_TOKEN
              valueFrom:
                secretKeyRef:
                  name: ez-api-secrets
                  key: EZ_BALANCER_STATS_FLUSH_TOKEN
            - name: EZ_BALANCER_STATS_FLUSH_INTERVAL_SECONDS
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_STATS_FLUSH_INTERVAL_SECONDS
            - name: EZ_BALANCER_STATS_FLUSH_BATCH_SIZE
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_STATS_FLUSH_BATCH_SIZE
            - name: EZ_BALANCER_STATS_FLUSH_TIMEOUT_SECONDS
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_STATS_FLUSH_TIMEOUT_SECONDS
            # Timeout by Provider
            - name: EZ_BALANCER_TIMEOUT_BY_PROVIDER
              valueFrom:
                configMapKeyRef:
                  name: ez-api-config
                  key: EZ_BALANCER_TIMEOUT_BY_PROVIDER
          resources:
            requests:
              memory: "128Mi"
              cpu: "50m"
            limits:
              memory: "512Mi"
              cpu: "500m"
          livenessProbe:
            httpGet:
              path: /health
              port: 8081
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /health
              port: 8081
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 5
            failureThreshold: 3
---
# Balancer Service (LoadBalancer)
apiVersion: v1
kind: Service
metadata:
  name: balancer
  namespace: ez-api-test-dev
  labels:
    app.kubernetes.io/name: ez-api-test-dev
    app.kubernetes.io/component: balancer
spec:
  type: LoadBalancer
  ports:
    - port: 8081
      targetPort: 8081
      protocol: TCP
      name: http
  selector:
    app: balancer
