# Kubernetes Deployment for Report Service
apiVersion: apps/v1
kind: Deployment
metadata:
  name: erp-report-service
  namespace: erp
  labels:
    app: erp-report-service
    version: v1.0.0
spec:
  replicas: 2
  selector:
    matchLabels:
      app: erp-report-service
  template:
    metadata:
      labels:
        app: erp-report-service
        version: v1.0.0
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "8086"
        prometheus.io/path: "/report/actuator/prometheus"
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: app
                      operator: In
                      values:
                        - erp-report-service
                topologyKey: kubernetes.io/hostname
      containers:
        - name: report-service
          image: registry.example.com/erp/report-service:1.0.0
          imagePullPolicy: Always
          ports:
            - name: http
              containerPort: 8086
              protocol: TCP
          env:
            - name: SPRING_PROFILES_ACTIVE
              value: "prod"
            - name: NACOS_SERVER_ADDR
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: nacos.server.addr
            - name: NACOS_NAMESPACE
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: nacos.namespace
            - name: NACOS_GROUP
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: nacos.group
            - name: PROFILE
              value: "prod"
            - name: TZ
              value: "Asia/Shanghai"
          resources:
            requests:
              memory: "512Mi"
              cpu: "250m"
            limits:
              memory: "1Gi"
              cpu: "1000m"
          livenessProbe:
            httpGet:
              path: /report/actuator/health/liveness
              port: http
            initialDelaySeconds: 60
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /report/actuator/health/readiness
              port: http
            initialDelaySeconds: 30
            periodSeconds: 5
            timeoutSeconds: 3
            failureThreshold: 3
          lifecycle:
            preStop:
              exec:
                command:
                  - sh
                  - -c
                  - sleep 10
      imagePullSecrets:
        - name: erp-registry-secret

---
apiVersion: v1
kind: Service
metadata:
  name: erp-report-service
  namespace: erp
  labels:
    app: erp-report-service
spec:
  type: ClusterIP
  ports:
    - name: http
      port: 8086
      targetPort: http
      protocol: TCP
  selector:
    app: erp-report-service

---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: erp-report-service-hpa
  namespace: erp
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: erp-report-service
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
        - type: Percent
          value: 10
          periodSeconds: 60
    scaleUp:
      stabilizationWindowSeconds: 0
      policies:
        - type: Percent
          value: 100
          periodSeconds: 15
        - type: Pods
          value: 4
          periodSeconds: 15
      selectPolicy: Max

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: erp-report-service-ingress
  namespace: erp
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "10m"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "60"
    nginx.ingress.kubernetes.io/rewrite-target: /report/$2
spec:
  ingressClassName: nginx
  rules:
    - host: api.erpzbbh.cn
      http:
        paths:
          - path: /report-service(/|$)(.*)
            pathType: ImplementationSpecific
            backend:
              service:
                name: erp-report-service
                port:
                  number: 8086
