apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "sonarqube.fullname" . }}
  labels:
    app: {{ template "sonarqube.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app: {{ template "sonarqube.name" . }}
      release: {{ .Release.Name }}
{{- if .Values.deploymentStrategy }}
  strategy:
{{ toYaml .Values.deploymentStrategy | indent 4 }}
{{- end }}
  template:
    metadata:
      labels:
        app: {{ template "sonarqube.name" . }}
        release: {{ .Release.Name }}
{{- with .Values.podLabels }}
{{ toYaml . | indent 8 }}
{{- end }}
{{ if .Values.annotations}}
      annotations:
      {{- range $key, $value := .Values.annotations }}
        {{ $key }}: {{ $value | quote }}
      {{- end }}
{{- end }}
    spec:
      securityContext:
{{ toYaml .Values.securityContext | indent 8 }}
      {{- if .Values.image.pullSecret }}
      imagePullSecrets:
        - name: {{ .Values.image.pullSecret }}
      {{- end }}
      initContainers:
        - name: chmod-volume-mounts
          image: busybox:1.31
          command:
            - "sh"
            - "-c"
            - 'mkdir -p $(printf "/opt/sonarqube/${1-%s\n}" temp logs data extensions/downloads extensions/plugins/tmp extensions/plugins certs) &&
               chown 999:999 -R $(printf "/opt/sonarqube/${1-%s\n}" temp logs data extensions/downloads extensions/plugins/tmp extensions/plugins certs)'
          volumeMounts:
          - mountPath: /opt/sonarqube/temp
            name: sonarqube
            subPath: temp
          - mountPath: /opt/sonarqube/logs
            name: sonarqube
            subPath: logs
          - mountPath: /opt/sonarqube/data
            name: sonarqube
            subPath: data
          - mountPath: /opt/sonarqube/extensions/plugins/tmp
            name: sonarqube
            subPath: tmp
          - mountPath: /opt/sonarqube/extensions/downloads
            name: sonarqube
            subPath: downloads
          - mountPath: /opt/sonarqube/extensions/plugins
            name: sonarqube
            subPath: plugins
          {{- with .Values.env }}
          env:
            {{- . | toYaml | trim | nindent 12 }}
          {{- end }}
      {{- if .Values.caCerts }}
        - name: ca-certs
          image: adoptopenjdk/openjdk11:alpine
          command: ["sh"]
          args: ["-c", "cp -f \"${JAVA_HOME}/lib/security/cacerts\" /tmp/certs/cacerts; for f in /tmp/secrets/ca-certs/*; do keytool -importcert -file \"${f}\" -alias \"$(basename \"${f}\")\" -keystore /tmp/certs/cacerts -storepass changeit -trustcacerts -noprompt; done;"]
          volumeMounts:
            - mountPath: /tmp/certs
              name: sonarqube
              subPath: certs
            - mountPath: /tmp/secrets/ca-certs
              name: ca-certs
          {{- with .Values.env }}
          env:
            {{- . | toYaml | trim | nindent 12 }}
          {{- end }}
      {{- end }}
      {{- if .Values.elasticsearch.configureNode }}
        - name: init-sysctl
          image: {{ default "busybox:1.31" .Values.plugins.initSysctlContainerImage }}
          securityContext:
            privileged: true
          command:
          - sysctl
          - -w
          - vm.max_map_count=262144
          {{- with .Values.env }}
          env:
            {{- . | toYaml | trim | nindent 12 }}
          {{- end }}
      {{- end }}
      {{- if .Values.plugins.install }}
        - name: install-plugins
          image: {{ default "alpine:3.10.3" .Values.plugins.initContainerImage }}
          command: ["sh",
            "-c",
            "mkdir -p {{ .Values.sonarqubeFolder }}/extensions/plugins/tmp &&
            rm -f {{ .Values.sonarqubeFolder }}/extensions/plugins/tmp/* &&
            cp /tmp/scripts/install_plugins.sh {{ .Values.sonarqubeFolder }}/extensions/plugins/tmp/install_plugins.sh &&
            chmod 0775 {{ .Values.sonarqubeFolder }}/extensions/plugins/tmp/install_plugins.sh &&
            {{ .Values.sonarqubeFolder }}/extensions/plugins/tmp/install_plugins.sh && pwd && ls -lah"]
          volumeMounts:
            - mountPath: {{ .Values.sonarqubeFolder }}/extensions/plugins/tmp
              name: sonarqube
              subPath: tmp
            - name: install-plugins
              mountPath: /tmp/scripts/
          {{- with .Values.env }}
          env:
            {{- . | toYaml | trim | nindent 12 }}
          {{- end }}
          resources:
{{ toYaml .Values.plugins.resources | indent 12 }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.hostAliases }}
      hostAliases:
{{ toYaml .Values.hostAliases | indent 8 }}
    {{- end }}
    {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
      {{- end }}
      {{- if and .Values.sonarProperties .Values.sonarSecretProperties }}
        - name: concat-properties
          image: {{ default "alpine:3.10.3" .Values.plugins.initContainerImage }}
          command: ["sh",
            "-c",
            "awk 1 /tmp/props/sonar.properties /tmp/props/secret.properties > /tmp/result/sonar.properties"]
          volumeMounts:
            - mountPath: /tmp/props/sonar.properties
              name: config
              subPath: sonar.properties
            - mountPath: /tmp/props/secret.properties
              name: secret-config
              subPath: secret.properties
            - mountPath: /tmp/result
              name: concat-dir
          {{- with .Values.env }}
          env:
            {{- . | toYaml | trim | nindent 12 }}
          {{- end }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.service.internalPort }}
              protocol: TCP
          {{- if .Values.plugins.install }}
          command:
            - /tmp/scripts/copy_plugins.sh
          {{- end }}
          env:
            {{- with .Values.env }}
            {{- . | toYaml | trim | nindent 12 }}
            {{- end }}
            - name: SONARQUBE_WEB_JVM_OPTS
              {{- if .Values.caCerts }}
              value: {{ printf "-Djavax.net.ssl.trustStore=%s/certs/cacerts %s" .Values.sonarqubeFolder .Values.jvmOpts | trim | quote }}
              {{- else }}
              value: "{{ .Values.jvmOpts }}"
              {{- end }}
            - name: SONARQUBE_JDBC_USERNAME
              {{- if eq .Values.database.type "postgresql" }}
              value: {{ .Values.postgresql.postgresqlUsername | quote }}
              {{- else if eq .Values.database.type "mysql" }}
              value: {{ .Values.mysql.mysqlUser | quote }}
              {{- end }}
            - name: SONARQUBE_JDBC_PASSWORD
              valueFrom:
                secretKeyRef:
                  {{- if eq .Values.database.type "postgresql" }}
                  name: {{- if .Values.postgresql.enabled }} {{ template "postgresql.fullname" .}} {{- else if .Values.postgresql.postgresqlPasswordSecret }} {{ .Values.postgresql.postgresqlPasswordSecret }} {{- else }} {{ template "sonarqube.fullname" . }} {{- end }}
                  key: postgresql-password
                  {{- else if eq .Values.database.type "mysql" }}
                  name: {{- if .Values.mysql.enabled }} {{ template "mysql.fullname" .}} {{- else if .Values.mysql.mysqlPasswordSecret }} {{ .Values.mysql.mysqlPasswordSecret }} {{- else }} {{ template "sonarqube.fullname" . }} {{- end }}
                  key: mysql-password
                  {{- end }}
            - name: SONARQUBE_JDBC_URL
              {{- if eq .Values.database.type "postgresql" }}
              value: "jdbc:postgresql://{{ template "postgresql.hostname" . }}:{{- .Values.postgresql.service.port -}}/{{- .Values.postgresql.postgresqlDatabase -}}"
              {{- else if eq .Values.database.type "mysql" }}
              value: "jdbc:mysql://{{ template "mysql.hostname" . }}:{{ .Values.mysql.service.port }}/{{ .Values.mysql.mysqlDatabase }}?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true{{- range $key, $value := .Values.mysql.mysqlParams }}&{{ $key }}={{ $value }}{{- end }}"
              {{- end }}
          livenessProbe:
            httpGet:
              path: {{ .Values.livenessProbe.sonarWebContext }}sessions/new
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
          readinessProbe:
            httpGet:
              path: {{ .Values.readinessProbe.sonarWebContext }}sessions/new
              port: http
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          volumeMounts:
{{- if .Values.persistence.mounts }}
{{ toYaml .Values.persistence.mounts | indent 12 }}
{{- end }}
            {{- if and .Values.sonarProperties .Values.sonarSecretProperties }}
            - mountPath: {{ .Values.sonarqubeFolder }}/conf/
              name: concat-dir
            {{- else if or .Values.sonarProperties (not .Values.elasticsearch.bootstrapChecks) }}
            - mountPath: {{ .Values.sonarqubeFolder }}/conf/
              name: config
            {{- end }}
            {{- if .Values.sonarSecretKey }}
            - mountPath: {{ .Values.sonarqubeFolder }}/secret/
              name: secret
            {{- end }}
            {{- if .Values.caCerts }}
            - mountPath: {{ .Values.sonarqubeFolder }}/certs
              name: sonarqube
              subPath: certs
            {{- end }}
            - mountPath: {{ .Values.sonarqubeFolder }}/data
              name: sonarqube
              subPath: data
            - mountPath: {{ .Values.sonarqubeFolder }}/extensions/plugins/tmp
              name: sonarqube
              subPath: tmp
            - mountPath: {{ .Values.sonarqubeFolder }}/extensions/downloads
              name: sonarqube
              subPath: downloads
            - mountPath: {{ .Values.sonarqubeFolder }}/extensions/plugins
              name: sonarqube
              subPath: plugins
            - mountPath: {{ .Values.sonarqubeFolder }}/temp
              name: sonarqube
              subPath: temp
            - mountPath: {{ .Values.sonarqubeFolder }}/logs
              name: sonarqube
              subPath: logs
            - mountPath: /tmp
              name: tmp-dir
            - name: copy-plugins
              mountPath: /tmp/scripts
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
    {{- end }}
    {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
    {{- end }}
    {{- if .Values.affinity }}
      affinity:
{{ toYaml .Values.affinity | indent 8 }}
    {{- end }}
      volumes:
{{- if .Values.persistence.volumes }}
{{ tpl (toYaml .Values.persistence.volumes | indent 6) . }}
{{- end }}
      {{- if or .Values.sonarProperties (not .Values.elasticsearch.bootstrapChecks) }}
      - name: config
        configMap:
          name: {{ template "sonarqube.fullname" . }}-config
          items:
          - key: sonar.properties
            path: sonar.properties
      {{- end }}
      {{- if .Values.sonarSecretProperties }}
      - name: secret-config
        secret:
          secretName: {{ .Values.sonarSecretProperties }}
          items:
          - key: secret.properties
            path: secret.properties
      {{- end }}
      {{- if .Values.sonarSecretKey }}
      - name: secret
        secret:
          secretName: {{ .Values.sonarSecretKey }}
          items:
          - key: sonar-secret.txt
            path: sonar-secret.txt
      {{- end }}
      {{- if .Values.caCerts }}
      - name: ca-certs
        secret:
          secretName: {{ .Values.caCerts.secret }}
      {{- end }}
      - name: install-plugins
        configMap:
          name: {{ template "sonarqube.fullname" . }}-install-plugins
          items:
            - key: install_plugins.sh
              path: install_plugins.sh
      - name: copy-plugins
        configMap:
          name: {{ template "sonarqube.fullname" . }}-copy-plugins
          defaultMode: 0755
          items:
            - key: copy_plugins.sh
              path: copy_plugins.sh
      - name: sonarqube
        {{- if .Values.persistence.enabled }}
        persistentVolumeClaim:
          claimName: {{ if .Values.persistence.existingClaim }}{{ .Values.persistence.existingClaim }}{{- else }}{{ template "sonarqube.fullname" . }}{{- end }}
        {{- else }}
        emptyDir: {}
        {{- end  }}
      - name : tmp-dir
        emptyDir: {}
        {{- if .Values.sonarSecretProperties }}
      - name : concat-dir
        emptyDir: {}
        {{- end }}
