apiVersion: apps/v1
kind: Deployment
metadata:
  name: django
  namespace: saleor
  labels:
    app: saleor
    component: server
spec:
  strategy:
    type: Recreate
  replicas: 1
  selector:
    matchLabels:
      app: saleor
      component: server
  template:
    metadata:
      labels:
        app: saleor
        component: server
    spec:
      initContainers:
        - name: database
          image: ariellahiany/saleor:latest
          imagePullPolicy: Always
          command: ["python", "manage.py", "migrate"]
          env:
            - name: DEBUG
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: DEBUG
            - name: ALLOWED_HOSTS
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: ALLOWED_HOSTS
            - name: ALLOWED_CLIENT_HOSTS
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: ALLOWED_CLIENT_HOSTS
            - name: SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: django
                  key: SECRET_KEY
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: postgres
                  key: POSTGRES_DB
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres
                  key: POSTGRES_PASSWORD
        - name: static
          image: ariellahiany/saleor:latest
          imagePullPolicy: Always
          command: ["python", "manage.py", "collectstatic", "--noinput"]
          # todo: need to give a password and a username for Redis.
          env:
            - name: DEBUG
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: DEBUG
            - name: ALLOWED_HOSTS
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: ALLOWED_HOSTS
            - name: ALLOWED_CLIENT_HOSTS
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: ALLOWED_CLIENT_HOSTS
            - name: SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: django
                  key: SECRET_KEY
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: postgres
                  key: POSTGRES_DB
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres
                  key: POSTGRES_PASSWORD
      containers:
        - name: django
          image: ariellahiany/saleor:latest
          imagePullPolicy: Always
          ports:
            - name: django
              protocol: TCP
              containerPort: 8000
          env:
            - name: DEBUG
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: DEBUG
            - name: ALLOWED_HOSTS
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: ALLOWED_HOSTS
            - name: ALLOWED_CLIENT_HOSTS
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: ALLOWED_CLIENT_HOSTS
            - name: CELERY_BROKER_URL
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: CELERY_BROKER_URL
            - name: DASHBOARD_URL
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: DASHBOARD_URL
            - name: STOREFRONT_URL
              valueFrom:
                configMapKeyRef:
                  name: django
                  key: STOREFRONT_URL
            - name: SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: django
                  key: SECRET_KEY
            - name: POSTGRES_DB
              valueFrom:
                secretKeyRef:
                  name: postgres
                  key: POSTGRES_DB
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: postgres
                  key: POSTGRES_USER
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: postgres
                  key: POSTGRES_PASSWORD
          args: ["gunicorn", "--bind", ":8000", "--workers", "1", "--worker-class", "uvicorn.workers.UvicornWorker" ,"saleor.asgi:application"]
# todo: need to create that with NFS(?)
#          volumeMounts:
#            - name: media
#              mountPath: /saleor/media
#      volumes:
#        - name: media
#          persistentVolumeClaim:
#            claimName: media
