apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}
  labels: {{ include "common.labels" . | nindent 4 }}
spec:
  strategy:
    type: {{ .Values.updateStrategy }}
  selector:
    matchLabels: {{ include "common.labels.selectorLabels" . | nindent 6 }}
  template:
    metadata:
      name: {{ template "common.names.fullname" . }}
      labels: {{ include "common.labels.selectorLabels" . | nindent 8 }}
    spec:
      serviceAccountName: {{ include "common.names.serviceAccountName" . | quote }}
      initContainers:
        - name: init-init
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          command: ['/bin/sh', '-c', '[ ! -e /data/ipfs/config ] && (/usr/local/bin/ipfs init ; chown -R 1000:100 /data/ipfs) ; exit 0']
          {{ include "common.storage.allContainerVolumeMounts" .Values | nindent 10 }}
        - name: init-api
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          command: ['/usr/local/bin/ipfs', 'config', 'Addresses.API', "/ip4/0.0.0.0/tcp/9501"]
          {{ include "common.storage.allContainerVolumeMounts" .Values | nindent 10 }}
        - name: init-gateway
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          command: ['/usr/local/bin/ipfs', 'config', 'Addresses.Gateway', "/ip4/0.0.0.0/tcp/9080"]
          {{ include "common.storage.allContainerVolumeMounts" .Values | nindent 10 }}
        - name: init-swarm
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          command: ['/usr/local/bin/ipfs', 'config', '--json', 'Addresses.Swarm', "[\"/ip4/0.0.0.0/tcp/9401\",\"/ip4/0.0.0.0/tcp/9401/quic\"]" ]
          {{ include "common.storage.allContainerVolumeMounts" .Values | nindent 10 }}
        - name: init-access-origin
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          command: ['/usr/local/bin/ipfs', 'config', '--json', 'API.HTTPHeaders.Access-Control-Allow-Origin', "[\"*\"]" ]
          {{ include "common.storage.allContainerVolumeMounts" .Values | nindent 10 }}
        - name: init-access-methods
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          command: ['/usr/local/bin/ipfs', 'config', '--json', 'API.HTTPHeaders.Access-Control-Allow-Methods', "[\"PUT\",\"POST\"]" ]
          {{ include "common.storage.allContainerVolumeMounts" .Values | nindent 10 }}
        - name: init-chown
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          command: ['chown', '1000:100', '/data/ipfs/config']
          {{ include "common.storage.allContainerVolumeMounts" .Values | nindent 10 }}
      containers:
        - name: {{ .Chart.Name }}
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          {{ include "common.storage.allContainerVolumeMounts" .Values | nindent 10 }}
          ports:
            - name: swarm
              containerPort: 9401
            - name: api
              containerPort: 9501
            - name: gateway
              containerPort: 9880
{{ include "common.containers.allEnvironmentVariables" .Values | nindent 10 }}
{{ include "common.networking.dnsConfiguration" .Values | nindent 6 }}
{{ include "common.storage.allAppVolumes" .Values | nindent 6 }}
