apiVersion: apps/v1
kind: Deployment
metadata:
  name: erp-file-service
  namespace: erp
  labels:
    app: erp-file-service
    version: v1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: erp-file-service
  template:
    metadata:
      labels:
        app: erp-file-service
        version: v1
    spec:
      containers:
        - name: file-service
          image: erp-file-service:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8082
              name: http
          env:
            - name: SPRING_PROFILES_ACTIVE
              value: "k8s"
            - name: SPRING_DATASOURCE_URL
              value: "jdbc:mysql://mysql.erp.svc.cluster.local:3306/erp_db?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai"
            - name: SPRING_DATASOURCE_USERNAME
              valueFrom:
                secretKeyRef:
                  name: erp-db-secret
                  key: username
            - name: SPRING_DATASOURCE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: erp-db-secret
                  key: password
            - name: SPRING_DATA_REDIS_HOST
              value: "redis.erp.svc.cluster.local"
            - name: SPRING_DATA_REDIS_PORT
              value: "6379"
            - name: SPRING_DATA_REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: erp-redis-secret
                  key: password
            - name: FILE_STORAGE_DEFAULT
              value: "local"
            - name: FILE_STORAGE_LOCAL_BASE_PATH
              value: "/var/erp/uploads"
            - name: FILE_STORAGE_LOCAL_BASE_URL
              value: "http://file-service.erp.svc.cluster.local:8082/files"
          resources:
            requests:
              memory: "256Mi"
              cpu: "100m"
            limits:
              memory: "512Mi"
              cpu: "500m"
          volumeMounts:
            - name: file-storage
              mountPath: /var/erp/uploads
          livenessProbe:
            httpGet:
              path: /actuator/health
              port: 8082
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 10
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /actuator/health
              port: 8082
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            failureThreshold: 3
      volumes:
        - name: file-storage
          persistentVolumeClaim:
            claimName: erp-file-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: erp-file-service
  namespace: erp
  labels:
    app: erp-file-service
spec:
  type: ClusterIP
  ports:
    - port: 8082
      targetPort: 8082
      protocol: TCP
      name: http
  selector:
    app: erp-file-service
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: erp-file-pvc
  namespace: erp
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 50Gi
  storageClassName: nfs-storage
