apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "cheminee.fullname" . }}
  labels:
    {{- include "cheminee.labels" . | nindent 4 }}
spec:
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  revisionHistoryLimit: 1
  {{- end }}
  {{- if .Values.persistence.enabled }}
  strategy:
    type: Recreate
  {{- end }}
  selector:
    matchLabels:
      {{- include "cheminee.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "cheminee.selectorLabels" . | nindent 8 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "cheminee.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}
          securityContext:
            {{- toYaml .Values.securityContext | nindent 12 }}
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command:
            - cheminee
            - rest-api-server
            - "--bind=0.0.0.0:{{ $.Values.service.port }}"
            - --index-storage-directory=/data
          {{- if gt (len $.Values.ingress.tls) 0 }}
            - "--server-url=https://{{ ($.Values.ingress.hosts | first).host }}"
          {{- else }}
            - "--server-url=http://{{ ($.Values.ingress.hosts | first).host }}"
          {{- end }}
          ports:
            - name: http
              containerPort: {{ .Values.service.port }}
              protocol: TCP
          {{- if .Values.persistence.enabled }}
          volumeMounts:
            - mountPath: /data
              name: data
              subPath: data
          {{- end }}
          livenessProbe:
            httpGet:
              path: /api/v1/openapi.json # TODO: We need a low-cost health endpoint
              port: http
          readinessProbe:
            httpGet:
              path: /api/v1/openapi.json
              port: http
          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 }}
      {{- if .Values.persistence.enabled }}
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: {{ include "cheminee.fullname" . }}-data
      {{- end }}