{{- if and .Values.dynamicConfig.enabled .Values.dynamicConfig.upload.enabled  -}}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "collabora-online.fullname" . }}-dynconfig
  labels:
    {{- include "collabora-online.labels" . | nindent 4 }}
spec:
  serviceName: {{ include "collabora-online.fullname" . }}-dynconfig
  replicas: 1
  selector:
    matchLabels:
      {{- include "collabora-online.selectorLabels" . | nindent 6 }}
      type: dynconfig
  template:
    metadata:
      {{- with .Values.dynamicConfig.podAnnotations }}
      annotations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      labels:
        {{- include "collabora-online.selectorLabels" . | nindent 8 }}
        type: dynconfig
        # confighash: config-{{ .Values | toYaml | sha256sum | trunc 32 }}
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      serviceAccountName: {{ include "collabora-online.serviceAccountName" . }}
      securityContext:
        {{- toYaml .Values.dynamicConfig.podSecurityContext | nindent 8 }}
      containers:
        - name: {{ .Chart.Name }}-dynconfig
          securityContext:
            {{- toYaml .Values.dynamicConfig.securityContext | nindent 12 }}
          image: "{{ .Values.dynamicConfig.image.repository }}:{{ .Values.dynamicConfig.image.tag }}"
          imagePullPolicy: {{ .Values.dynamicConfig.image.pullPolicy }}
          ports:
            - name: http
              containerPort: {{ .Values.dynamicConfig.containerPort }}
              protocol: TCP
          {{- if .Values.probes.startup.enabled }}
          startupProbe:
            httpGet:
              path: /
              port: {{ .Values.dynamicConfig.containerPort }}
              scheme: HTTP
            failureThreshold: {{ .Values.dynamicConfig.probes.startup.failureThreshold }}
            periodSeconds: {{ .Values.dynamicConfig.probes.startup.periodSeconds }}
          {{- end }}
          {{- if .Values.dynamicConfig.probes.liveness.enabled }}
          livenessProbe:
            httpGet:
              path: /
              port: {{ .Values.dynamicConfig.containerPort }}
              scheme: HTTP
            initialDelaySeconds: {{ .Values.dynamicConfig.probes.liveness.initialDelaySeconds }}
            periodSeconds: {{ .Values.dynamicConfig.probes.liveness.periodSeconds }}
            timeoutSeconds: {{ .Values.dynamicConfig.probes.liveness.timeoutSeconds }}
            successThreshold: {{ .Values.dynamicConfig.probes.liveness.successThreshold }}
            failureThreshold: {{ .Values.dynamicConfig.probes.liveness.failureThreshold }}
          {{- end }}
          {{- if .Values.dynamicConfig.probes.readiness.enabled }}
          readinessProbe:
            httpGet:
              path: /
              port: {{ .Values.dynamicConfig.containerPort }}
              scheme: HTTP
            initialDelaySeconds: {{ .Values.dynamicConfig.probes.readiness.initialDelaySeconds }}
            periodSeconds: {{ .Values.dynamicConfig.probes.readiness.periodSeconds }}
            timeoutSeconds: {{ .Values.dynamicConfig.probes.readiness.timeoutSeconds }}
            successThreshold: {{ .Values.dynamicConfig.probes.readiness.successThreshold }}
            failureThreshold: {{ .Values.dynamicConfig.probes.readiness.failureThreshold }}
          {{- end }}
            {{- with .Values.dynamicConfig.env }}
            {{ toYaml . | nindent 12 }}
            {{- end }}
          resources:
            {{- toYaml .Values.dynamicConfig.resources | nindent 12 }}
          volumeMounts:
            - name: config
              mountPath: /usr/share/nginx/html/config
        - name: {{ .Chart.Name }}-dynconfig-upload
          image: {{ .Values.dynamicConfig.upload.image.repository }}@sha256:{{ .Values.dynamicConfig.upload.image.digest }}
          envFrom:
            - secretRef:
                name: {{ include "collabora-online.fullname" . }}-upload-env
          ports:
            - name: "upload-http"
              containerPort: 3000
          volumeMounts:
            - name: "config"
              mountPath: "/config"
      {{- with .Values.dynamicConfig.nodeSelector }}
      nodeSelector:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.dynamicConfig.affinity }}
      affinity:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.dynamicConfig.tolerations }}
      tolerations:
        {{- toYaml . | nindent 8 }}
      {{- end }}
  volumeClaimTemplates:
    - metadata:
        name: config
      spec:
        accessModes: [ "{{ .Values.dynamicConfig.upload.pvc.accessMode }}" ]
        resources:
          requests:
            storage: {{ .Values.dynamicConfig.upload.pvc.size }}
        {{- if .Values.dynamicConfig.upload.pvc.storageClassName }}
        storageClassName: {{ .Values.dynamicConfig.upload.pvc.storageClassName }}
        {{- end }}
{{- end }}
