apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "akhq.fullname" . }}
  {{- with .Values.annotations }}
  annotations:
    {{- toYaml . | nindent 4 }}
  {{- end }}
  labels:
    app.kubernetes.io/name: {{ include "akhq.name" . }}
    helm.sh/chart: {{ include "akhq.chart" . }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
     {{- with .Values.labels }}
       {{- toYaml . | nindent 4 }}
     {{- end }}
spec:
  replicas: {{ .Values.replicaCount | default 1 }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ include "akhq.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      annotations:
        {{- if .Values.configuration }}
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        {{- end }}
        {{- if and (not .Values.existingSecret) (.Values.secrets) }}
        checksum/secrets: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
        {{- end }}
        {{- with .Values.podAnnotations }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
      labels:
        app.kubernetes.io/name: {{ include "akhq.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        {{- with .Values.podLabels }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      {{- if .Values.imagePullSecrets }}
      imagePullSecrets:
        {{ toYaml .Values.imagePullSecrets | indent 8 }}
      {{- end }}
      {{- if .Values.securityContext }}
      securityContext:
        {{ toYaml .Values.securityContext | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "akhq.serviceAccountName" . }}
      {{- if .Values.initContainers }}
      initContainers:
        {{- range $key, $value := .Values.initContainers }}
        - name: {{ $key }}
{{ toYaml $value | indent 10 }}
        {{- end }}
      {{- end }}
      containers:
        - name: {{ .Chart.Name }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy | default "Always" }}
          {{- if .Values.containerSecurityContext }}
          securityContext:
            {{ toYaml .Values.containerSecurityContext | nindent 12 }}
          {{- end }}
          env:
            {{- if .Values.extraEnv }}{{ toYaml .Values.extraEnv | trim | nindent 12 }}{{ end }}
            {{- if or (.Values.existingSecrets) (.Values.secrets) }}
            - name: MICRONAUT_ENVIRONMENTS
              value: secrets
            - name: MICRONAUT_CONFIG_FILES
              value: /app/application.yml,/app/application-secrets.yml
            {{- end }}
          volumeMounts:
          {{- if .Values.extraVolumeMounts }}{{ toYaml .Values.extraVolumeMounts | trim | nindent 12 }}{{ end }}
          {{- if .Values.configuration }}
            - name: config
              mountPath: /app/application.yml
              subPath: application.yml
          {{- end }}
          {{- if or (.Values.existingSecrets) (.Values.secrets) }}
            - name: secrets
              mountPath: /app/application-secrets.yml
              subPath: application-secrets.yml
          {{- end }}
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
            - name: management
              containerPort: 28081
              protocol: TCP
          livenessProbe:
            tcpSocket:
              port: management
          readinessProbe:
            httpGet:
              path: {{ .Values.readinessProbe.prefix | default "" }}/health
              port: management
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      volumes:
      {{- if .Values.extraVolumes  }}{{ toYaml .Values.extraVolumes | trim | nindent 6 }}{{ end }}
      {{- if .Values.configuration }}
      - name: config
        configMap:
          name: {{ template "akhq.fullname" . }}
      {{- end }}
      {{- if or (.Values.existingSecrets) (.Values.secrets) }}
      - name: secrets
        secret:
          {{- if .Values.existingSecrets }}
          secretName: {{ .Values.existingSecrets }}
          {{- else }}
          secretName: {{ template "akhq.fullname" . }}-secrets
          {{- end }}
      {{- end }}
