{{- if and (not .Values.application.initializeCommand) .Values.workers -}}
apiVersion: v1
kind: List
items:
{{- range $workerName, $workerConfig :=  .Values.workers }}
- apiVersion: {{ default "extensions/v1beta1" $.Values.deploymentApiVersion }}
  kind: Deployment
  metadata:
    name: {{ template "trackableappname" $ }}-{{ $workerName }}
    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:
      track: "{{ $.Values.application.track }}"
      tier: worker
      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:
        track: "{{ $.Values.application.track }}"
        tier: worker
        release: {{ $.Release.Name }}
  {{- end }}
    replicas: {{ $workerConfig.replicaCount }}
  {{- if $workerConfig.strategyType }}
    strategy:
      type: {{ $workerConfig.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 10 }}
  {{- end }}
        labels:
          track: "{{ $.Values.application.track }}"
          tier: worker
          release: {{ $.Release.Name }}
      spec:
        imagePullSecrets:
  {{ toYaml $.Values.image.secrets | indent 12 }}
        terminationGracePeriodSeconds: {{ $workerConfig.terminationGracePeriodSeconds }}
        containers:
        - name: {{ $.Chart.Name }}-{{ $workerName }}
          image: {{ template "imagename" $ }}
          command:
          {{- range $workerConfig.command }}
          - {{ . }}
          {{- end }}
          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 }}
          livenessProbe:
{{- if eq $.Values.livenessProbe.probeType "httpGet" }}
            httpGet:
              path: {{ $.Values.livenessProbe.path }}
              scheme: {{ $.Values.livenessProbe.scheme }}
              port: {{ $.Values.service.internalPort }}
{{- else if eq $.Values.livenessProbe.probeType "tcpSocket" }}
            tcpSocket:
              port: {{ $.Values.service.internalPort }}
{{- end }}
            initialDelaySeconds: {{ $.Values.livenessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ $.Values.livenessProbe.timeoutSeconds }}
          readinessProbe:
{{- if eq $.Values.readinessProbe.probeType "httpGet" }}
            httpGet:
              path: {{ $.Values.readinessProbe.path }}
              scheme: {{ $.Values.readinessProbe.scheme }}
              port: {{ $.Values.service.internalPort }}
{{- else if eq $.Values.readinessProbe.probeType "tcpSocket" }}
            tcpSocket:
              port: {{ $.Values.service.internalPort }}
{{- end }}
            initialDelaySeconds: {{ $.Values.readinessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ $.Values.readinessProbe.timeoutSeconds }}
          {{- if $workerConfig.preStopCommand }}
          lifecycle:
            preStop:
              exec:
                command:
                {{- range $workerConfig.preStopCommand }}
                - {{ . }}
                {{- end }}
          {{- end }}
          resources:
{{ toYaml $.Values.resources | indent 12 }}
{{- end -}}
{{- end -}}
