apiVersion: apps/v1
kind: Deployment
metadata:
  name: maya-global-backend
  labels:
    app: maya-global-backend
spec:
  replicas: 5
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 0
  selector:
    matchLabels:
      app: maya-global-backend
  template:
    metadata:
      labels:
        app: maya-global-backend
    spec:
      terminationGracePeriodSeconds: 30
      imagePullSecrets:
      - name: ar-pull-secret
      containers:
      - name: maya-global-backend
        image: asia-southeast1-docker.pkg.dev/mayaresearchgcp/maya-global-backend/maya-global-backend:latest
        imagePullPolicy: Always
        ports:
        - containerPort: 7860
        resources:
          requests:
            memory: "6Gi"
            cpu: "2000m"
          limits:
            memory: "8Gi"
            cpu: "8000m"
        livenessProbe:
          httpGet:
            path: /health
            port: 7860
          initialDelaySeconds: 30
          periodSeconds: 15
          timeoutSeconds: 15
        readinessProbe:
          httpGet:
            path: /health
            port: 7860
          initialDelaySeconds: 15
          periodSeconds: 15
          timeoutSeconds: 5
          failureThreshold: 3
          successThreshold: 1
        envFrom:
        - secretRef:
            name: maya-global-env-secrets
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        volumeMounts:
        - name: gcp-creds
          mountPath: /app/gcp-service-account.json
          subPath: gcp-service-account.json
          readOnly: true
        - name: gcp-creds
          mountPath: /app/google-play-service-account.json
          subPath: google-play-service-account.json
          readOnly: true
      volumes:
      - name: gcp-creds
        secret:
          secretName: gcp-credentials
---
apiVersion: v1
kind: Service
metadata:
  name: maya-global-backend
  labels:
    app: maya-global-backend
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 7860
    protocol: TCP
  selector:
    app: maya-global-backend
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: maya-global-backend-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: maya-global-backend
  minReplicas: 5
  maxReplicas: 7
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80
  behavior:
    scaleUp:
      stabilizationWindowSeconds: 30
      policies:
      - type: Pods
        value: 2
        periodSeconds: 30
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
      - type: Pods
        value: 1
        periodSeconds: 60
