apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "kafka-ui.fullname" . }}
  labels:
    {{- include "kafka-ui.labels" . | nindent 4 }}
spec:
{{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
{{- end }}
  selector:
    matchLabels:
      {{- include "kafka-ui.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      annotations:
      {{- with .Values.podAnnotations }}
          {{- toYaml . | nindent 8 }}
      {{- end }}
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
        checksum/secret: {{ include (print $.Template.BasePath "/secret.yaml") . | sha256sum }}
      labels:
        {{- include "kafka-ui.selectorLabels" . | nindent 8 }}
        {{- if .Values.podLabels }}
        {{- toYaml .Values.podLabels | nindent 8 }}
        {{- end }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.initContainers }}
      initContainers:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "kafka-ui.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: {{ include "kafka-ui.imageName" . }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          {{- if or .Values.env  .Values.yamlApplicationConfig .Values.yamlApplicationConfigConfigMap}}
          env:
            {{- with .Values.env }}
              {{- toYaml . | nindent 12 }}
            {{- end }}
            {{- if or .Values.yamlApplicationConfig .Values.yamlApplicationConfigConfigMap}}
            - name: SPRING_CONFIG_LOCATION
              {{- if .Values.yamlApplicationConfig }}
              value: /kafka-ui/config.yml
              {{- else if .Values.yamlApplicationConfigConfigMap }}
              value: /kafka-ui/{{ .Values.yamlApplicationConfigConfigMap.keyName | default "config.yml" }}
              {{- end }}
            {{- end }}
          {{- end }}
          envFrom:
            {{- if .Values.existingConfigMap }}
            - configMapRef:
                name: {{ .Values.existingConfigMap }}
            {{- end }}
            {{- if .Values.envs.config }}
            - configMapRef:
                name: {{ include "kafka-ui.fullname" . }}
            {{- end }}
            {{- if .Values.existingSecret }}
            - secretRef:
                name: {{ .Values.existingSecret }}
            {{- end }}
            {{- if .Values.envs.secret}}
            - secretRef:
                name: {{ include "kafka-ui.fullname" . }}
            {{- end}}    
          ports:
            - name: http
              containerPort: 8080
              protocol: TCP
          livenessProbe:
            httpGet:
              {{- $contextPath := .Values.envs.config.SERVER_SERVLET_CONTEXT_PATH | default "" | printf "%s/actuator/health" | urlParse }}
              path: {{ get $contextPath "path" }}
              port: http
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 10
          readinessProbe:
            httpGet:
              {{- $contextPath := .Values.envs.config.SERVER_SERVLET_CONTEXT_PATH | default "" | printf "%s/actuator/health" | urlParse }}
              path: {{ get $contextPath "path" }}
              port: http
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 10
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          {{- if or .Values.yamlApplicationConfig .Values.volumeMounts .Values.yamlApplicationConfigConfigMap}}
          volumeMounts:
            {{- with .Values.volumeMounts }} 
              {{- toYaml . | nindent 12 }}
            {{- end }}
            {{- if .Values.yamlApplicationConfig }}
            - name: kafka-ui-yaml-conf
              mountPath: /kafka-ui/
            {{- end }}
            {{- if .Values.yamlApplicationConfigConfigMap}}
            - name: kafka-ui-yaml-conf-configmap
              mountPath: /kafka-ui/
            {{- end }}
          {{- end }}
      {{- if or .Values.yamlApplicationConfig .Values.volumes .Values.yamlApplicationConfigConfigMap}}
      volumes:
        {{- with .Values.volumes }}
          {{- toYaml . | nindent 8 }}
        {{- end }}
        {{- if .Values.yamlApplicationConfig }}
        - name: kafka-ui-yaml-conf
          configMap: 
            name: {{ include "kafka-ui.fullname" . }}-fromvalues
        {{- end }}
        {{- if .Values.yamlApplicationConfigConfigMap}}
        - name: kafka-ui-yaml-conf-configmap
          configMap: 
            name: {{ .Values.yamlApplicationConfigConfigMap.name }}
        {{- end }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}