---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: eth-geth-miner
  labels:
    app: eth-geth-miner
spec:
  replicas: 3
  revisionHistoryLimit: 1
  selector:
    matchLabels:
      app: eth-geth-miner
  template:
    metadata:
      labels:
        app: eth-geth-miner
    spec:
      containers:
        - name: geth-miner
          image: ethereum/client-go:release-1.8
          imagePullPolicy: IfNotPresent
          resources:
            limits:
              cpu: "1"
            requests:
              cpu: "0.5"
          command: ["/bin/sh"]
          args:
            - -c
            - |
              geth \
              --bootnodes=$(cat /root/.ethereum/bootnodes) \
              --mine \
              --etherbase=0 \
              --networkid=$NETWORK_ID \
              --ethstats=$HOSTNAME:$ETHSTATS_SECRET@$ETHSTATS_SVC \
              --verbosity=5
          env:
            - name: ETHSTATS_SVC
              value: eth-ethstats.default.svc.cluster.local
            - name: ETHSTATS_SECRET
              valueFrom:
                secretKeyRef:
                  name: eth-ethstats
                  key: WS_SECRET
            - name: NETWORK_ID
              valueFrom:
                configMapKeyRef:
                  name: eth-geth
                  key: networkid
          ports:
            - name: discovery-udp
              containerPort: 30303
              protocol: UDP
            - name: discovery-tcp
              containerPort: 30303
          volumeMounts:
            - name: data
              mountPath: /root/.ethereum
      initContainers:
        - name: init-genesis
          image: ethereum/client-go:release-1.8
          imagePullPolicy: IfNotPresent
          args:
            - "init"
            - "/var/geth/genesis.json"
          volumeMounts:
            - name: data
              mountPath: /root/.ethereum
            - name: config
              mountPath: /var/geth
        - name: create-account
          image: ethereum/client-go:release-1.8
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh"]
          args:
            - -c
            - |
              printf '${ACCOUNT_SECRET}\n${ACCOUNT_SECRET}\n' | geth account new
          env:
            - name: ACCOUNT_SECRET
              valueFrom:
                secretKeyRef:
                  name: eth-geth-miner
                  key: accountsecret
          volumeMounts:
            - name: data
              mountPath: /root/.ethereum
        - name: get-bootnodes
          image: ethereum/client-go:release-1.8
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh"]
          args:
            - -c
            - |
              apk add --no-cache curl
              COUNT=0
              echo "retreiving bootnodes from $BOOTNODE_REGISTRAR_SVC"
              while [ $COUNT -le 90 ]
              do
                curl -m 5 -s $BOOTNODE_REGISTRAR_SVC | xargs echo -n >> /geth/bootnodes
                if [ -s /geth/bootnodes ]
                then
                  cat /geth/bootnodes
                  exit 0
                fi
                echo "no bootnodes found. retrying $COUNT..."
                sleep 2 || break
                COUNT=$((COUNT+1))
              done
              echo "WARNING. unable to find bootnodes. continuing but geth may not be able to find any peers."
              exit 0
          env:
            - name: BOOTNODE_REGISTRAR_SVC
              value: eth-bootnode-registrar.default.svc.cluster.local
          volumeMounts:
            - name: data
              mountPath: /geth
      volumes:
        - name: data
          emptyDir: {}
        - name: config
          configMap:
            name: eth-geth
