{{- if .Values.filer.enabled }}
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: {{ template "seaweedfs.name" . }}-filer
  namespace: {{ .Release.Namespace }}
  labels:
    app.kubernetes.io/name: {{ template "seaweedfs.name" . }}
    helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
    app.kubernetes.io/managed-by: {{ .Release.Service }}
    app.kubernetes.io/instance: {{ .Release.Name }}
    app.kubernetes.io/component: filer
spec:
  serviceName: {{ template "seaweedfs.name" . }}-filer
  podManagementPolicy: {{ .Values.filer.podManagementPolicy }}
  replicas: {{ .Values.filer.replicas }}
  {{- if (gt (int .Values.filer.updatePartition) 0) }}
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      partition: {{ .Values.filer.updatePartition }}
  {{- end }}
  selector:
    matchLabels:
      app.kubernetes.io/name: {{ template "seaweedfs.name" . }}
      app.kubernetes.io/instance: {{ .Release.Name }}
      app.kubernetes.io/component: filer
  template:
    metadata:
      labels:
        app.kubernetes.io/name: {{ template "seaweedfs.name" . }}
        helm.sh/chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
        app.kubernetes.io/instance: {{ .Release.Name }}
        app.kubernetes.io/component: filer
      {{- with .Values.podLabels }}
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.filer.podLabels }}
      {{- toYaml . | nindent 8 }}
      {{- end }}
      annotations:
      {{- with .Values.podAnnotations }}
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- with .Values.filer.podAnnotations }}
      {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.filer.s3.existingConfigSecret }}
        {{- $configSecret := (lookup "v1" "Secret" .Release.Namespace .Values.filer.s3.existingConfigSecret) | default dict }}
        checksum/s3config: {{ $configSecret | toYaml | sha256sum }}
      {{- else }}
        checksum/s3config: {{ include (print .Template.BasePath "/s3-secret.yaml") . | sha256sum }}
      {{- end }}
    spec:
      restartPolicy: {{ default .Values.global.restartPolicy .Values.filer.restartPolicy }}
      {{- if .Values.filer.affinity }}
      affinity:
        {{ tpl .Values.filer.affinity . | nindent 8 | trim }}
      {{- end }}
      {{- if .Values.filer.tolerations }}
      tolerations:
        {{ tpl .Values.filer.tolerations . | nindent 8 | trim }}
      {{- end }}
      {{- include "seaweedfs.imagePullSecrets" . | nindent 6 }}
      serviceAccountName: {{ .Values.filer.serviceAccountName | default .Values.global.serviceAccountName | quote }} # for deleting statefulset pods after migration
      terminationGracePeriodSeconds: 60
      {{- if .Values.filer.priorityClassName }}
      priorityClassName: {{ .Values.filer.priorityClassName | quote }}
      {{- end }}
      enableServiceLinks: false
      {{- if .Values.filer.initContainers }}
      initContainers:
        {{ tpl .Values.filer.initContainers . | nindent 8 | trim }}
      {{- end }}
      {{- if .Values.filer.podSecurityContext.enabled }}
      securityContext: {{- omit .Values.filer.podSecurityContext "enabled" | toYaml | nindent 8 }}
      {{- end }}
      containers:
        - name: seaweedfs
          image: {{ template "filer.image" . }}
          imagePullPolicy: {{ default "IfNotPresent" .Values.global.imagePullPolicy }}
          env:
            - name: POD_IP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: WEED_MYSQL_USERNAME
              valueFrom:
                secretKeyRef:
                  name: secret-seaweedfs-db
                  key: user
                  optional: true
            - name: WEED_MYSQL_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: secret-seaweedfs-db
                  key: password
                  optional: true
            - name: SEAWEEDFS_FULLNAME
              value: "{{ template "seaweedfs.name" . }}"
            {{- if .Values.filer.extraEnvironmentVars }}
            {{- range $key, $value := .Values.filer.extraEnvironmentVars }}
            - name: {{ $key }}
            {{- if kindIs "string" $value }}
              value: {{ $value | quote }}
            {{- else }}
              valueFrom:
                {{ toYaml $value | nindent 16 | trim }}
            {{- end -}}
            {{- end }}
            {{- end }}
            {{- if .Values.global.extraEnvironmentVars }}
            {{- range $key, $value := .Values.global.extraEnvironmentVars }}
            - name: {{ $key }}
            {{- if kindIs "string" $value }}
              value: {{ $value | quote }}
            {{- else }}
              valueFrom:
                {{ toYaml $value | nindent 16 | trim }}
            {{- end -}}
            {{- end }}
            {{- end }}
            {{- if .Values.filer.secretExtraEnvironmentVars }}
            {{- range $key, $value := .Values.filer.secretExtraEnvironmentVars }}
            - name: {{ $key }}
              valueFrom: {{ toYaml $value | nindent 16 }}
            {{- end }}
            {{- end }}
          command:
            - "/bin/sh"
            - "-ec"
            - |
              exec /usr/bin/weed \
              {{- if or (eq .Values.filer.logs.type "hostPath") (eq .Values.filer.logs.type "persistentVolumeClaim") (eq .Values.filer.logs.type "emptyDir") }}
              -logdir=/logs \
              {{- else }}
              -logtostderr=true \
              {{- end }}
              {{- if .Values.filer.loggingOverrideLevel }}
              -v={{ .Values.filer.loggingOverrideLevel }} \
              {{- else }}
              -v={{ .Values.global.loggingLevel }} \
              {{- end }}
              filer \
              -port={{ .Values.filer.port }} \
              {{- if .Values.filer.metricsPort }}
              -metricsPort={{ .Values.filer.metricsPort }} \
              {{- end }}
              {{- if .Values.filer.redirectOnRead }}
              -redirectOnRead \
              {{- end }}
              {{- if .Values.filer.disableHttp }}
              -disableHttp \
              {{- end }}
              {{- if .Values.filer.disableDirListing }}
              -disableDirListing \
              {{- end }}
              -dirListLimit={{ .Values.filer.dirListLimit }} \
              {{- if .Values.global.enableReplication }}
              -defaultReplicaPlacement={{ .Values.global.replicationPlacment }} \
              {{- else }}
              -defaultReplicaPlacement={{ .Values.filer.defaultReplicaPlacement }} \
              {{- end }}
              {{- if .Values.filer.disableDirListing }}
              -disableDirListing \
              {{- end }}
              {{- if .Values.filer.maxMB }}
              -maxMB={{ .Values.filer.maxMB }} \
              {{- end }}
              {{- if .Values.filer.encryptVolumeData }}
              -encryptVolumeData \
              {{- end }}
              -ip=${POD_IP} \
              {{- if .Values.filer.filerGroup}}
              -filerGroup={{ .Values.filer.filerGroup}} \
              {{- end }}
              {{- if .Values.filer.s3.enabled }}
              -s3 \
              -s3.port={{ .Values.filer.s3.port }} \
              {{- if .Values.filer.s3.domainName }}
              -s3.domainName={{ .Values.filer.s3.domainName }} \
              {{- end }}
              {{- if .Values.global.enableSecurity }}
              {{- if .Values.filer.s3.httpsPort }}
              -s3.port.https={{ .Values.filer.s3.httpsPort }} \
              {{- end }}
              -s3.cert.file=/usr/local/share/ca-certificates/client/tls.crt \
              -s3.key.file=/usr/local/share/ca-certificates/client/tls.key \
              {{- end }}
              {{- if .Values.filer.s3.allowEmptyFolder }}
              -s3.allowEmptyFolder={{ .Values.filer.s3.allowEmptyFolder }} \
              {{- end }}
              {{- if .Values.filer.s3.enableAuth }}
              -s3.config=/etc/sw/seaweedfs_s3_config \
              {{- end }}
              {{- if .Values.filer.s3.auditLogConfig }}
              -s3.auditLogConfig=/etc/sw/filer_s3_auditLogConfig.json \
              {{- end }}
              {{- end }}
              -master={{ if .Values.global.masterServer }}{{.Values.global.masterServer}}{{ else }}{{ range $index := until (.Values.master.replicas | int) }}${SEAWEEDFS_FULLNAME}-master-{{ $index }}.${SEAWEEDFS_FULLNAME}-master.{{ $.Release.Namespace }}:{{ $.Values.master.port }}{{ if lt $index (sub ($.Values.master.replicas | int) 1) }},{{ end }}{{ end }}{{ end }}
          volumeMounts:
            {{- if (or (eq .Values.filer.logs.type "hostPath") (eq .Values.filer.logs.type "persistentVolumeClaim") (eq .Values.filer.logs.type "emptyDir")) }}
            - name: seaweedfs-filer-log-volume
              mountPath: "/logs/"
            {{- end }}
            {{- if .Values.filer.s3.enableAuth }}
            - name: config-users
              mountPath: /etc/sw
              readOnly: true
            {{- end }}
            {{- if (or .Values.filer.enablePVC (or (eq .Values.filer.data.type "hostPath") (eq .Values.filer.data.type "persistentVolumeClaim") (eq .Values.filer.data.type "emptyDir"))) }}
            - name: data-filer
              mountPath: /data
            {{- end }}
            {{- if .Values.global.enableSecurity }}
            - name: security-config
              readOnly: true
              mountPath: /etc/seaweedfs/security.toml
              subPath: security.toml
            - name: ca-cert
              readOnly: true
              mountPath: /usr/local/share/ca-certificates/ca/
            - name: master-cert
              readOnly: true
              mountPath: /usr/local/share/ca-certificates/master/
            - name: volume-cert
              readOnly: true
              mountPath: /usr/local/share/ca-certificates/volume/
            - name: filer-cert
              readOnly: true
              mountPath: /usr/local/share/ca-certificates/filer/
            - name: client-cert
              readOnly: true
              mountPath: /usr/local/share/ca-certificates/client/
            {{- end }}
            {{ tpl .Values.filer.extraVolumeMounts . | nindent 12 | trim }}
          ports:
            - containerPort: {{ .Values.filer.port }}
              name: swfs-filer
            - containerPort: {{ .Values.filer.metricsPort }}
              name: metrics
            - containerPort: {{ .Values.filer.grpcPort }}
              #name: swfs-filer-grpc
          {{- if .Values.filer.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: {{ .Values.filer.readinessProbe.httpGet.path }}
              port: {{ .Values.filer.port }}
              scheme: {{ .Values.filer.readinessProbe.scheme }}
            initialDelaySeconds: {{ .Values.filer.readinessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.filer.readinessProbe.periodSeconds }}
            successThreshold: {{ .Values.filer.readinessProbe.successThreshold }}
            failureThreshold: {{ .Values.filer.readinessProbe.failureThreshold }}
            timeoutSeconds: {{ .Values.filer.readinessProbe.timeoutSeconds }}
          {{- end }}
          {{- if .Values.filer.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: {{ .Values.filer.livenessProbe.httpGet.path }}
              port: {{ .Values.filer.port }}
              scheme: {{ .Values.filer.livenessProbe.scheme }}
            initialDelaySeconds: {{ .Values.filer.livenessProbe.initialDelaySeconds }}
            periodSeconds: {{ .Values.filer.livenessProbe.periodSeconds }}
            successThreshold: {{ .Values.filer.livenessProbe.successThreshold }}
            failureThreshold: {{ .Values.filer.livenessProbe.failureThreshold }}
            timeoutSeconds: {{ .Values.filer.livenessProbe.timeoutSeconds }}
          {{- end }}
          {{- with .Values.filer.resources }}
          resources:
            {{- toYaml . | nindent 12 }}
          {{- end }}
          {{- if .Values.filer.containerSecurityContext.enabled }}
          securityContext: {{- omit .Values.filer.containerSecurityContext "enabled" | toYaml | nindent 12 }}
          {{- end }}
        {{- if .Values.filer.sidecars }}
        {{- include "common.tplvalues.render" (dict "value" .Values.filer.sidecars "context" $) | nindent 8 }}
        {{- end }}
      volumes:
        {{- if eq .Values.filer.logs.type "hostPath" }}
        - name: seaweedfs-filer-log-volume
          hostPath:
            path: {{ .Values.filer.logs.hostPathPrefix }}/logs/seaweedfs/filer
            type: DirectoryOrCreate
        {{- end }}
        {{- if eq .Values.filer.logs.type "existingClaim" }}
        - name: seaweedfs-filer-log-volume
          persistentVolumeClaim:
            claimName: {{ .Values.filer.logs.claimName }}
        {{- end }}
        {{- if eq .Values.filer.logs.type "emptyDir" }}
        - name: seaweedfs-filer-log-volume
          emptyDir: {}
        {{- end }}
        {{- if eq .Values.filer.data.type "hostPath" }}
        - name: data-filer
          hostPath:
            path: {{ .Values.filer.data.hostPathPrefix }}/filer_store
            type: DirectoryOrCreate
        {{- end }}
        {{- if eq .Values.filer.data.type "existingClaim" }}
        - name: data-filer
          persistentVolumeClaim:
            claimName: {{ .Values.filer.data.claimName }}
        {{- end }}
        {{- if eq .Values.filer.data.type "emptyDir" }}
        - name: data-filer
          emptyDir: {}
        {{- end }}
        - name: db-schema-config-volume
          configMap:
            name: seaweedfs-db-init-config
        {{- if and .Values.filer.s3.enabled .Values.filer.s3.enableAuth }}
        - name: config-users
          secret:
            defaultMode: 420
            {{- if .Values.filer.s3.existingConfigSecret }}
            secretName: {{ .Values.filer.s3.existingConfigSecret }}
            {{- else }}
            secretName: seaweedfs-s3-secret
            {{- end }}
        {{- end }}
        {{- if .Values.global.enableSecurity }}
        - name: security-config
          configMap:
            name: {{ template "seaweedfs.name" . }}-security-config
        - name: ca-cert
          secret:
            secretName: {{ template "seaweedfs.name" . }}-ca-cert
        - name: master-cert
          secret:
            secretName: {{ template "seaweedfs.name" . }}-master-cert
        - name: volume-cert
          secret:
            secretName: {{ template "seaweedfs.name" . }}-volume-cert
        - name: filer-cert
          secret:
            secretName: {{ template "seaweedfs.name" . }}-filer-cert
        - name: client-cert
          secret:
            secretName: {{ template "seaweedfs.name" . }}-client-cert
        {{- end }}
        {{ tpl .Values.filer.extraVolumes . | indent 8 | trim }}
      {{- if .Values.filer.nodeSelector }}
      nodeSelector:
        {{ tpl .Values.filer.nodeSelector . | indent 8 | trim }}
      {{- end }}
  {{- if and (.Values.filer.enablePVC) (eq .Values.filer.data.type "persistentVolumeClaim") }}
  # DEPRECATION: Deprecate in favor of filer.data section below
  volumeClaimTemplates:
  - metadata:
      name: data-filer
    spec:
      accessModes:
      - ReadWriteOnce
      resources:
        requests:
          storage: {{ .Values.filer.storage }}
      {{- if .Values.filer.storageClass }}
      storageClassName: {{ .Values.filer.storageClass }}
      {{- end }}
  {{- end }}
  {{- $pvc_exists := include "filer.pvc_exists" . -}}
  {{- if $pvc_exists }}
  volumeClaimTemplates:
    {{- if eq .Values.filer.data.type "persistentVolumeClaim" }}
    - metadata:
        name: data-filer
        {{- with .Values.filer.data.annotations }}
        annotations:
          {{- toYaml . | nindent 10 }}
        {{- end }}
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: {{ .Values.filer.data.storageClass }}
        resources:
          requests:
            storage: {{ .Values.filer.data.size }}
    {{- end }}
    {{- if eq .Values.filer.logs.type "persistentVolumeClaim" }}
    - metadata:
        name: seaweedfs-filer-log-volume
        {{- with .Values.filer.logs.annotations }}
        annotations:
          {{- toYaml . | nindent 10 }}
        {{- end }}
      spec:
        accessModes: [ "ReadWriteOnce" ]
        storageClassName: {{ .Values.filer.logs.storageClass }}
        resources:
          requests:
            storage: {{ .Values.filer.logs.size }}
    {{- end }}
  {{- end }}
{{- end }}
