{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.worker.enabled (or (not .Values.web.enabled) (include "concourse.host" .)) }}
{{- if eq .Values.worker.mode "deployment" }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "concourse.worker.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: worker
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.worker.replicaCount }}
  {{- if .Values.worker.updateStrategy }}
  strategy: {{- toYaml .Values.worker.updateStrategy | nindent 4 }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.worker.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
      app.kubernetes.io/component: worker
  template:
    metadata:
      {{- if .Values.worker.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.worker.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/component: worker
    spec:
      serviceAccountName: {{ template "concourse.worker.serviceAccountName" . }}
      {{- include "concourse.imagePullSecrets" . | nindent 6 }}
      automountServiceAccountToken: {{ .Values.worker.automountServiceAccountToken }}
      {{- if .Values.worker.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.worker.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.affinity }}
      affinity: {{- include "common.tplvalues.render" ( dict "value" .Values.worker.affinity "context" $) | nindent 8 }}
       {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.worker.podAffinityPreset "component" "worker" "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.worker.podAntiAffinityPreset "component" "worker" "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.worker.nodeAffinityPreset.type "key" .Values.worker.nodeAffinityPreset.key "values" .Values.worker.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.worker.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.worker.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.worker.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.worker.topologySpreadConstraints "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.worker.priorityClassName }}
      priorityClassName: {{ .Values.worker.priorityClassName }}
      {{- end }}
      {{- if .Values.worker.schedulerName }}
      schedulerName: {{ .Values.worker.schedulerName | quote }}
      {{- end }}
      {{- if .Values.worker.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.worker.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.worker.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.worker.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.worker.initContainers }}
        {{- include "common.tplvalues.render" (dict "value" .Values.worker.initContainers "context" $) | nindent 8 }}
        {{- end }}
      containers:
        - name: concourse
          image: {{ template "concourse.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.worker.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.worker.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.worker.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.worker.containerSecurityContext "context" $) | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          {{- else if .Values.worker.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.worker.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - /opt/bitnami/concourse/bin/concourse
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.worker.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.worker.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - worker
          {{- end }}
          env:
            - name: CONCOURSE_TSA_PUBLIC_KEY
              value: "/bitnami/concourse/concourse-keys/host_key.pub"
            - name: CONCOURSE_TSA_WORKER_PRIVATE_KEY
              value: "/bitnami/concourse/concourse-keys/worker_key"
            - name: CONCOURSE_WORK_DIR
              value: "/bitnami/concourse/workdir"
            - name: CONCOURSE_HEALTHCHECK_BIND_PORT
              value: {{ .Values.worker.containerPorts.health | quote }}
            - name: CONCOURSE_RUNTIME
              value: {{ .Values.worker.runtime }}
            {{- if .Values.worker.logLevel }}
            - name: CONCOURSE_LOG_LEVEL
              value: {{ .Values.worker.logLevel | quote }}
            {{- end }}
            - name: CONCOURSE_TSA_HOST
              value: {{ include "concourse.web.tsa.address" . | quote }}
            {{- if .Values.worker.bindIp }}
            - name: CONCOURSE_BIND_IP
              value: {{ .Values.worker.bindIp | quote }}
            {{- end }}
            {{- if .Values.worker.containerPorts.garden }}
            - name: CONCOURSE_BIND_PORT
              value: {{ .Values.worker.containerPorts.garden | quote }}
            {{- end }}
            {{- if .Values.worker.baggageclaim.logLevel }}
            - name: CONCOURSE_BAGGAGECLAIM_LOG_LEVEL
              value: {{ .Values.worker.baggageclaim.logLevel | quote }}
            {{- end }}
            {{- if .Values.worker.baggageclaim.bindIp }}
            - name: CONCOURSE_BAGGAGECLAIM_BIND_IP
              value: {{ .Values.worker.baggageclaim.bindIp | quote }}
            {{- end }}
            {{- if .Values.worker.containerPorts.baggageclaim }}
            - name: CONCOURSE_BAGGAGECLAIM_BIND_PORT
              value: {{ .Values.worker.containerPorts.baggageclaim | quote }}
            {{- end }}
            {{- if .Values.worker.baggageclaim.debugBindIp }}
            - name: CONCOURSE_BAGGAGECLAIM_DEBUG_BIND_IP
              value: {{ .Values.worker.baggageclaim.debugBindIp | quote }}
            {{- end }}
            {{- if .Values.worker.containerPorts.pprof }}
            - name: CONCOURSE_BAGGAGECLAIM_DEBUG_BIND_PORT
              value: {{ .Values.worker.containerPorts.pprof | quote }}
            {{- end }}
            {{- if .Values.worker.baggageclaim.volumes }}
            - name: CONCOURSE_BAGGAGECLAIM_VOLUMES
              value: {{ .Values.worker.baggageclaim.volumes | quote }}
            {{- end }}
            {{- if .Values.worker.baggageclaim.driver }}
            - name: CONCOURSE_BAGGAGECLAIM_DRIVER
              value: {{ .Values.worker.baggageclaim.driver | quote }}
            {{- end }}
            {{- if .Values.worker.baggageclaim.btrfsBin }}
            - name: CONCOURSE_BAGGAGECLAIM_BTRFS_BIN
              value: {{ .Values.worker.baggageclaim.btrfsBin | quote }}
            {{- end }}
            {{- if .Values.worker.baggageclaim.mkfsBin }}
            - name: CONCOURSE_BAGGAGECLAIM_MKFS_BIN
              value: {{ .Values.worker.baggageclaim.mkfsBin | quote }}
            {{- end }}
            {{- if .Values.worker.baggageclaim.overlaysDir }}
            - name: CONCOURSE_BAGGAGECLAIM_OVERLAYS_DIR
              value: {{ .Values.worker.baggageclaim.overlaysDir | quote }}
            {{- end }}
            {{- if .Values.worker.baggageclaim.disableUserNamespaces }}
            - name: CONCOURSE_BAGGAGECLAIM_DISABLE_USER_NAMESPACES
              value: {{ .Values.worker.baggageclaim.disableUserNamespaces | quote }}
            {{- end }}
            {{- if .Values.worker.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.worker.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.worker.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.worker.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.worker.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.worker.resources }}
          resources: {{- toYaml .Values.worker.resources | nindent 12 }}
          {{- else if ne .Values.worker.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.worker.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.worker.containerPorts.garden }}
            - name: healthz
              containerPort: {{ .Values.worker.containerPorts.health }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.worker.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.worker.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.worker.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.worker.startupProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: healthz
          {{- end }}
          {{- if .Values.worker.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.worker.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.worker.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.worker.livenessProbe "enabled") "context" $) | nindent 12 }}
            tcpSocket:
              port: healthz
          {{- end }}
          {{- if .Values.worker.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.worker.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.worker.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.worker.readinessProbe "enabled") "context" $) | nindent 12 }}
            httpGet:
              path: /
              port: healthz
          {{- end }}
          {{- end }}
          volumeMounts:
            # In this kind of deployment, the state is ephemeral
            - name: empty-dir
              mountPath: /bitnami/concourse/workdir
              subPath: app-work-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: empty-dir
              mountPath: /var/run/containerd
              subPath: containerd-dir
            - name: concourse-keys
              mountPath: /bitnami/concourse/concourse-keys
              readOnly: true
          {{- if .Values.worker.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.worker.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.worker.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        - name: concourse-keys
          secret:
            secretName: {{ include "concourse.worker.secretName" . }}
            defaultMode: 0400
        {{- if .Values.worker.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.worker.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}
{{- end }}
