apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "plateforme-ebs" . }}
  labels:
    {{- include "plateforme-ebs.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "plateforme-ebs.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "plateforme-ebs.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "plateforme-ebs.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: {{ .Chart.Name }}-chown
          securityContext:
            runAsUser: 0
          image: "{{ .Values.php.image.repository }}:{{ .Values.php.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.php.image.pullPolicy }}
          command: ['/bin/sh', '-c']
          args: ['set -ex; mkdir -p public/storage/uploads/category ; mkdir -p public/storage/uploads/menu ; mkdir -p public/storage/uploads/product ; mkdir -p public/storage/uploads/user ; chown -R www-data: public/storage/']
          volumeMounts:
            - mountPath: /srv/app/public/storage
              name: storage
      containers:
        - name: {{ .Chart.Name }}-caddy
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.caddy.image.repository }}:{{ .Values.caddy.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.caddy.image.pullPolicy }}
          env:
            - name: SERVER_NAME
              value: :80
            - name: PWA_UPSTREAM
              value: {{ include "plateforme-ebs" . }}-pwa:3000
            - name: MERCURE_EXTRA_DIRECTIVES
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mercure-extra-directives
            - name: MERCURE_PUBLISHER_JWT_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mercure-jwt-secret
            - name: MERCURE_SUBSCRIBER_JWT_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mercure-jwt-secret
          ports:
            - name: http
              containerPort: 80
              protocol: TCP
            - name: admin
              containerPort: 2019
              protocol: TCP
          volumeMounts:
            - mountPath: /srv/app/public/storage
              name: storage
            - mountPath: /var/run/php
              name: php-socket
          lifecycle:
            preStop:
              exec:
                command: ["curl", "-XPOST", "http://localhost:2019/stop"]
          readinessProbe:
            tcpSocket:
              port: 80
            initialDelaySeconds: 3
          livenessProbe:
            tcpSocket:
              port: 80
            initialDelaySeconds: 3
          resources:
            {{- toYaml .Values.resources.caddy | nindent 12 }}
        - name: {{ .Chart.Name }}-php
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.php.image.repository }}:{{ .Values.php.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.php.image.pullPolicy }}
          env:
            - name: API_ENTRYPOINT_HOST
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-host
            - name: JWT_PASSPHRASE
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-jwt-passphrase
            - name: JWT_PUBLIC_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-jwt-public-key
            - name: JWT_SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-jwt-secret-key
            - name: TRUSTED_HOSTS
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-trusted-hosts
            - name: TRUSTED_PROXIES
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-trusted-proxies
            - name: APP_ENV
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-app-env
            - name: APP_DEBUG
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-app-debug
            - name: APP_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-app-secret
            - name: CORS_ALLOW_ORIGIN
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-cors-allow-origin
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: database-url
            - name: MAILER_DSN
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mailer-dsn
            - name: MERCURE_URL
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mercure-url
            - name: MERCURE_PUBLIC_URL
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mercure-public-url
            - name: MERCURE_JWT_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mercure-jwt-secret
            {{- if .Values.meilisearch.enabled }}
            - name: MEILISEARCH_API_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ printf "%s-%s" (include "meilisearch.fullname" .Subcharts.meilisearch ) "master-key" }}
                  key: MEILI_MASTER_KEY
            - name: MEILISEARCH_URL
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: meilisearch-url
            {{- end }}
            {{- if .Values.redis.enabled }}
            - name: REDIS_URL
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: redis-url
            {{- end }}
            - name: SMS_DSN
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: sms-dsn
            - name: PAYUM_APIKEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: payum-apikey
            - name: PAYUM_GATEWAY
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: payum-gateway
            - name: STORAGE_BUCKET
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-storage-bucket
            - name: STORAGE_ENDPOINT
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-storage-endpoint
            - name: STORAGE_REGION
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-storage-region
            - name: STORAGE_USE_PATH_STYLE_ENDPOINT
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-storage-use-path-style-endpoint
            - name: STORAGE_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-storage-key
            - name: STORAGE_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-storage-secret
          volumeMounts:
            - mountPath: /srv/app/public/storage
              name: storage
            - mountPath: /var/run/php
              name: php-socket
          lifecycle:
            preStop:
              exec:
                command: ["/bin/sh", "-c", "/bin/sleep 1; kill -QUIT 1"]
          startupProbe:
            exec:
              command:
                - docker-healthcheck
            failureThreshold: 40
          readinessProbe:
            exec:
              command:
                - docker-healthcheck
          livenessProbe:
            exec:
              command:
                - docker-healthcheck
          resources:
            {{- toYaml .Values.resources.php | nindent 12 }}
        {{- if .Values.consumer.enabled }}
        - name: {{ .Chart.Name }}-consumer
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.php.image.repository }}:{{ .Values.php.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.php.image.pullPolicy }}
          args: ['bin/console', 'messenger:consume']
          env:
            - name: API_ENTRYPOINT_HOST
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-host
            - name: JWT_PASSPHRASE
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-jwt-passphrase
            - name: JWT_PUBLIC_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-jwt-public-key
            - name: JWT_SECRET_KEY
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-jwt-secret-key
            - name: TRUSTED_HOSTS
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-trusted-hosts
            - name: TRUSTED_PROXIES
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-trusted-proxies
            - name: APP_ENV
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-app-env
            - name: APP_DEBUG
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-app-debug
            - name: APP_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-app-secret
            - name: CORS_ALLOW_ORIGIN
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: php-cors-allow-origin
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: database-url
            - name: MERCURE_URL
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mercure-url
            - name: MERCURE_PUBLIC_URL
              valueFrom:
                configMapKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mercure-public-url
            - name: MERCURE_JWT_SECRET
              valueFrom:
                secretKeyRef:
                  name: {{ include "plateforme-ebs" . }}
                  key: mercure-jwt-secret
          startupProbe:
            exec:
              command: ['pgrep', '-f', 'php bin/console messenger:consume']
            failureThreshold: 40
          readinessProbe:
            exec:
              command: ['pgrep', '-f', 'php bin/console messenger:consume']
          livenessProbe:
            exec:
              command: ['pgrep', '-f', 'php bin/console messenger:consume']
          resources:
            {{- toYaml .Values.resources.consumer | nindent 12 }}
        {{- end }}
      volumes:
      - name: storage
        # TODO: pvc option?
        emptyDir: {}
      - name: php-socket
        emptyDir: {}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
