apiVersion: apps/v1
kind: Deployment
metadata:
  name: tenant-service
  namespace: erp
  labels:
    app: tenant-service
    version: v1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: tenant-service
  template:
    metadata:
      labels:
        app: tenant-service
        version: v1
    spec:
      containers:
        - name: tenant-service
          image: registry.example.com/tenant-service:latest
          ports:
            - containerPort: 8084
              name: http
          env:
            - name: SPRING_PROFILES_ACTIVE
              value: "prod"
            - name: SPRING_DATASOURCE_URL
              value: "jdbc:mysql://mysql.erp.svc.cluster.local:3306/erp_db"
            - name: SPRING_DATASOURCE_USERNAME
              value: "root"
            - name: SPRING_DATASOURCE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: erp-db-secret
                  key: password
            - name: SPRING_REDIS_HOST
              value: "redis.erp.svc.cluster.local"
            - name: SPRING_REDIS_PORT
              value: "6379"
            - name: SPRING_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: erp-redis-secret
                  key: password
            - name: SPRING_CLOUD_NACOS_DISCOVERY_SERVER-ADDR
              value: "nacos.erp.svc.cluster.local:8848"
            - name: SPRING_CLOUD_NACOS_CONFIG_SERVER-ADDR
              value: "nacos.erp.svc.cluster.local:8848"
          resources:
            requests:
              memory: "256Mi"
              cpu: "100m"
            limits:
              memory: "512Mi"
              cpu: "500m"
          livenessProbe:
            httpGet:
              path: /actuator/health
              port: 8084
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /actuator/health
              port: 8084
            initialDelaySeconds: 30
            periodSeconds: 5
            timeoutSeconds: 3
            successThreshold: 1
            failureThreshold: 3
---
apiVersion: v1
kind: Service
metadata:
  name: tenant-service
  namespace: erp
  labels:
    app: tenant-service
spec:
  type: ClusterIP
  ports:
    - port: 8084
      targetPort: 8084
      protocol: TCP
      name: http
  selector:
    app: tenant-service
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: tenant-service-hpa
  namespace: erp
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: tenant-service
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
