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 }}
{{- 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 }}
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
    spec:
      initContainers:
        - name: chown-db
          image: {{ .Values.init.image.repository }}:{{ .Values.init.image.tag }}
          imagePullPolicy: {{ .Values.init.image.pullPolicy }}
          command: ["chown", "1000:1000", "/var/db/cerebro"]
          volumeMounts:
            - name: db
              mountPath: /var/db/cerebro
      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
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
{{ toYaml .Values.resources | indent 12 }}
      volumes:
        - name: db
          emptyDir: {}
        - name: config
          configMap:
            name: {{ template "cerebro.fullname" . }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
