# ===============================================
# Reconciliation Service Kubernetes部署配置
# ===============================================

apiVersion: v1
kind: Namespace
metadata:
  name: erp
  labels:
    app.kubernetes.io/name: erp

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: reconciliation-service-config
  namespace: erp
data:
  SPRING_PROFILES_ACTIVE: "prod"
  DB_HOST: "mysql.erp.svc.cluster.local"
  DB_PORT: "3306"
  DB_NAME: "erp_db"
  NACOS_SERVER_ADDR: "nacos.erp.svc.cluster.local:8848"
  LOG_LEVEL: "INFO"

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: reconciliation-service
  namespace: erp
  labels:
    app: reconciliation-service
    version: v1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: reconciliation-service
  template:
    metadata:
      labels:
        app: reconciliation-service
        version: v1
    spec:
      containers:
        - name: reconciliation-service
          image: erp-reconciliation-service:1.0.0
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 8018
              protocol: TCP
          envFrom:
            - configMapRef:
                name: reconciliation-service-config
          env:
            - name: DB_USERNAME
              valueFrom:
                secretKeyRef:
                  name: erp-db-secret
                  key: username
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: erp-db-secret
                  key: password
          resources:
            requests:
              memory: "256Mi"
              cpu: "100m"
            limits:
              memory: "768Mi"
              cpu: "500m"
          livenessProbe:
            httpGet:
              path: /actuator/health
              port: http
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 10
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /actuator/health
              port: http
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
          volumeMounts:
            - name: timezone
              mountPath: /etc/localtime
              readOnly: true
      volumes:
        - name: timezone
          hostPath:
            path: /etc/localtime
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 100
              podAffinityTerm:
                labelSelector:
                  matchExpressions:
                    - key: app
                      operator: In
                      values:
                        - reconciliation-service
                topologyKey: kubernetes.io/hostname

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

---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: reconciliation-service-hpa
  namespace: erp
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: reconciliation-service
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: reconciliation-service-ingress
  namespace: erp
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/proxy-body-size: "100m"
spec:
  ingressClassName: nginx
  rules:
    - host: api.erpzbbh.cn
      http:
        paths:
          - path: /api/reconciliation
            pathType: Prefix
            backend:
              service:
                name: reconciliation-service
                port:
                  name: http
