{{- if .Values.worker.enabled -}}
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: {{ template "concourse.worker.fullname" . }}
  labels:
    app: {{ template "concourse.worker.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
spec:
  serviceName: {{ template "concourse.worker.fullname" . }}
  replicas: {{ .Values.worker.replicas }}
  template:
    metadata:
      labels:
        app: {{ template "concourse.worker.fullname" . }}
        release: "{{ .Release.Name }}"
        {{- with .Values.worker.labels }}
{{ toYaml . | trim | indent 8 }}
        {{- end }}
      {{- if .Values.worker.annotations }}
      annotations:
{{ toYaml .Values.worker.annotations | indent 8 }}
      {{- end }}
    spec:
    {{- if .Values.worker.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.worker.nodeSelector | indent 8 }}
    {{- end }}
      serviceAccountName: {{ if .Values.rbac.create }}{{ template "concourse.worker.fullname" . }}{{ else }}{{ .Values.rbac.workerServiceAccountName }}{{ end }}
      {{- if .Values.worker.tolerations }}
      tolerations:
{{ toYaml .Values.worker.tolerations | indent 8 }}
      {{- end }}
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
      {{- range .Values.imagePullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      {{- if .Values.worker.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.worker.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.worker.cleanUpWorkDirOnStart }}
      initContainers:
        - name: {{ template "concourse.worker.fullname" . }}-init-rm
          {{- if .Values.imageDigest }}
          image: "{{ .Values.image }}@{{ .Values.imageDigest }}"
          {{- else }}
          image: "{{ .Values.image }}:{{ .Values.imageTag }}"
          {{- end }}
          imagePullPolicy: {{ .Values.imagePullPolicy | quote }}
          securityContext:
            privileged: true
          command:
            - /bin/bash
          args:
            - -ce
            - |-
              for v in $((btrfs subvolume list --sort=-ogen "{{ .Values.concourse.worker.workDir }}" || true) | awk '{print $9}'); do
                (btrfs subvolume show "{{ .Values.concourse.worker.workDir }}/$v" && btrfs subvolume delete "{{ .Values.concourse.worker.workDir }}/$v") || true
              done
              rm -rf "{{ .Values.concourse.worker.workDir }}"/*
          volumeMounts:
            - name: concourse-work-dir
              mountPath: {{ .Values.concourse.worker.workDir | quote }}
      {{- end }}
      containers:
      {{- if .Values.worker.sidecarContainers }}
      {{- toYaml .Values.worker.sidecarContainers | nindent 8 }}
      {{- end }}
        - name: {{ template "concourse.worker.fullname" . }}
          {{- if .Values.imageDigest }}
          image: "{{ .Values.image }}@{{ .Values.imageDigest }}"
          {{- else }}
          image: "{{ .Values.image }}:{{ .Values.imageTag }}"
          {{- end }}
          imagePullPolicy: {{ .Values.imagePullPolicy | quote }}
          args:
            - worker
{{- if .Values.worker.livenessProbe }}
          livenessProbe:
{{ toYaml .Values.worker.livenessProbe | indent 12 }}
{{- end }}
{{- if .Values.worker.readinessProbe }}
          readinessProbe:
{{ toYaml .Values.worker.readinessProbe | indent 12 }}
{{- end }}
          lifecycle:
            preStop:
              exec:
                command:
                  - "/bin/bash"
                  - "/pre-stop-hook.sh"
          env:
            {{- if .Values.concourse.worker.rebalanceInterval }}
            - name: CONCOURSE_REBALANCE_INTERVAL
              value: {{ .Values.concourse.worker.rebalanceInterval | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.sweepInterval }}
            - name: CONCOURSE_SWEEP_INTERVAL
              value: {{ .Values.concourse.worker.sweepInterval | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.resourceTypes }}
            - name: CONCOURSE_RESOURCE_TYPES
              value: {{ .Values.concourse.worker.resourceTypes | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.connectionDrainTimeout }}
            - name: CONCOURSE_CONNECTION_DRAIN_TIMEOUT
              value: {{ .Values.concourse.worker.connectionDrainTimeout | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.healthcheckBindIp }}
            - name: CONCOURSE_HEALTHCHECK_BIND_IP
              value: {{ .Values.concourse.worker.healthcheckBindIp | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.healthcheckBindPort }}
            - name: CONCOURSE_HEALTHCHECK_BIND_PORT
              value: {{ .Values.concourse.worker.healthcheckBindPort | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.healthcheckTimeout }}
            - name: CONCOURSE_HEALTHCHECK_TIMEOUT
              value: {{ .Values.concourse.worker.healthcheckTimeout | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.name }}
            - name: CONCOURSE_NAME
              value: {{ .Values.concourse.worker.name | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.tag }}
            - name: CONCOURSE_TAG
              value: {{ .Values.concourse.worker.tag | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.team }}
            - name: CONCOURSE_TEAM
              value: {{ .Values.concourse.worker.team | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.http_proxy }}
            - name: http_proxy
              value: {{ .Values.concourse.worker.http_proxy | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.https_proxy }}
            - name: https_proxy
              value: {{ .Values.concourse.worker.https_proxy | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.no_proxy }}
            - name: no_proxy
              value: {{ .Values.concourse.worker.no_proxy | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.ephemeral }}
            - name: CONCOURSE_EPHEMERAL
              value: {{ .Values.concourse.worker.ephemeral | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.debugBindIp }}
            - name: CONCOURSE_DEBUG_BIND_IP
              value: {{ .Values.concourse.worker.debugBindIp | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.debugBindPort }}
            - name: CONCOURSE_DEBUG_BIND_PORT
              value: {{ .Values.concourse.worker.debugBindPort | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.certsDir }}
            - name: CONCOURSE_CERTS_DIR
              value: {{ .Values.concourse.worker.certsDir | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.workDir }}
            - name: CONCOURSE_WORK_DIR
              value: {{ .Values.concourse.worker.workDir | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.bindIp }}
            - name: CONCOURSE_BIND_IP
              value: {{ .Values.concourse.worker.bindIp | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.bindPort }}
            - name: CONCOURSE_BIND_PORT
              value: {{ .Values.concourse.worker.bindPort | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.logLevel }}
            - name: CONCOURSE_LOG_LEVEL
              value: {{ .Values.concourse.worker.logLevel | quote }}
            {{- end }}
            {{ if not .Values.web.enabled }}
            - name: CONCOURSE_TSA_HOST
              value: "{{- range $i, $tsaHost := .Values.concourse.worker.tsa.hosts }}{{- if $i }},{{ end }}{{- $tsaHost }}{{- end -}}"
            {{ else }}
            - name: CONCOURSE_TSA_HOST
              value: "{{ template "concourse.web.tsa.address" . -}}"
            {{- end }}
            - name: CONCOURSE_TSA_PUBLIC_KEY
              value: "{{ .Values.worker.keySecretsPath }}/host_key.pub"
            - name: CONCOURSE_TSA_WORKER_PRIVATE_KEY
              value: "{{ .Values.worker.keySecretsPath }}/worker_key"
            {{- if .Values.concourse.worker.externalGardenUrl }}
            - name: CONCOURSE_EXTERNAL_GARDEN_URL
              value: {{ .Values.concourse.worker.externalGardenUrl | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.garden.useHoudini }}
            - name: CONCOURSE_GARDEN_USE_HOUDINI
              value: {{ .Values.concourse.worker.garden.useHoudini | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.garden.bin }}
            - name: CONCOURSE_GARDEN_BIN
              value: {{ .Values.concourse.worker.garden.bin | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.garden.config }}
            - name: CONCOURSE_GARDEN_CONFIG
              value: {{ .Values.concourse.worker.garden.config | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.garden.dnsProxyEnable }}
            - name: CONCOURSE_GARDEN_DNS_PROXY_ENABLE
              value: {{ .Values.concourse.worker.garden.dnsProxyEnable | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.logLevel }}
            - name: CONCOURSE_BAGGAGECLAIM_LOG_LEVEL
              value: {{ .Values.concourse.worker.baggageclaim.logLevel | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.bindIp }}
            - name: CONCOURSE_BAGGAGECLAIM_BIND_IP
              value: {{ .Values.concourse.worker.baggageclaim.bindIp | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.bindPort }}
            - name: CONCOURSE_BAGGAGECLAIM_BIND_PORT
              value: {{ .Values.concourse.worker.baggageclaim.bindPort | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.debugBindIp }}
            - name: CONCOURSE_BAGGAGECLAIM_DEBUG_BIND_IP
              value: {{ .Values.concourse.worker.baggageclaim.debugBindIp | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.debugBindPort }}
            - name: CONCOURSE_BAGGAGECLAIM_DEBUG_BIND_PORT
              value: {{ .Values.concourse.worker.baggageclaim.debugBindPort | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.volumes }}
            - name: CONCOURSE_BAGGAGECLAIM_VOLUMES
              value: {{ .Values.concourse.worker.baggageclaim.volumes | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.driver }}
            - name: CONCOURSE_BAGGAGECLAIM_DRIVER
              value: {{ .Values.concourse.worker.baggageclaim.driver | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.btrfsBin }}
            - name: CONCOURSE_BAGGAGECLAIM_BTRFS_BIN
              value: {{ .Values.concourse.worker.baggageclaim.btrfsBin | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.mkfsBin }}
            - name: CONCOURSE_BAGGAGECLAIM_MKFS_BIN
              value: {{ .Values.concourse.worker.baggageclaim.mkfsBin | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.overlaysDir }}
            - name: CONCOURSE_BAGGAGECLAIM_OVERLAYS_DIR
              value: {{ .Values.concourse.worker.baggageclaim.overlaysDir | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.baggageclaim.disableUserNamespaces }}
            - name: CONCOURSE_BAGGAGECLAIM_DISABLE_USER_NAMESPACES
              value: {{ .Values.concourse.worker.baggageclaim.disableUserNamespaces | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.volumeSweeperMaxInFlight }}
            - name: CONCOURSE_VOLUME_SWEEPER_MAX_IN_FLIGHT
              value: {{ .Values.concourse.worker.volumeSweeperMaxInFlight | quote }}
            {{- end }}
            {{- if .Values.concourse.worker.containerSweeperMaxInFlight }}
            - name: CONCOURSE_CONTAINER_SWEEPER_MAX_IN_FLIGHT
              value: {{ .Values.concourse.worker.containerSweeperMaxInFlight | quote }}
            {{- end }}
{{- if .Values.worker.env }}
{{ toYaml .Values.worker.env | indent 12 }}
{{- end }}
          ports:
            - name: worker-hc
              containerPort: {{ .Values.concourse.worker.healthcheckBindPort }}
{{- if .Values.worker.resources }}
          resources:
{{ toYaml .Values.worker.resources | indent 12 }}
{{- end }}
          securityContext:
            privileged: true
          volumeMounts:
            - name: concourse-keys
              mountPath: {{ .Values.worker.keySecretsPath | quote }}
              readOnly: true
            - name: concourse-work-dir
              mountPath: {{ .Values.concourse.worker.workDir | quote }}
            - name: pre-stop-hook
              mountPath: /pre-stop-hook.sh
              subPath: pre-stop-hook.sh

{{- if .Values.worker.additionalVolumeMounts }}
{{ toYaml .Values.worker.additionalVolumeMounts | indent 12 }}
{{- end }}
      affinity:
{{- if .Values.worker.additionalAffinities }}
{{ toYaml .Values.worker.additionalAffinities | indent 8 }}
{{- end }}
        podAntiAffinity:
          {{- if .Values.worker.hardAntiAffinity }}
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchLabels:
                app: {{ template "concourse.worker.fullname" . }}
                release: {{ .Release.Name | quote }}
            topologyKey: kubernetes.io/hostname
          {{- else }}
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              topologyKey: kubernetes.io/hostname
              labelSelector:
                matchLabels:
                  app: {{ template "concourse.worker.fullname" . }}
                  release: {{ .Release.Name | quote }}
          {{- end }}
      volumes:
{{- if .Values.worker.additionalVolumes }}
{{ toYaml .Values.worker.additionalVolumes | indent 8 }}
{{- end }}
        - name: pre-stop-hook
          configMap:
            name: {{ template "concourse.worker.fullname" . }}
        - name: concourse-keys
          secret:
            secretName: {{ template "concourse.worker.fullname" . }}
            defaultMode: 0400
            items:
              - key: host-key-pub
                path: host_key.pub
              - key: worker-key
                path: worker_key
  {{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: concourse-work-dir
      spec:
        accessModes:
          - {{ .Values.persistence.worker.accessMode | quote }}
        resources:
          requests:
            storage: {{ .Values.persistence.worker.size | quote }}
      {{- if .Values.persistence.worker.storageClass }}
      {{- if (eq "-" .Values.persistence.worker.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: "{{ .Values.persistence.worker.storageClass }}"
      {{- end }}
      {{- end }}
  {{- else }}
      {{ if include "concourse.are-there-additional-volumes.with-the-name.concourse-work-dir" . | not }}
        - name: concourse-work-dir
          emptyDir:
            {{- if .Values.worker.emptyDirSize }}
            sizeLimit: {{ .Values.worker.emptyDirSize | quote }}
            {{- end }}
      {{- end }}
  {{- end }}
  {{- if semverCompare "^1.7-0" .Capabilities.KubeVersion.GitVersion }}
  updateStrategy:
    type: {{ .Values.worker.updateStrategy }}
  {{- end }}
  {{- if .Values.worker.podManagementPolicy }}
  podManagementPolicy: {{ .Values.worker.podManagementPolicy }}
  {{- end }}
{{- end }}
