apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "ipfs.fullname" . }}
  labels:
    app: {{ template "ipfs.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  replicas: {{ .Values.replicaCount }}
  serviceName: {{ template "ipfs.servicename" . }}
  selector:
    matchLabels:
      app: {{ template "ipfs.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ template "ipfs.name" . }}
        chart: {{ .Chart.Name }}-{{ .Chart.Version }}
        heritage: {{ .Release.Service }}
        release: {{ .Release.Name }}
    spec:
      securityContext:
        # The image runs as uid 1000 by default, and needs to be able to write to
        # the persistent volume to be able to start.
        fsGroup: 1000
        runAsUser: 1000
      containers:
      - name: {{ .Chart.Name }}
        image: {{ .Values.image }}
        livenessProbe:
          httpGet:
            path: /debug/metrics/prometheus
            port: api
          initialDelaySeconds: 15
          periodSeconds: 3
        readinessProbe:
          httpGet:
            path: /debug/metrics/prometheus
            port: api
          initialDelaySeconds: 15
          periodSeconds: 3
        ports:
{{- if .Values.swarm.enabled }}
        - containerPort: 4001
          name: swarm
{{- end }}
        - containerPort: 5001
          name: api
        - containerPort: 8080
          name: gateway
        volumeMounts:
          - name: ipfs-storage
            mountPath: /data/ipfs
        resources:
{{ toYaml .Values.resources | indent 10 }}

  {{- if .Values.persistence.enabled }}
  volumeClaimTemplates:
    - metadata:
        name: ipfs-storage
        {{- range $key, $value := .Values.persistence.annotations }}
          {{ $key }}: {{ $value }}
        {{- end }}
      spec:
        accessModes:
          {{- range .Values.persistence.accessModes }}
          - {{ . | quote }}
          {{- end }}
        resources:
          requests:
            storage: {{ .Values.persistence.size | quote }}
      {{- if .Values.persistence.storageClass }}
      {{- if (eq "-" .Values.persistence.storageClass) }}
        storageClassName: ""
      {{- else }}
        storageClassName: "{{ .Values.persistence.storageClass }}"
      {{- end }}
      {{- end }}
  {{- else }}
      volumes:
        - name: ipfs-storage
          emptyDir: {}
  {{- end }}
