apiVersion: {{ template "jenkins.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "jenkins.fullname" . }}
  labels: {{- include "jenkins.labels" . | nindent 4 }}
spec:
  selector:
    matchLabels: {{- include "jenkins.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: {{- include "jenkins.labels" . | nindent 8 }}
      {{- if or .Values.podAnnotations .Values.metrics.enabled }}
      annotations:
        {{- if .Values.podAnnotations }}
        {{- include "jenkins.tplValue" (dict "value" .Values.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
        {{- if .Values.metrics.podAnnotations }}
        {{- include "jenkins.tplValue" (dict "value" .Values.metrics.podAnnotations "context" $) | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
{{- include "jenkins.imagePullSecrets" . | indent 6 }}
      {{- if .Values.affinity }}
      affinity: {{- include "jenkins.tplValue" (dict "value" .Values.affinity "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector: {{- include "jenkins.tplValue" (dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.tolerations }}
      tolerations: {{- include "jenkins.tplValue" (dict "value" .Values.tolerations "context" $) | nindent 8 }}
      {{- end }}
      {{- if .Values.podSecurityContext }}
      securityContext: {{- toYaml .Values.podSecurityContext | nindent 8 }}
      {{- end }}
      {{- if and .Values.volumePermissions.enabled .Values.persistence.enabled }}
      initContainers:
        - name: volume-permissions
          image: {{ include "jenkins.volumePermissions.image" . }}
          imagePullPolicy: {{ .Values.volumePermissions.image.pullPolicy | quote }}
          command:
            - /bin/bash
          args:
            - -ec
            - |
              mkdir -p /bitnami/jenkins
              chown -R "{{ .Values.containerSecurityContext.runAsUser }}:{{ .Values.podSecurityContext.fsGroup }}" "/bitnami/jenkins"
          securityContext:
            runAsUser: 0
          {{- if .Values.volumePermissions.resources }}
          resources: {{- toYaml .Values.volumePermissions.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: jenkins-data
              mountPath: /bitnami/jenkins
      {{- end }}
      containers:
        - name: jenkins
          image: {{ template "jenkins.image" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy | quote }}
          {{- if .Values.containerSecurityContext }}
          securityContext: {{- toYaml .Values.containerSecurityContext | nindent 12 }}
          {{- end }}
          env:
            - name: JENKINS_USERNAME
              value: {{ .Values.jenkinsUser | quote }}
            - name: JENKINS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "jenkins.fullname" . }}
                  key: jenkins-password
            - name: JENKINS_HOME
              value: {{ .Values.jenkinsHome | quote }}
            - name: DISABLE_JENKINS_INITIALIZATION
              value: {{ .Values.disableInitialization | quote }}
            {{- if .Values.javaOpts }}
            - name: JAVA_OPTS
              value: {{ .Values.javaOpts | quote }}
            {{- end }}
          ports:
            - name: http
              containerPort: 8080
            - name: https
              containerPort: 8443
          {{- if .Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: /login
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.livenessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.livenessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.livenessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: /login
              port: http
            initialDelaySeconds: {{ .Values.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.readinessProbe.periodSeconds }}
            timeoutSeconds: {{ .Values.readinessProbe.timeoutSeconds }}
            successThreshold: {{ .Values.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.readinessProbe.failureThreshold }}
          {{- end }}
          {{- if .Values.resources }}
          resources: {{- toYaml .Values.resources | nindent 12 }}
          {{- end }}
          volumeMounts:
            - name: jenkins-data
              mountPath: /bitnami/jenkins
          {{- if .Values.metrics.enabled }}
        - name: metrics
          image: {{ template "jenkins.metrics.image" . }}
          imagePullPolicy: {{ .Values.metrics.image.pullPolicy | quote }}
          command:
            - sh
            - -c
            - |
              python -u ./jenkins_exporter.py -j http://127.0.0.1:8080 --user $JENKINS_USERNAME --password $JENKINS_PASSWORD
          env:
            - name: JENKINS_USERNAME
              value: {{ .Values.jenkinsUser | quote }}
            - name: JENKINS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ template "jenkins.fullname" . }}
                  key: jenkins-password
          ports:
            - name: metrics
              containerPort: 9118
          livenessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: 15
            timeoutSeconds: 5
          readinessProbe:
            httpGet:
              path: /metrics
              port: metrics
            initialDelaySeconds: 5
            timeoutSeconds: 1
          {{- if .Values.metrics.resources }}
          resources: {{- toYaml .Values.metrics.resources | nindent 12 }}
          {{- end }}
        {{- end }}
      volumes:
        - name: jenkins-data
          {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ template "jenkins.fullname" . }}
          {{- else }}
          emptyDir: {}
          {{- end }}
