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

{{- if not .Values.existingConfigMap }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "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 }}
data:
  # Taken from the upstream chart with miminal value adaptations
  # https://gitlab.com/gitlab-org/charts/gitlab-runner/-/blob/main/templates/configmap.yaml?ref_type=heads
  config.toml: |
    shutdown_timeout = {{ .Values.shutdownTimeout }}
    concurrent = {{ .Values.concurrent }}
    check_interval = {{ .Values.checkInterval }}
    log_level = {{ default "info" .Values.logLevel | quote }}
    {{- if .Values.logFormat }}
    log_format = {{ .Values.logFormat | quote }}
    {{- end }}
    {{- if .Values.metrics.enabled }}
    listen_address = ":{{ .Values.containerPorts.metrics }}"
    {{- end }}
    {{- if .Values.sentryDsn }}
    sentry_dsn = "{{ .Values.sentryDsn }}"
    {{- end }}
    {{- if .Values.connectionMaxAge }}
    connection_max_age = "{{ .Values.connectionMaxAge }}"
    {{- end }}
    {{- if .Values.sessionServer.enabled }}
    [session_server]
      session_timeout = {{ .Values.sessionServer.sessionTimeout }}
      listen_address = "0.0.0.0:{{ .Values.containerPorts.sessionServer }}"
      advertise_address = "SESSION_SERVER_ADDRESS:SESSION_SERVER_PORT"
    {{- end }}
    {{- if .Values.extraConfig }}
    {{- include "common.tplvalues.render" (dict "value" .Values.extraConfig "context" $) | nindent 4 }}
    {{- end }}
  {{ if .Values.runners.config }}
  config.template.toml: |
    {{- include "common.tplvalues.render" (dict "value" .Values.runners.config "context" $) | nindent 4 }}
  {{ end }}
{{- end }}
