{{ include "common.storage.hostPathValidate" .Values }}
apiVersion: {{ template "common.capabilities.deployment.apiVersion" . }}
kind: Deployment
metadata:
  name: {{ template "common.names.fullname" . }}-storj
  labels:
    app: {{ template "common.names.name" . }}
    chart: {{ template "common.names.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
  annotations:
    rollme: {{ randAlphaNum 5 | quote }}
spec:
  replicas: {{ (default 1 .Values.replicas) }}
  strategy:
    type: "Recreate"
  selector:
    matchLabels:
      app: {{ template "common.names.name" . }}
      release: {{ .Release.Name }}
  template:
    metadata:
      name: {{ template "common.names.fullname" . }}
      labels:
        app: {{ template "common.names.name" . }}
        release: {{ .Release.Name }}
        {{- include "common.labels.selectorLabels" . | nindent 8 }}
      annotations: {{ include "common.annotations" . | nindent 8 }}
    spec:
      terminationGracePeriodSeconds: {{ .Values.terminationGracePeriod | default 30 }}
      initContainers:
      - name: generate-identity
        image: "alpine/curl:latest"
        securityContext:
          runAsUser: 0
          runAsGroup: 0
        command:
          - ./init_script/init_config.sh
        env:
          {{ $envListIdentity := (default list) }}
          {{ $envListIdentity = mustAppend $envListIdentity (dict "name" "DEFAULT_CERT_PATH" "value" (printf "%s/ca.cert" .Values.identityCreationMountPath)) }}
          {{ $envListIdentity = mustAppend $envListIdentity (dict "name" "DEFAULT_IDENTITY_CERT_PATH" "value" (printf "%s/identity.cert" .Values.identityCreationMountPath)) }}
          {{ $envListIdentity = mustAppend $envListIdentity (dict "name" "AUTH_KEY" "valueFromSecret" true "secretName" "storj-credentials" "secretKey" "authToken") }}
          {{ include "common.containers.environmentVariables" (dict "environmentVariables" $envListIdentity) | nindent 12 }}
        volumeMounts:
          - name: initial-scripts
            mountPath: /init_script/
          - name: identity
            mountPath: {{ .Values.identityCreationMountPath }}
      - name: setup
        {{ include "common.containers.imageConfig" .Values.image | nindent 8 }}
        command:
          - /bin/sh
          - -c
          - 'test ! -f {{ .Values.appVolumeMounts.data.mountPath }}/config.yaml && export SETUP="true"; /entrypoint;'
          {{ include "common.storage.allContainerVolumeMounts" .Values | nindent 8 }}
        securityContext:
          runAsUser: {{ .Values.runAsUser }}
          runAsGroup: {{ .Values.runAsGroup }}
      containers:
        - name: {{ .Chart.Name }}
          {{ include "common.containers.imageConfig" .Values.image | nindent 10 }}
          {{ include "common.resources.limitation" . | nindent 10 }}
          volumeMounts: {{ include "common.storage.configureAppVolumeMountsInContainer" .Values | nindent 12 }}
          {{ range $index, $hostPathConfiguration := .Values.extraAppVolumeMounts }}
            - name: extrappvolume-{{ $index }}
              mountPath: {{ $hostPathConfiguration.mountPath }}
          {{ end }}
          securityContext:
            runAsUser: {{ .Values.runAsUser }}
            runAsGroup: {{ .Values.runAsGroup }}
          ports:
            - name: web
              containerPort: 14002
            - name: tcp
              containerPort: 28967
              protocol: TCP
            - name: udp
              containerPort: 28967
              protocol: UDP
          {{- $walletFeats := list -}}
          {{- if .Values.zksync }}
            {{- $walletFeats = mustAppend $walletFeats "zksync" -}}
          {{- end -}}
          {{ if .Values.zksyncEra }}
            {{- $walletFeats = mustAppend $walletFeats "zksync-era" -}}
          {{- end -}}
          {{- if $walletFeats }}
          args:
            - --operator.wallet-features={{ join "," $walletFeats }}
          {{ end }}
          env:
            {{ $envList := (default list .Values.environmentVariables) }}
            {{ $envList = mustAppend $envList (dict "name" "WALLET" "valueFromSecret" true "secretName" "storj-credentials" "secretKey" "wallet") }}
            {{ $envList = mustAppend $envList (dict "name" "ADDRESS" "value" (printf "%s:%d" (.Values.domainAddress) (.Values.nodePort | int))) }}
            {{ $envList = mustAppend $envList (dict "name" "EMAIL" "value" (printf "%s" (.Values.email))) }}
            {{ $envList = mustAppend $envList (dict "name" "STORAGE" "value" (printf "%dGB" (.Values.storageSize | int))) }}
            {{ include "common.containers.environmentVariables" (dict "environmentVariables" $envList) | nindent 12 }}
{{ include "common.networking.dnsConfiguration" .Values | nindent 6 }}
      volumes: {{ include "common.storage.configureAppVolumes" .Values | nindent 8 }}
      {{ range $index, $hostPathConfiguration := .Values.extraAppVolumeMounts }}
        - name: extrappvolume-{{ $index }}
          hostPath:
            path: {{ $hostPathConfiguration.hostPath }}
      {{ end }}
        - name: initial-scripts
          configMap:
            defaultMode: 0755
            name: "initial-scripts"
