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

{{- if (include "gitlab-runner.canDeploy" .) }}
apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}
  namespace: {{ include "common.names.namespace" . | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
    app.kubernetes.io/part-of: gitlab-runner
    app.kubernetes.io/component: gitlab-runner
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  {{- if and (not .Values.autoscaling.hpa.enabled) (not .Values.sessionServer.enabled) }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  {{- $podLabels := include "common.tplvalues.merge" ( dict "values" ( list .Values.podLabels .Values.commonLabels ) "context" . ) }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" ( dict "customLabels" $podLabels "context" $ ) | nindent 6 }}
  {{- if .Values.updateStrategy }}
  strategy: {{- include "common.tplvalues.render" (dict "value" .Values.updateStrategy "context" $) | nindent 4 }}
  {{- end }}
  template:
    metadata:
      labels: {{- include "common.labels.standard" ( dict "customLabels" $podLabels "context" $ ) | nindent 8 }}
        app.kubernetes.io/part-of: gitlab-runner
        app.kubernetes.io/component: gitlab-runner
      {{- if .Values.podAnnotations }}
      annotations: {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
      {{- end }}
    spec:
      {{- include "gitlab-runner.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      serviceAccountName: {{ template "gitlab-runner.serviceAccountName" . }}
      {{- if .Values.affinity }}
      affinity: {{- include "common.tplvalues.render" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- else }}
      affinity:
        podAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAffinityPreset "customLabels" $podLabels "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "customLabels" $podLabels "context" $) | nindent 10 }}
        nodeAffinity: {{- include "common.affinities.nodes" (dict "type" .Values.nodeAffinityPreset.type "key" .Values.nodeAffinityPreset.key "values" .Values.nodeAffinityPreset.values) | nindent 10 }}
      {{- end }}
      {{- if .Values.topologySpreadConstraints }}
      topologySpreadConstraints: {{- include "common.tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      automountServiceAccountToken: {{ .Values.automountServiceAccountToken }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.podSecurityContext "context" $) | nindent 8 }}
      {{- end }}
      dnsPolicy: {{ .Values.dnsPolicy }}
      {{- if .Values.terminationGracePeriodSeconds }}
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- end }}
      {{- if .Values.initContainers }}
      initContainers: {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
      {{- end }}
      containers:
        - name: gitlab-runner
          image: {{ template "gitlab-runner.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- include "common.compatibility.renderSecurityContext" (dict "secContext" .Values.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.command }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.command "context" $) | nindent 12 }}
          {{- else }}
          command:
            - dumb-init
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else if .Values.args }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.args "context" $) | nindent 12 }}
          {{- else }}
          args:
            - --
            - /bin/bash
            - /configmaps/entrypoint
            {{- if .Values.extraArgs }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraArgs "context" $) | nindent 12 }}
            {{- end }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- else if .Values.unregisterRunners }}
          lifecycle:
            preStop:
              exec:
                command:
                  - /entrypoint
                  - unregister
                  - --all-runners
          {{- end }}
          env:
            - name: HOME
              value: /home/gitlab-runner
            - name: CI_SERVER_URL
              value: {{ .Values.gitlabUrl | quote }}
            - name: RUNNER_EXECUTOR
              value: {{ .Values.runners.executor | quote }}
            - name: KUBERNETES_NAMESPACE
              value: {{ include "common.names.namespace" . | quote }}
            {{- if .Values.sessionServer.enabled }}
            - name: SESSION_SERVER_ADDRESS
              {{- if .Values.sessionServer.service.loadBalancerIP }}
              value: {{ .Values.sessionServer.service.loadBalancerIP }}
              {{- else if eq .Values.sessionServer.service.type "ClusterIP" }}
              value: {{ printf "%s.%s" (include "gitlab-runner.session-server.fullname" .) (include "common.names.namespace" .) }}
              {{- else if eq .Values.sessionServer.service.type "NodePort" }}
              valueFrom:
                fieldRef:
                  apiVersion: v1
                  fieldPath: status.hostIP
              {{- end }}
            {{- end }}
            {{- if .Values.extraEnvVars }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraEnvVars "context" $) | nindent 12 }}
            {{- end }}
          envFrom:
            {{- if .Values.extraEnvVarsCM }}
            - configMapRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsCM "context" $) }}
            {{- end }}
            {{- if .Values.extraEnvVarsSecret }}
            - secretRef:
                name: {{ include "common.tplvalues.render" (dict "value" .Values.extraEnvVarsSecret "context" $) }}
            {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- else if ne .Values.resourcesPreset "none" }}
          resources: {{- include "common.resources.preset" (dict "type" .Values.resourcesPreset) | nindent 12 }}
          {{- end }}
          ports:
            {{- if .Values.metrics.enabled }}
            - name: http-metrics
              containerPort: {{ .Values.containerPorts.metrics }}
            {{- end }}
            {{- if .Values.sessionServer.enabled }}
            - name: http-session
              containerPort: {{ .Values.containerPorts.sessionServer }}
              protocol: TCP
            {{- end }}
          {{- if not .Values.diagnosticMode.enabled }}
          {{- if .Values.customLivenessProbe }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customLivenessProbe "context" $) | nindent 12 }}
          {{- else if .Values.livenessProbe.enabled }}
          livenessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.livenessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - /bin/bash
                - /configmaps/check-live
                - {{ .Values.livenessProbe.timeoutSeconds | add -1 | max 1 | quote }}
          {{- end }}
          {{- if .Values.customReadinessProbe }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customReadinessProbe "context" $) | nindent 12 }}
          {{- else if .Values.readinessProbe.enabled }}
          readinessProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.readinessProbe "enabled") "context" $) | nindent 12 }}
            exec:
              command:
                - pgrep
                - "gitlab.*runner"
          {{- end }}
          {{- if .Values.customStartupProbe }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" .Values.customStartupProbe "context" $) | nindent 12 }}
          {{- else if .Values.startupProbe.enabled }}
          startupProbe: {{- include "common.tplvalues.render" (dict "value" (omit .Values.startupProbe "enabled") "context" $) | nindent 12 }}
            command:
              - pgrep
              - "gitlab.*runner"
          {{- end }}
          {{- end }}
          volumeMounts:
            - name: projected-secrets
              mountPath: /secrets
            - name: empty-dir
              mountPath: /home/gitlab-runner/.gitlab-runner
              subPath: app-gitlab-runner-dir
            - name: empty-dir
              mountPath: /etc/gitlab-runner
              subPath: etc-gitlab-runner-dir
            - name: empty-dir
              mountPath: /tmp
              subPath: tmp-dir
            - name: configmaps
              mountPath: /configmaps
            {{- if .Values.existingCertsSecret }}
            - name: custom-certs
              readOnly: true
              mountPath: /home/gitlab-runner/.gitlab-runner/certs/
            {{- end }}
            {{- if .Values.extraVolumeMounts }}
            {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
            {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: empty-dir
          emptyDir: {}
        # The gitlab script expect all secrets mounted in the same folder, so we need to use projected secrets
        - name: projected-secrets
          projected:
            sources:
              {{- if .Values.existingCacheSecret }}
              - secret:
                  name: {{ include "common.tplvalues.render" (dict "value" .Values.existingCacheSecret "context" $) }}
              {{- end }}
              - secret:
                  name: {{ include "gitlab-runner.secretName" . }}
                  items:
                    - key: runner-token
                      path: runner-token
        {{- if .Values.existingCertsSecret }}
        - name: custom-certs
          secret:
            secretName: {{ include "common.tplvalues.render" (dict "value" .Values.existingCertsSecret "context" $) }}
        {{- end }}
        # The gitlab script expect all scripts and configuration files to be mounted in the same folder
        - name: configmaps
          projected:
            sources:
              - configMap:
                  name: {{ include "gitlab-runner.configmapName" . }}
              - configMap:
                  name: {{ printf "%s-scripts" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
{{- end }}