# helm_charts_deployments.yaml
{{- $workDir := printf "/work" }}
{{- $refDir := printf "/ref" }}
{{- $keysDir := printf "/keys" }}
{{- range $index, $val := $.Values.validators }}
{{- $nodeNumber := printf "%03d" $index }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: {{ template "burrow.name" $ }}
    chart: {{ template "burrow.chart" $ }}
    heritage: {{ $.Release.Service }}
    release: {{ $.Release.Name }}
    nodeNumber: {{ $nodeNumber | quote }}
  name: {{ template "burrow.fullname" $ }}-{{ $nodeNumber }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ template "burrow.name" $ }}
      release: {{ $.Release.Name }}
      nodeNumber: {{ $nodeNumber | quote }}
  template:
    metadata:
{{- if (or $.Values.podAnnotations $.Values.config.RPC.Metrics.Enabled) }}
      annotations:
{{- if $.Values.config.RPC.Metrics.Enabled }}
        prometheus.io/scrape: "true"
        prometheus.io/port: {{ $.Values.config.RPC.Metrics.ListenPort | quote }}
        prometheus.io/path: {{ $.Values.config.RPC.Metrics.MetricsPath }}
{{- end }}
{{- if $.Values.podAnnotations }}
{{ toYaml $.Values.podAnnotations | indent 8 }}
{{- end }}
{{- end }}
      labels:
        app: {{ template "burrow.name" $ }}
        release: {{ $.Release.Name }}
        nodeNumber: {{ $nodeNumber | quote }}
{{- if $.Values.podLabels }}
{{ toYaml $.Values.podLabels | indent 8 }}
{{- end }}
    spec:
      initContainers:
        - name: init-keys
          image: busybox
          imagePullPolicy: IfNotPresent
          workingDir: {{ $keysDir }}
          volumeMounts:
            - name: keys-dir
              mountPath: {{ $keysDir }}
            - name: work-dir
              mountPath: {{ $workDir }}
          command:
            - 'sh'
            - '-xc'
            - |-
              mkdir -p {{ $workDir }}/.burrow/config && \
              cp node_key.json {{ $workDir }}/.burrow/config/node_key.json && \
              chmod 600 {{ $workDir }}/.burrow/config/node_key.json
{{- if $.Values.chain.restore.enabled }}
        - name: retrieve
          image: appropriate/curl
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
          workingDir: {{ $workDir }}
          command:
          - curl
          args:
          - -o
          - dumpFile
          - {{ $.Values.chain.restore.dumpURL }}
          volumeMounts:
            - name: work-dir
              mountPath: {{ $workDir }}
        - name: restore
          image: "{{ $.Values.image.repository }}:{{ $.Values.image.tag }}"
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
          workingDir: {{ $workDir }}
          command:
          - burrow
          args:
          - restore
          - --config
          - "{{ $refDir }}/burrow.json"
          - --genesis
          - "{{ $refDir }}/genesis.json"
          - --silent
          - dumpFile
          - --address
          - {{ $val.address | quote }}
          - --moniker
          - {{ printf "%s-validator-%s" $.Values.organization $nodeNumber | quote }}
          volumeMounts:
            - mountPath: {{ $workDir }}
              name: work-dir
            - mountPath: {{ $refDir }}
              name: ref-dir
{{- end }}
      containers:
        - name: node
          image: "{{ $.Values.image.repository }}:{{ $.Values.image.tag }}"
          imagePullPolicy: {{ $.Values.image.pullPolicy }}
          workingDir: {{ $workDir }}
          command:
          - burrow
          args:
          - start
          - --config
          - "{{ $refDir }}/burrow.json"
          - --genesis
          - "{{ $refDir }}/genesis.json"
          - --address
          - {{ $val.address | quote }}
          - --moniker
          - {{ printf "%s-validator-%s" $.Values.organization $nodeNumber | quote }}
{{- if (and $.Values.peer.ingress.enabled (not (eq (len $.Values.peer.ingress.hosts) 0))) }}
          - --external-address
          - "{{ $nodeNumber }}.{{ index $.Values.peer.ingress.hosts 0 }}:{{ $.Values.config.Tendermint.ListenPort }}"
{{- end }}
{{- range $key, $value := $.Values.extraArgs }}
            - --{{ $key }}={{ $value }}
{{- end }}
          env:
{{- include "settings" $ | indent 10 }}
          volumeMounts:
            - name: ref-dir
              mountPath: {{ $refDir }}
            - name: work-dir
              mountPath: {{ $workDir }}
            - name: keys-dir
              mountPath: {{ $keysDir }}/data
            - name: keys-dir-names
              mountPath: {{ $keysDir }}/names
          ports:
            - name: peer
              protocol: TCP
              containerPort: {{ $.Values.config.Tendermint.ListenPort }}
{{- if $.Values.config.RPC.GRPC.Enabled }}
            - name: grpc
              protocol: TCP
              containerPort: {{ $.Values.config.RPC.GRPC.ListenPort }}
{{- end }}
{{- if $.Values.config.RPC.Info.Enabled }}
            - name: info
              protocol: TCP
              containerPort: {{ $.Values.config.RPC.Info.ListenPort }}
{{- end }}
{{- if $.Values.config.RPC.Metrics.Enabled }}
            - name: metrics
              protocol: TCP
              containerPort: {{ $.Values.config.RPC.Metrics.ListenPort }}
{{- end }}
{{- if not $.Values.chain.testing }}
{{- if $.Values.livenessProbe.enabled }}
          livenessProbe:
            httpGet:
              path: {{ $.Values.livenessProbe.path }}
              port: info
              scheme: HTTP
            initialDelaySeconds: {{ $.Values.livenessProbe.initialDelaySeconds }}
            timeoutSeconds: {{ $.Values.livenessProbe.timeoutSeconds }}
            periodSeconds: {{ $.Values.livenessProbe.periodSeconds }}
{{- end }}
{{- if $.Values.readinessProbe.enabled }}
          readinessProbe:
            httpGet:
              path: {{ $.Values.readinessProbe.path }}
              port: info
              scheme: HTTP
            initialDelaySeconds: {{ $.Values.readinessProbe.initialDelaySeconds }}
{{- end }}
{{- end }}
{{- if $.Values.resources }}
          resources:
{{ toYaml $.Values.resources | indent 12 }}
{{- end }}
      restartPolicy: Always
      volumes:
        - name: ref-dir
          projected:
            sources:
              - configMap:
                  name: {{ template "burrow.fullname" $ }}-config
              - configMap:
                  name: {{ template "burrow.fullname" $ }}-genesis
        - name: keys-dir
          projected:
            sources:
              - secret:
                  name: {{ template "burrow.fullname" $ }}-keys-{{ $nodeNumber }}
        - name: keys-dir-names
          emptyDir: {}
        - name: work-dir
{{- if $.Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ template "burrow.fullname" $ }}-{{ $nodeNumber }}
{{- else }}
          emptyDir: {}
{{- end }}
      securityContext:
        fsGroup: 101
        runAsUser: 1000
{{- if $.Values.affinity }}
      affinity:
{{ toYaml $.Values.affinity | indent 8 }}
{{- end }}
{{- if $.Values.nodeSelector }}
      nodeSelector:
{{ toYaml $.Values.nodeSelector | indent 8 }}
{{- end }}
{{- if $.Values.tolerations }}
      tolerations:
{{ toYaml $.Values.tolerations | indent 8 }}
{{- end }}
{{- end }}