apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "kache-service.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "kache-service.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  selector:
    matchLabels:
      {{- include "kache-service.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      labels:
        {{- include "kache-service.selectorLabels" . | nindent 8 }}
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
    spec:
      {{- if .Values.ha.enabled }}
      serviceAccountName: {{ include "kache-service.serviceAccountName" . }}
      automountServiceAccountToken: true
      {{- else }}
      automountServiceAccountToken: false
      {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: planner
          image: {{ include "kache-service.image" . | quote }}
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          securityContext:
            {{- toYaml .Values.containerSecurityContext | nindent 12 }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          env:
            - name: KACHE_PLANNER_BIND
              value: "0.0.0.0:{{ .Values.service.port }}"
            - name: KACHE_PLANNER_NAME
              value: {{ .Values.planner.name | quote }}
            - name: KACHE_PLANNER_DB_PATH
              value: {{ .Values.planner.dbPath | quote }}
            {{- if .Values.planner.seedStateFile }}
            - name: KACHE_PLANNER_SEED_STATE_FILE
              value: {{ .Values.planner.seedStateFile | quote }}
            {{- end }}
            - name: KACHE_LOG
              value: {{ .Values.logLevel | quote }}
            {{- if .Values.auth.existingSecret }}
            - name: KACHE_PLANNER_TOKEN
              valueFrom:
                secretKeyRef:
                  name: {{ .Values.auth.existingSecret | quote }}
                  key: {{ .Values.auth.existingSecretKey | quote }}
            {{- end }}
            {{- if .Values.ha.enabled }}
            - name: KACHE_HA_ENABLED
              value: "true"
            - name: KACHE_HA_LEASE_NAME
              value: {{ .Values.ha.leaseName | quote }}
            - name: KACHE_HA_NAMESPACE
              {{- if .Values.ha.namespace }}
              value: {{ .Values.ha.namespace | quote }}
              {{- else }}
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
              {{- end }}
            {{- end }}
            {{- with .Values.extraEnv }}
            {{- toYaml . | nindent 12 }}
            {{- end }}
          volumeMounts:
            {{- if .Values.planner.persistence.enabled }}
            - name: planner-data
              mountPath: {{ .Values.planner.persistence.mountPath | quote }}
            {{- end }}
          readinessProbe:
            httpGet:
              path: /readyz
              port: http
            initialDelaySeconds: 2
            periodSeconds: 5
          livenessProbe:
            httpGet:
              path: /healthz
              port: http
            initialDelaySeconds: 5
            periodSeconds: 10
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
      {{- if .Values.planner.persistence.enabled }}
      volumes:
        - name: planner-data
          {{- if eq .Values.planner.persistence.type "ephemeral" }}
          emptyDir: {}
          {{- else if eq .Values.planner.persistence.type "pvc" }}
          persistentVolumeClaim:
            claimName: {{ include "kache-service.fullname" . }}
          {{- else }}
          {{- fail "planner.persistence.type must be 'ephemeral' or 'pvc'" }}
          {{- end }}
      {{- end }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
