{{- range .Values.statefuls }}
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ include "exam.fullname" $ }}-{{ .name }}
  labels:
    app: {{ .name }}
    {{- include "exam.labels" $ | nindent 4 }}
spec:
  serviceName: {{ include "exam.fullname" $ }}-{{ .name }}
  replicas: 1
  selector:
    matchLabels:
      app: {{ .name }}
      tier: {{ .tier }}
    {{- include "exam.selectorLabels" $ | nindent 6 }}
  template:
    metadata:
      labels:
        app: {{ .name }}
        tier: {{ .tier }}
      {{- include "exam.selectorLabels" $ | nindent 8 }}
    spec:
      terminationGracePeriodSeconds: {{ default "30" .grace }}
      {{- if .initContainer }}
      initContainers:
        - name: {{ .name }}-init
          {{- with .image }}
          image: {{ .repository }}:{{ .tag | default $.Chart.AppVersion }}
          {{- end }}
          {{- with .initContainer }}
            {{- toYaml . | nindent 10 }}
          {{- end }}
      {{- end }}
      containers:
        - name: {{ .name }}
          {{- with .image }}
          image: {{ .repository }}:{{ .tag | default $.Chart.AppVersion }}
          {{- end }}
          ports:
            - name: {{ .name }}
              containerPort: {{ .port }}
          {{- if .env }}
          env:
            {{- with .env }}
            {{- range $key, $val := . }}
            {{- if ne "secrets" $key }}
            {{- $envKey := $key | upper | replace "-" "_" }}
            - name: {{ $envKey }}
              value: {{ quote $val }}
            {{- else }}
            {{- range $key, $val := $val }}
            {{- $envKey := $key | upper | replace "-" "_" }}
            - name: {{ $envKey }}
              valueFrom:
                secretKeyRef:
                  name: {{ include "exam.secrets" $ }}
                  key: {{ $val }}
            {{- end }}
            {{- end }}
            {{- end }}
            {{- end }}
          {{- end }}
          resources: {}
          livenessProbe:
            exec:
              {{- toYaml .probe | nindent 14 }}
            initialDelaySeconds: 5
          readinessProbe:
            exec:
              {{- toYaml .probe | nindent 14 }}
            initialDelaySeconds: 5
          {{- if .mountPath }}
          volumeMounts:
            - mountPath: {{ .mountPath }}
              name: {{ .storage }}
          {{- end }}
      {{- if .mountPath }}
      volumes:
        - name: {{ .storage }}
          persistentVolumeClaim:
            claimName: {{ include "exam.pvclaim" $ }}
      {{- end }}
{{- end }}