{{- $kubefullname := include "qovery-engine.fullname" . }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: qovery-engine
  namespace: {{ .Values.namespace }}
  labels:
    {{- include "qovery-engine.labels" . | nindent 4 }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: qovery-engine
  selector:
    matchLabels:
      {{- include "qovery-engine.selectorLabels" . | nindent 6 }}
  template:
    metadata:
    {{- with .Values.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
    {{- end }}
      labels:
        {{- include "qovery-engine.selectorLabels" . | nindent 8 }}
    spec:
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "qovery-engine.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.podSecurityContext | nindent 8 }}
      initContainers:
        - name: clean-data
          image: busybox:latest
          command:
            - /bin/sh
            - -c
            - |
              rm -Rf {{ .Values.volumes.qoveryWorkspace.path }}/* {{ .Values.volumes.dockerGraphStorage.path }}/*
              chmod 777 {{ .Values.volumes.qoveryWorkspace.path }} {{ .Values.volumes.dockerGraphStorage.path }}
          volumeMounts:
            - name: docker-graph-storage
              mountPath: {{ .Values.volumes.dockerGraphStorage.path }}
            - name: qovery-workspace
              mountPath: {{ .Values.volumes.qoveryWorkspace.path }}
      containers:
        - name: qovery-engine
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          env:
            {{ range $key, $value := .Values.environmentVariables -}}
            - name: "{{ $key }}"
              valueFrom:
                secretKeyRef:
                  name: {{ $kubefullname }}
                  key: {{ $key }}
            {{ end }}
          resources:
            {{- toYaml .Values.engineResources | nindent 12 }}
          volumeMounts:
            - name: qovery-workspace
              mountPath: {{ .Values.volumes.qoveryWorkspace.path }}
              # Required for image cleaning
            - name: docker-graph-storage
              mountPath: {{ .Values.volumes.dockerGraphStorage.path }}
        - name: docker-build
          image: "{{ .Values.buildContainer.image }}:{{ .Values.buildContainer.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          securityContext:
          {{- toYaml .Values.securityContext | nindent 12 }}
          env:
            - name: DOCKER_TLS_CERTDIR
              value: ""
            {{ range $key, $value := .Values.environmentVariables -}}
            - name: "{{ $key }}"
              valueFrom:
                secretKeyRef:
                  name: qovery-engine
                  key: {{ $key }}
            {{ end }}
          livenessProbe:
            exec:
              command: [ "docker", "info" ]
            initialDelaySeconds: 15
            periodSeconds: 30
          resources:
          {{- toYaml .Values.buildResources | nindent 12 }}
          volumeMounts:
            - name: docker-graph-storage
              mountPath: {{ .Values.volumes.dockerGraphStorage.path }}
      {{- with .Values.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
  volumeClaimTemplates:
  - metadata:
      name: docker-graph-storage
      labels:
        {{- include "qovery-engine.labels" . | nindent 8 }}
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: {{ .Values.volumes.storageClassName }}
      resources:
        requests:
          storage: {{ .Values.volumes.dockerGraphStorage.size }}
  - metadata:
      name: qovery-workspace
      labels:
        {{- include "qovery-engine.labels" . | nindent 8 }}
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: {{ .Values.volumes.storageClassName }}
      resources:
        requests:
          storage: {{ .Values.volumes.qoveryWorkspace.size }}