{{- if eq .Values.deployment.kind "StatefulSet" -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "collabora-online.fullname" . }}
  labels:
    {{- include "collabora-online.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "collabora-online.fullname" . }}
  minReadySeconds: {{ .Values.deployment.minReadySeconds }}
  {{- if not .Values.autoscaling.enabled }}
  replicas: {{ .Values.replicaCount }}
  {{- end }}
  selector:
    matchLabels:
      {{- include "collabora-online.selectorLabels" . | nindent 6 }}
      type: main
  template:
    metadata:
      annotations:
        {{- with .Values.podAnnotations }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
        confighash: config-{{ .Values.collabora | toYaml | sha256sum | trunc 32 }}
        cluster-autoscaler.kubernetes.io/safe-to-evict: "true"
      labels:
        {{- include "collabora-online.selectorLabels" . | nindent 8 }}
        type: main
        {{- with .Values.podLabels }}
        {{- toYaml . | nindent 8 }}
        {{- end }}
    spec:
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }}
      {{- with .Values.deployment.hostAliases }}
      hostAliases:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "collabora-online.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 }}
          ports:
            - name: http
              containerPort: {{ .Values.deployment.containerPort }}
              protocol: TCP
          {{- if .Values.probes.startup.enabled }}
          startupProbe:
            httpGet:
              path: /
              port: {{ .Values.deployment.containerPort }}
              scheme: HTTP
            failureThreshold: {{ .Values.probes.startup.failureThreshold }}
            periodSeconds: {{ .Values.probes.startup.periodSeconds }}
          {{- end }}
          {{- if .Values.probes.liveness.enabled }}
          livenessProbe:
            httpGet:
              path: /
              port: {{ .Values.deployment.containerPort }}
              scheme: HTTP
            initialDelaySeconds: {{ .Values.probes.liveness.initialDelaySeconds }}
            periodSeconds: {{ .Values.probes.liveness.periodSeconds }}
            timeoutSeconds: {{ .Values.probes.liveness.timeoutSeconds }}
            successThreshold: {{ .Values.probes.liveness.successThreshold }}
            failureThreshold: {{ .Values.probes.liveness.failureThreshold }}
          {{- end }}
          {{- if .Values.probes.readiness.enabled }}
          readinessProbe:
            httpGet:
              path: /
              port: {{ .Values.deployment.containerPort }}
              scheme: HTTP
            initialDelaySeconds: {{ .Values.probes.readiness.initialDelaySeconds }}
            periodSeconds: {{ .Values.probes.readiness.periodSeconds }}
            timeoutSeconds: {{ .Values.probes.readiness.timeoutSeconds }}
            successThreshold: {{ .Values.probes.readiness.successThreshold }}
            failureThreshold: {{ .Values.probes.readiness.failureThreshold }}
          {{- end }}

          envFrom:
            - configMapRef:
                name: {{ include "collabora-online.fullname" . }}
          env:
            - name: username
              valueFrom:
                secretKeyRef:
                  {{- if (.Values.collabora.existingSecret).enabled }}
                  name: {{ .Values.collabora.existingSecret.secretName | quote }}
                  key: {{ .Values.collabora.existingSecret.usernameKey | quote }}
                  {{- else }}
                  name: {{ include "collabora-online.fullname" . }}
                  key: username
                  {{- end }}
            - name: password
              valueFrom:
                secretKeyRef:
                  {{- if (.Values.collabora.existingSecret).enabled }}
                  name: {{ .Values.collabora.existingSecret.secretName | quote }}
                  key: {{ .Values.collabora.existingSecret.passwordKey | quote }}
                  {{- else }}
                  name: {{ include "collabora-online.fullname" . }}
                  key: password
                  {{- end }}
            {{- with .Values.collabora.env }}
            {{ toYaml . | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.resources | nindent 12 }}
          volumeMounts:
            - name: tmp
              mountPath: /tmp
      {{- 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:
        - name: tmp
          emptyDir: {}
{{- end }}
