{{- if not .Values.application.initializeCommand -}}
apiVersion: {{ default "extensions/v1beta1" .Values.deploymentApiVersion }}
kind: Deployment
metadata:
  name: {{ template "trackableappname" . }}
  annotations:
    {{ if .Values.gitlab.app }}app.gitlab.com/app: {{ .Values.gitlab.app | quote }}{{ end }}
    {{ if .Values.gitlab.env }}app.gitlab.com/env: {{ .Values.gitlab.env | quote }}{{ end }}
  labels:
    app: {{ template "appname" . }}
    track: "{{ .Values.application.track }}"
    tier: "{{ .Values.application.tier }}"
    chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
{{- if or .Values.enableSelector (eq (default "extensions/v1beta1" .Values.deploymentApiVersion) "apps/v1") }}
  selector:
    matchLabels:
      app: {{ template "appname" . }}
      track: "{{ .Values.application.track }}"
      tier: "{{ .Values.application.tier }}"
      release: {{ .Release.Name }}
{{- end }}
  replicas: {{ .Values.replicaCount }}
{{- if .Values.strategyType }}
  strategy:
    type: {{ .Values.strategyType | quote }}
{{- end }}
  template:
    metadata:
      annotations:
        checksum/application-secrets: "{{ .Values.application.secretChecksum }}"
        {{ if .Values.gitlab.app }}app.gitlab.com/app: {{ .Values.gitlab.app | quote }}{{ end }}
        {{ if .Values.gitlab.env }}app.gitlab.com/env: {{ .Values.gitlab.env | quote }}{{ end }}
{{- if .Values.podAnnotations }}
{{ toYaml .Values.podAnnotations | indent 8 }}
{{- end }}
      labels:
        app: {{ template "appname" . }}
        track: "{{ .Values.application.track }}"
        tier: "{{ .Values.application.tier }}"
        release: {{ .Release.Name }}
    spec:
      imagePullSecrets:
{{ toYaml .Values.image.secrets | indent 10 }}
      volumes:
      - name: env-files
        secret:
          secretName: {{ .Values.application.secretName }}
      containers:
      - name: {{ .Chart.Name }}-phpfpm
        image: {{ template "imagename" . }}
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        {{- if .Values.application.secretName }}
        envFrom:
        - secretRef:
            name: {{ .Values.application.secretName }}
        {{- end }}
        env:
        - name: DATABASE_URL
          value: {{ .Values.application.database_url | quote }}
        - name: GITLAB_ENVIRONMENT_NAME
          value: {{ .Values.gitlab.envName | quote }}
        - name: GITLAB_ENVIRONMENT_URL
          value: {{ .Values.gitlab.envURL | quote }}
        volumeMounts:
        - name: env-files
          mountPath: /html/.env
          subPath: ENV_FILE
          readOnly: true
        ports:
        - name: "{{ .Values.service.name }}-phpfpm"
          containerPort: 9000
        livenessProbe:
          tcpSocket:
            port: "{{ .Values.service.name }}-phpfpm"
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
        readinessProbe:
          tcpSocket:
            port: "{{ .Values.service.name }}-phpfpm"
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
        resources:
{{ toYaml .Values.resources | indent 12 }}
      # NGINX
      - name: {{ .Chart.Name }}-nginx
        image: {{ template "imagename" . }}
        command: ["nginx"]
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        {{- if .Values.application.secretName }}
        envFrom:
        - secretRef:
            name: {{ .Values.application.secretName }}
        {{- end }}
        env:
        - name: DATABASE_URL
          value: {{ .Values.application.database_url | quote }}
        - name: GITLAB_ENVIRONMENT_NAME
          value: {{ .Values.gitlab.envName | quote }}
        - name: GITLAB_ENVIRONMENT_URL
          value: {{ .Values.gitlab.envURL | quote }}
        ports:
        - name: "{{ .Values.service.name }}"
          containerPort: {{ .Values.service.internalPort }}
        livenessProbe:
          httpGet:
            path: {{ .Values.livenessProbe.path }}
            scheme: {{ .Values.livenessProbe.scheme }}
            port: {{ .Values.service.internalPort }}
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
        readinessProbe:
          httpGet:
            path: {{ .Values.readinessProbe.path }}
            scheme: {{ .Values.readinessProbe.scheme }}
            port: {{ .Values.service.internalPort }}
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
        resources:
          requests:
            cpu: 100m
            memory: 100M  
      - name: {{ .Chart.Name }}-worker
        image: {{ template "imagename" . }}
        command: ["su"]
        args: ["-s", "/bin/sh", "-c", "php artisan requests:fetcher", "nginx"]
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        {{- if .Values.application.secretName }}
        envFrom:
        - secretRef:
            name: {{ .Values.application.secretName }}
        {{- end }}
        env:
        - name: DATABASE_URL
          value: {{ .Values.application.database_url | quote }}
        - name: GITLAB_ENVIRONMENT_NAME
          value: {{ .Values.gitlab.envName | quote }}
        - name: GITLAB_ENVIRONMENT_URL
          value: {{ .Values.gitlab.envURL | quote }}
        volumeMounts:
        - name: env-files
          mountPath: /html/.env
          subPath: ENV_FILE
          readOnly: true
        livenessProbe:
          exec:
            command:
            - cat
            - /tmp/fetcher
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
        readinessProbe:
          exec:
            command:
            - cat
            - /tmp/fetcher
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
        resources:
          requests:
            cpu: 1
            memory: 1Gi 
      # REDIS
      - name: {{ .Chart.Name }}-redis
        image: "redis:5.0.3-alpine"
        imagePullPolicy: {{ .Values.image.pullPolicy }}
        ports:
        - name: "{{ .Values.service.name }}-redis"
          containerPort: 6379
        livenessProbe:
          tcpSocket:
            port: "{{ .Values.service.name }}-redis"
          initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
        readinessProbe:
          tcpSocket:
            port: "{{ .Values.service.name }}-redis"
          initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
          timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
        resources:
          requests:
            cpu: 100m
            memory: 500M  
{{- end -}}
