apiVersion: apps/v1
kind: Deployment
metadata:
  name: api
  labels:
    app: api
    component: api
spec:
  replicas: 3
  selector:
    matchLabels:
      app: api
      component: api
  template:
    metadata:
      labels:
        app: api
        component: api
    spec:
      securityContext:
        fsGroup: 1000
      initContainers:
        - name: prepare-storage
          image: ghcr.io/tpo-project/e-commerce-api:2.x.x-latest
          command:
            - "/bin/sh"
            - "-c"
            - "if [ -z \"$(ls -A /storage/logs)\" ]; then \
                cp -R /var/www/html/storage/* /storage; \
                chown -R nginx:nginx /storage; \
              fi"
          volumeMounts:
            - name: storage
              mountPath: /storage
      containers:
        - name: api
          image: ghcr.io/tpo-project/e-commerce-api:2.x.x-latest
          ports:
            - containerPort: 80
          envFrom:
            - configMapRef:
                name: api-config
            - secretRef:
                name: api-secret
            - configMapRef:
                name: postgres-config
            - secretRef:
                name: postgres-secret
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/conf.d/default.conf
              subPath: default.conf
            - name: storage
              mountPath: /var/www/html/storage
          resources:
            limits:
              cpu: "150m"
              memory: "128Mi"
      volumes:
        - name: nginx-config
          configMap:
            name: nginx-config
            defaultMode: 0600
        - name: storage
          persistentVolumeClaim:
            claimName: api-storage
