apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "cerebro.fullname" . }}
  labels:
    app: {{ template "cerebro.name" . }}
    chart: {{ template "cerebro.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    {{- range $key, $value := .Values.deployment.labels }}
    {{ $key }}: {{ $value | quote }}
    {{- end }}
{{- if .Values.deployment.annotations }}
  annotations:
{{ toYaml .Values.deployment.annotations | indent 4 }}
{{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  revisionHistoryLimit: {{ .Values.revisionHistoryLimit }}
  selector:
    matchLabels:
      app: {{ template "cerebro.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "cerebro.name" . }}
        release: {{ .Release.Name }}
        {{- if .Values.deployment.podLabels }}
        {{- toYaml .Values.deployment.podLabels | nindent 8 }}
        {{- end }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- if .Values.deployment.podAnnotations }}
        {{- toYaml .Values.deployment.podAnnotations | nindent 8 }}
        {{- end }}
    spec:
      {{- if .Values.securityContext }}
      securityContext:
{{ toYaml .Values.securityContext | indent 8 }}
      {{- end }}
      {{- if .Values.image.pullSecrets }}
      imagePullSecrets:
      {{- range .Values.image.pullSecrets }}
        - name: {{ . }}
      {{- end }}
      {{- end }}
      {{- if .Values.priorityClassName }}
      priorityClassName: {{ .Values.priorityClassName }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          args: ["-Dconfig.file=/etc/cerebro/application.conf"]
          ports:
            - name: http
              containerPort: 9000
              protocol: TCP
          volumeMounts:
            - name: db
              mountPath: /var/db/cerebro
            - name: config
              mountPath: /etc/cerebro
            - name: logs
              mountPath: /opt/cerebro/logs/
            - name: tmp
              mountPath: /tmp
          {{- if .Values.volumeMounts }}
{{ toYaml .Values.volumeMounts | indent 12 }}
          {{- end }}
          {{- if .Values.env }}
          env:
          {{- range $index, $element := .Values.env }}
          - name: {{ $index | quote }}
            value: {{ $element | quote }}
          {{- end }}
          {{- end }}
          {{- if .Values.envFromSecretRef }}
          envFrom:
            - secretRef:
                name: "{{ .Values.envFromSecretRef }}"
          {{- end }}
          {{- if .Values.deployment.livenessProbe.enabled}}
          livenessProbe:
            httpGet:
              path: {{ .Values.config.basePath }}
              port: http
            initialDelaySeconds: 120
          {{- end }}
          {{- if .Values.deployment.readinessProbe.enabled}}
          readinessProbe:
            httpGet:
              path: {{ .Values.config.basePath }}
              port: http
            initialDelaySeconds: 5
          {{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
      volumes:
        - name: db
          emptyDir: {}
        - name: config
          configMap:
            name: {{ template "cerebro.fullname" . }}
        - name: logs
          emptyDir: {}
        - name: tmp
          emptyDir: {}
    {{- if .Values.volumes }}
{{ toYaml .Values.volumes | indent 8 }}
    {{- end }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
