apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ include "hlf-ord.fullname" . }}
  labels:
{{ include "labels.standard" . | indent 4 }}
spec:
  replicas: 1
  selector:
    matchLabels:
      app: {{ include "hlf-ord.name" . }}
      release: {{ .Release.Name }}
  # Ensure we allow our pod to be unavailable, so we can upgrade
  strategy:
    rollingUpdate:
      maxUnavailable: 1
  template:
    metadata:
      labels:
{{ include "labels.standard" . | indent 8 }}
    spec:
      volumes:
        - name: data
        {{- if .Values.persistence.enabled }}
          persistentVolumeClaim:
            claimName: {{ .Values.persistence.existingClaim | default (include "hlf-ord.fullname" .) }}
        {{- else }}
          emptyDir: {}
        {{- end }}
        {{- if .Values.secrets.ord.cert }}
        - name: id-cert
          secret:
            secretName: {{ .Values.secrets.ord.cert }}
        {{- end }}
        {{- if .Values.secrets.ord.key }}
        - name: id-key
          secret:
            secretName: {{ .Values.secrets.ord.key }}
        {{- end }}
        {{- if .Values.secrets.ord.caCert }}
        - name: cacert
          secret:
            secretName: {{ .Values.secrets.ord.caCert }}
        {{- end }}
        {{- if .Values.secrets.ord.intCaCert }}
        - name: intcacert
          secret:
            secretName: {{ .Values.secrets.ord.intCaCert }}
        {{- end }}
        {{- if .Values.secrets.ord.tls }}
        - name: tls
          secret:
            secretName: {{ .Values.secrets.ord.tls }}
        {{- end }}
        {{- if .Values.secrets.ord.tlsRootCert }}
        - name: tls-rootcert
          secret:
            secretName: {{ .Values.secrets.ord.tlsRootCert }}
        {{- end }}
        {{- if .Values.secrets.ord.tlsClientRootCert }}
        - name: tls-clientrootcert
          secret:
            secretName: {{ .Values.secrets.ord.tlsClientRootCert }}
        {{- end }}
        {{- if .Values.secrets.genesis }}
        - name: genesis
          secret:
            secretName: {{ .Values.secrets.genesis }}
        {{- end }}
        {{- if .Values.secrets.adminCert }}
        - name: admin-cert
          secret:
            secretName: {{ .Values.secrets.adminCert }}
        {{- end }}
        {{- if .Values.secrets.caServerTls }}
        - name: ca-server-tls
          secret:
            secretName: {{ .Values.secrets.caServerTls }}
        {{- end }}
      containers:
        - name: orderer
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          ports:
            - name: ord-port
              containerPort: 7050
              protocol: TCP
          livenessProbe:
            exec:
              command:
              - ls
              - /var/hyperledger
            initialDelaySeconds: 5
            periodSeconds: 5
          # TODO: Improve readiness probe (ideally `ps aux | awk '$11=="orderer"'`)
          readinessProbe:
            exec:
              command:
              - ls
              - /var/hyperledger
            initialDelaySeconds: 15
          command:
            - bash
            - -c
            - |

              while [ ! -d /var/hyperledger/admin_msp/admincerts ] || [ -z "$(ls -A /var/hyperledger/admin_msp/admincerts)" ];
              do
                echo "\033[0;31m /var/hyperledger/admin_msp/admincerts must contain Ord admin certificates files \033[0m"
                sleep 60
              done

              while [ ! -d /hl_config/genesis ] || [ -z "$(ls -A /hl_config/genesis)" ];
              do
                echo "\033[0;31m /hl_config/genesis must contain Genesis transaction \033[0m"
                sleep 60
              done

              while [ ! -d ${ORDERER_GENERAL_LOCALMSPDIR}/signcerts ];
              do
                echo "\033[0;31m ${ORDERER_GENERAL_LOCALMSPDIR}/signcerts directory must exist \033[0m"
                sleep 60
              done

              echo "\033[0;32m Create ${ORDERER_FILELEDGER_LOCATION} directory to avoid restarts \033[0m"
              mkdir -p ${ORDERER_FILELEDGER_LOCATION}
              mkdir -p ${ORDERER_FILELEDGER_LOCATION}/index

              if [ $ORDERER_GENERAL_TLS_CLIENTAUTHREQUIRED ]
              then
                export ORDERER_GENERAL_TLS_CLIENTROOTCAS=$(echo $(ls $ORDERER_GENERAL_TLS_CLIENTROOTCAS) | echo "[$(sed 's/ /,/g')]")
              fi

              echo ">\033[0;35m orderer \033[0m"
              orderer
          envFrom:
            {{- if .Values.secrets.ord.cred }}
            - secretRef:
                # Environmental variables CA_USERNAME and CA_PASSWORD
                name: {{ .Values.secrets.ord.cred }}
            {{- end }}
            - configMapRef:
                name: {{ include "hlf-ord.fullname" . }}--ord
          volumeMounts:
            - mountPath: /var/hyperledger
              name: data
            {{- if .Values.secrets.ord.cert }}
            - mountPath: /var/hyperledger/msp/signcerts
              name: id-cert
            {{- end }}
            {{- if .Values.secrets.ord.key }}
            - mountPath: /var/hyperledger/msp/keystore
              name: id-key
            {{- end }}
            {{- if .Values.secrets.ord.caCert }}
            - mountPath: /var/hyperledger/msp/cacerts
              name: cacert
            - mountPath: /var/hyperledger/admin_msp/cacerts
              name: cacert
            {{- end }}
            {{- if .Values.secrets.ord.intCaCert }}
            - mountPath: /var/hyperledger/msp/intermediatecerts
              name: intcacert
            - mountPath: /var/hyperledger/admin_msp/intermediatecerts
              name: intcacert
            {{- end }}
            {{- if .Values.secrets.ord.tls }}
            - mountPath: /var/hyperledger/tls/server/pair
              name: tls
            {{- end }}
            {{- if .Values.secrets.ord.tlsRootCert }}
            - mountPath: /var/hyperledger/tls/server/cert
              name: tls-rootcert
            {{- end }}
            {{- if .Values.secrets.ord.tlsClientRootCert }}
            - mountPath: /var/hyperledger/tls/client/cert
              name: tls-clientrootcert
            {{- end }}
            {{- if .Values.secrets.genesis }}
            - mountPath: /hl_config/genesis
              name: genesis
            {{- end }}
            {{- if .Values.secrets.adminCert }}
            - mountPath: /var/hyperledger/admin_msp/admincerts
              name: admin-cert
            - mountPath: /var/hyperledger/admin_msp/signcerts
              name: admin-cert
            - mountPath: /var/hyperledger/msp/admincerts
              name: admin-cert
            {{- end }}
          resources:
{{ toYaml .Values.resources | indent 12 }}
    {{- with .Values.nodeSelector }}
      nodeSelector:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.affinity }}
      affinity:
{{ toYaml . | indent 8 }}
    {{- end }}
    {{- with .Values.tolerations }}
      tolerations:
{{ toYaml . | indent 8 }}
    {{- end }}
