apiVersion: {{ include "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ include "common.names.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" . | nindent 4 }}
    {{- if .Values.commonLabels }}
    {{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
    {{- end }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  {{- if .Values.updateStrategy }}
  strategy: {{- toYaml .Values.updateStrategy | nindent 4 }}
  {{- end }}
  selector:
    matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
        {{- if (include "sonarqube.createSecret" .) }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.metrics.jmx.enabled }}
        checksum/jmx-configuration: {{ include (print $.Template.BasePath "/jmx-configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.podAnnotations }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      labels: {{- include "common.labels.standard" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- include "common.tplvalues.render" (dict "value" .Values.podLabels "context" $) | nindent 8 }}
        {{- end }}
    spec:
      serviceAccountName: {{ include "sonarqube.serviceAccountName" . }}
      {{- include "sonarqube.imagePullSecrets" . | nindent 6 }}
      {{- if .Values.hostAliases }}
      hostAliases: {{- include "common.tplvalues.render" (dict "value" .Values.hostAliases "context" $) | nindent 8 }}
      {{- end }}
      {{- 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 "context" $) | nindent 10 }}
        podAntiAffinity: {{- include "common.affinities.pods" (dict "type" .Values.podAntiAffinityPreset "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.nodeSelector }}
      nodeSelector: {{- include "common.tplvalues.render" ( dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "common.tplvalues.render" (dict "value" .Values.tolerations "context" .) | nindent 8 }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName | quote }}
      {{- end }}
      {{- if .Values.schedulerName }}
      schedulerName: {{ .Values.schedulerName | quote }}
      {{- end }}
      {{- if .Values.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      {{- if or .Values.initContainers .Values.sysctl.enabled (and .Values.volumePermissions.enabled .Values.persistence.enabled) }}
      initContainers:
        {{- if .Values.sysctl.enabled }}
        ## Image that performs the sysctl operation to modify Kernel settings (needed sometimes to avoid boot errors)
        - name: sysctl
          image: {{ include "sonarqube.sysctl.image" . }}
          imagePullPolicy: {{ .Values.sysctl.image.pullPolicy | quote }}
          command:
            - /bin/bash
            - -ec
            - |
              {{- include "sonarqube.sysctl.ifLess" (dict "key" "vm.max_map_count" "value" "262144") | nindent 14 }}
              {{- include "sonarqube.sysctl.ifLess" (dict "key" "fs.file-max" "value" "65536") | nindent 14 }}
          securityContext:
            privileged: true
            runAsUser: 0
          {{- if .Values.sysctl.resources }}
          resources: {{- toYaml .Values.sysctl.resources | nindent 12 }}
          {{- end }}
        {{- end }}
        {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
        - name: volume-permissions
          image: {{ include "sonarqube.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p /bitnami/sonarqube
              chmod 700 /bitnami/sonarqube
              find /bitnami/sonarqube -mindepth 1 -maxdepth 1 -not -name ".snapshot" -not -name "lost+found" | xargs -r chown -R {{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}
          {{- if .Values.volumePermissions.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.volumePermissions.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: data
              mountPath: /bitnami/sonarqube
        {{- end }}
        {{- if .Values.initContainers }}
          {{- include "common.tplvalues.render" (dict "value" .Values.initContainers "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
      containers:
        - name: sonarqube
          image: {{ include "sonarqube.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if .Values.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.containerSecurityContext "enabled" | toYaml | 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 }}
          {{- 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 }}
          {{- end }}
          env:
            - name: BITNAMI_DEBUG
              value: {{ ternary "true" "false" (or .Values.image.debug .Values.diagnosticMode.enabled) | quote }}
            - name: SONARQUBE_SKIP_BOOTSTRAP
              value: {{ ternary "yes" "no" .Values.sonarqubeSkipInstall | quote }}
            - name: SONARQUBE_USERNAME
              value: {{ .Values.sonarqubeUsername | quote }}
            - name: SONARQUBE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "sonarqube.secretName" . }}
                  key: sonarqube-password
            - name: SONARQUBE_EMAIL
              value: {{ .Values.sonarqubeEmail | quote }}
            {{- if .Values.minHeapSize }}
            - name: SONARQUBE_MIN_HEAP_SIZE
              value: {{ .Values.minHeapSize | quote }}
            {{- end }}
            {{- if .Values.maxHeapSize }}
            - name: SONARQUBE_MAX_HEAP_SIZE
              value: {{ .Values.maxHeapSize | quote }}
            {{- end }}
            {{- if .Values.sonarSecurityRealm }}
            - name: SONAR_SECURITY_REALM
              value: {{ .Values.sonarSecurityRealm | quote }}
            {{- end }}
            {{- if .Values.sonarAuthenticatorDowncase }}
            - name: SONAR_AUTHENTICATOR_DOWNCASE
              value: {{ .Values.sonarAuthenticatorDowncase | quote }}
            {{- end }}
            {{- if .Values.ldap.url }}
            - name: LDAP_URL
              value: {{ .Values.ldap.url | quote }}
            {{- end }}
            {{- if .Values.ldap.bindDn }}
            - name: LDAP_BINDDN
              value: {{ .Values.ldap.bindDn | quote }}
            {{- end }}
            {{- if .Values.ldap.bindPassword }}
            - name: LDAP_BINDPASSWORD
              value: {{ .Values.ldap.bindPassword | quote }}
            {{- end }}
            {{- if .Values.ldap.authentication }}
            - name: LDAP_AUTHENTICATION
              value: {{ .Values.ldap.authentication | quote }}
            {{- end }}
            {{- if .Values.ldap.realm }}
            - name: LDAP_REALM
              value: {{ .Values.ldap.realm | quote }}
            {{- end }}
            {{- if .Values.ldap.contextFactoryClass }}
            - name: LDAP_CONTEXTFACTORYCLASS
              value: {{ .Values.ldap.contextFactoryClass | quote }}
            {{- end }}
            {{- if .Values.ldap.StartTLS }}
            - name: LDAP_STARTTLS
              value: {{ .Values.ldap.StartTLS | quote }}
            {{- end }}
            {{- if .Values.ldap.followReferrals }}
            - name: LDAP_FOLLOWREFERRALS
              value: {{ .Values.ldap.followReferrals | quote }}
            {{- end }}
            {{- if .Values.ldap.user.baseDn }}
            - name: LDAP_USER_BASEDN
              value: {{ .Values.ldap.user.baseDn | quote }}
            {{- end }}
            {{- if .Values.ldap.user.request }}
            - name: LDAP_USER_REQUEST
              value: {{ .Values.ldap.user.request | quote }}
            {{- end }}
            {{- if .Values.ldap.user.realNameAttribute }}
            - name: LDAP_USER_REALNAMEATTRIBUTE
              value: {{ .Values.ldap.user.realNameAttribute | quote }}
            {{- end }}
            {{- if .Values.ldap.user.emailAttribute }}
            - name: LDAP_USER_EMAILATTRIBUTE
              value: {{ .Values.ldap.user.emailAttribute | quote }}
            {{- end }}
            {{- if .Values.ldap.group.baseDn }}
            - name: LDAP_GROUP_BASEDN
              value: {{ .Values.ldap.group.baseDn | quote }}
            {{- end }}
            {{- if .Values.ldap.group.request }}
            - name: LDAP_GROUP_REQUEST
              value: {{ .Values.ldap.group.request | quote }}
            {{- end }}
            {{- if .Values.ldap.group.idAttribute }}
            - name: LDAP_GROUP_IDATTRIBUTE
              value: {{ .Values.ldap.group.idAttribute | quote }}
            {{- end }}
            - name: SONARQUBE_START_TIMEOUT
              value: {{ .Values.startTimeout | quote }}
            - name: SONARQUBE_DATABASE_HOST
              value: {{ include "sonarqube.database.host" . | quote }}
            - name: SONARQUBE_DATABASE_PORT_NUMBER
              value: {{ include "sonarqube.database.port" . | quote }}
            - name: SONARQUBE_DATABASE_NAME
              value: {{ include "sonarqube.database.name" . | quote }}
            - name: SONARQUBE_DATABASE_USER
              value: {{ include "sonarqube.database.username" . | quote }}
            - name: SONARQUBE_DATABASE_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "sonarqube.database.secretName" . }}
                  key: password
            {{- if .Values.smtpHost }}
            - name: SONARQUBE_SMTP_HOST
              value: {{ .Values.smtpHost | quote }}
            {{- end }}
            {{- if .Values.smtpPort }}
            - name: SONARQUBE_SMTP_PORT
              value: {{ .Values.smtpPort | quote }}
            {{- end }}
            {{- if .Values.smtpUser }}
            - name: SONARQUBE_SMTP_USER
              value: {{ .Values.smtpUser | quote }}
            {{- end }}
            {{- if or .Values.smtpPassword .Values.smtpExistingSecret }}
            - name: SONARQUBE_SMTP_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ include "sonarqube.smtpSecretName" . }}
                  key: smtp-password
            {{- end }}
            {{- if .Values.smtpProtocol }}
            - name: SONARQUBE_SMTP_PROTOCOL
              value: {{ .Values.smtpProtocol | quote }}
            {{- end }}
            - name: SONARQUBE_PORT_NUMBER
              value: {{ .Values.containerPorts.http | quote }}
            - name: SONARQUBE_ELASTICSEARCH_PORT_NUMBER
              value: {{ .Values.containerPorts.elastic | quote }}
            {{- if .Values.extraProperties }}
            - name: SONARQUBE_EXTRA_PROPERTIES
              value: {{ join "," .Values.extraProperties | quote }}
            {{- end }}
            {{- if .Values.metrics.jmx.enabled }}
            - name: SONARQUBE_WEB_JAVA_ADD_OPTS
              value: "-Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=10443 -Dcom.sun.management.jmxremote.rmi.port=10444"
            {{- 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 }}
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.containerPorts.http }}
            - name: elastic
              containerPort: {{ .Values.containerPorts.elastic }}
          {{- 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 }}
            httpGet:
              path: /
              port: http
          {{- 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 }}
            httpGet:
              path: /
              port: http
          {{- 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 }}
            tcpSocket:
              port: http
          {{- end }}
          {{- end }}
          {{- if .Values.lifecycleHooks }}
          lifecycle: {{- include "common.tplvalues.render" (dict "value" .Values.lifecycleHooks "context" $) | nindent 12 }}
          {{- end }}
          volumeMounts:
            - mountPath: /bitnami/sonarqube
              name: data
          {{- if .Values.extraVolumeMounts }}
          {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumeMounts "context" $) | nindent 12 }}
          {{- end }}
        {{- if .Values.metrics.jmx.enabled }}
        - name: jmx-exporter
          image: {{ include "sonarqube.metrics.jmx.image" . }}
          imagePullPolicy: {{ .Values.metrics.jmx.image.pullPolicy | quote }}
          {{- if .Values.metrics.jmx.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.metrics.jmx.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
          {{- if .Values.diagnosticMode.enabled }}
          command: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.command "context" $) | nindent 12 }}
          args: {{- include "common.tplvalues.render" (dict "value" .Values.diagnosticMode.args "context" $) | nindent 12 }}
          {{- else }}
          command:
            - java
            - -XX:+UnlockExperimentalVMOptions
            - -XX:+UseCGroupMemoryLimitForHeap
            - -XX:MaxRAMFraction=1
            - -XshowSettings:vm
            - -jar
            - jmx_prometheus_httpserver.jar
            - {{ .Values.metrics.jmx.containerPorts.metrics | quote }}
            - /etc/jmx/sonarqube-prometheus.yml
          {{- end }}
          ports:
            - name: metrics
              containerPort: {{ .Values.metrics.jmx.containerPorts.metrics }}
          {{- if .Values.metrics.jmx.resources }}
          resources: {{- toYaml .Values.metrics.jmx.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: jmx-config
              mountPath: /etc/jmx
        {{- end }}
        {{- if .Values.sidecars }}
        {{- include "common.tplvalues.render" ( dict "value" .Values.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        - name: data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ (tpl .Values.persistence.existingClaim .) | default (include "common.names.fullname" .) }}
          {{- else }}
          emptyDir: {}
          {{- end }}
        {{- if .Values.metrics.jmx.enabled }}
        - name: jmx-config
          configMap:
            name: {{ printf "%s-jmx-conf" (include "common.names.fullname" .) }}
        {{- end }}
        {{- if .Values.extraVolumes }}
        {{- include "common.tplvalues.render" (dict "value" .Values.extraVolumes "context" $) | nindent 8 }}
        {{- end }}
