apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "karma.fullname" . }}
  labels:
    app.kubernetes.io/name: {{ include "karma.name" . }}
    helm.sh/chart: {{ include "karma.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
{{- with .Values.deployment.annotations }}
  annotations:
{{ toYaml . | indent 4 }}
{{- end }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "karma.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ include "karma.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        {{- if .Values.podLabels -}}
        {{ toYaml .Values.podLabels | nindent 8 }}
        {{- end }}
      {{- if or .Values.configMap.enabled .Values.podAnnotations }}
      annotations:
        {{- if .Values.configMap.enabled }}
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if .Values.podAnnotations -}}
        {{ toYaml .Values.podAnnotations | nindent 8 }}
        {{- end }}
      {{- end }}
    spec:
      serviceAccountName: {{ template "karma.serviceAccountName" . }}
      {{- if .Values.securityContext }}
      securityContext:
{{ toYaml .Values.securityContext | indent 8 }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          env:
          {{- if .Values.configMap.enabled }}
          - name: CONFIG_FILE
            value: /etc/karma/karma.conf
          {{- end }}
          {{- with .Values.env }}
{{ toYaml . | indent 10 }}
          {{- end }}
          livenessProbe:
            httpGet:
              path: {{ .Values.livenessProbe.path }}
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.delay }}
            periodSeconds: {{ .Values.livenessProbe.period }}
          readinessProbe:
            httpGet:
              path: {{ .Values.livenessProbe.path }}
              port: http
            initialDelaySeconds: {{ .Values.livenessProbe.delay }}
            periodSeconds: {{ .Values.livenessProbe.period }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
          volumeMounts:
          {{- if .Values.configMap.enabled }}
            - name: karma-config
              mountPath: /etc/karma
          {{- end }}
          {{- range .Values.certSecretNames }}
            - name: {{ . }}
              mountPath: /etc/certs/{{ . }}
              readOnly: true
          {{- 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 }}
      volumes:
      {{- if .Values.configMap.enabled }}
      - name: karma-config
        configMap:
          name: {{ .Release.Name }}-config
      {{- end }}
      {{- range .Values.certSecretNames }}
      - name: {{ . }}
        secret:
          defaultMode: 420
          secretName: {{ . }}
      {{- end }}
