# _helpers.tpl
{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "ipfs.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "ipfs.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a name for the service to use.

We allow overrides for name of service, since this service provides an API that could be directly
called by end-users. For example, you could call it 'ipfs' and then all users running in a namespace
could just connect to it by specifying 'ipfs'.
*/}}
{{- define "ipfs.servicename" -}}
{{- if .Values.service.nameOverride -}}
{{- .Values.service.nameOverride -}}
{{- else -}}
{{- template "ipfs.fullname" . }}
{{- end -}}
{{- end -}}
 # ingress-api.yaml
{{- if .Values.ingressApi.enabled -}}
{{- $serviceName := include "ipfs.fullname" . -}}
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  labels:
    app: {{ template "ipfs.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "ipfs.fullname" . }}-api
  annotations:
          {{- range $key, $value := .Values.ingressApi.annotations }}
          {{ $key }}: {{ $value | quote }}
        {{- end }}
spec:
  rules:
          {{- range .Values.ingressApi.hosts }}
          {{- $url := splitList "/" . }}
    - host: {{ first $url | quote }}
      http:
        paths:
          - path: /{{ rest $url | join "/" }}
            backend:
              serviceName: {{ $serviceName }}
              servicePort: 5001
        {{- end -}}
        {{- if .Values.ingressApi.tls }}
  tls:
{{ toYaml .Values.ingressApi.tls | indent 4 }}
        {{- end -}}
        {{- end -}} # ingress-gateway.yaml
{{- if .Values.ingressGateway.enabled -}}
{{- $serviceName := include "ipfs.fullname" . -}}
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  labels:
    app: {{ template "ipfs.name" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  name: {{ template "ipfs.fullname" . }}-gateway
  annotations:
          {{- range $key, $value := .Values.ingressGateway.annotations }}
          {{ $key }}: {{ $value | quote }}
        {{- end }}
spec:
  rules:
          {{- range .Values.ingressGateway.hosts }}
          {{- $url := splitList "/" . }}
    - host: {{ first $url | quote }}
      http:
        paths:
          - path: /{{ rest $url | join "/" }}
            backend:
              serviceName: {{ $serviceName }}
              servicePort: 8080
        {{- end -}}
        {{- if .Values.ingressGateway.tls }}
  tls:
{{ toYaml .Values.ingressGateway.tls | indent 4 }}
        {{- end -}}
        {{- end -}} # service.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: {{ template "ipfs.servicename" . }}
  labels:
    app: {{ template "ipfs.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
spec:
  type: {{ .Values.service.type }}
  ports:
  - port: 5001
    targetPort: 5001
    protocol: TCP
    name: api
  - port: 8080
    targetPort: 8080
    name: gateway
  selector:
    app: {{ template "ipfs.name" . }}
    release: {{ .Release.Name }}

{{- if .Values.swarm.enabled }}
---
apiVersion: v1
kind: Service
metadata:
  name: {{ template "ipfs.servicename" . }}-swarm
  labels:
    app: {{ template "ipfs.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
  annotations:
    prometheus.io/scrape: "true"
    prometheus.io/port: "5001"
    prometheus.io/path: /debug/metrics/prometheus
spec:
  type: {{ .Values.swarm.type }}
  {{- if .Values.swarm.loadBalancerIP }}
  loadBalancerIP: {{ .Values.swarm.loadBalancerIP }}
  {{- end }}
  {{- if .Values.swarm.externalIP }}
  externalIPs:
    - {{ .Values.swarm.externalIP }}
  {{- end }}
  ports:
  - port: 4001
    {{- if (and (eq .Values.swarm.type "NodePort") (not (empty .Values.swarm.nodePort)))}}
    nodePort: {{ .Values.swarm.nodePort }}
    {{- end }}
    targetPort: 4001
    protocol: TCP
    name: swarm
  selector:
    app: {{ template "ipfs.name" . }}
    release: {{ .Release.Name }}
{{- end -}} # statefulset.yaml
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 }}

replicaCount: 1

image: ipfs/go-ipfs:v0.4.22

service:
  type: ClusterIP

ingressApi:
  enabled: false
    # hosts:
    # - ipfs-api.localhost.localdomain
    # - localhost.localdomain/ipfs-api
    # annotations:
    #   kubernetes.io/ingress.class: nginx
    #   kubernetes.io/tls-acme: "true"
    # tls:
    # - secretName: chart-example-tls
    #   hosts:
    #     - chart-example.local

ingressGateway:
  enabled: false
    # hosts:
    # - ipfs-gateway.localhost.localdomain
    # - localhost.localdomain/ipfs-gateway
    # annotations:
    #   kubernetes.io/ingress.class: nginx
    #   kubernetes.io/tls-acme: "true"
    # tls:
    # - secretName: chart-example-tls
    #   hosts:
    #     - chart-example.local

swarm:
  enabled: false
  type: ClusterIP
  # loadBalancerIP: ""
  # externalIP: ""
  ## Further config for service of type NodePort
  ## Default config with empty string "" will assign a dynamic nodePort
  nodePort: ""
  ## If static nodePort configuration is required it can be enabled as below
  ## Configure ports in allowable range (eg. 30000 - 32767 on minikube)
  # nodePort: 34001

resources: {}

persistence:
  enabled: true
  ## ipfs data Persistent Volume Storage Class
  ## If defined, storageClassName: <storageClass>
  ## If set to "-", storageClassName: "", which disables dynamic provisioning
  ## If undefined (the default) or set to null, no storageClassName spec is
  ##   set, choosing the default provisioner.  (gp2 on AWS, standard on
  ##   GKE, AWS & OpenStack)
  ##
  # storageClass: "-"
  size: "8Gi"
  accessModes:
    - ReadWriteOnce
