apiVersion: v1
kind: Pod
metadata:
  name: fn-{validator_index}-{fullnode_index}
  labels:
    app: libra-fullnode
    libra-node: "true"
    peer_id: fn-{validator_index}-{fullnode_index}
  annotations:
    prometheus.io/should_be_scraped: "true"
spec:
  hostNetwork: true
  dnsPolicy: ClusterFirstWithHostNet
  serviceAccountName: fullnode
  nodeSelector:
    nodeType: validators
  nodeName: "{node_name}"
  initContainers:
  - name: init
    image: 853397791086.dkr.ecr.us-west-2.amazonaws.com/libra_init:master
    volumeMounts:
    - mountPath: /opt/libra/data
      name: data
    securityContext:
      runAsUser: 0 # To get permissions to write to /opt/libra/data
    command:
    - "bash"
    - "-c"
    - |
      set -x;
      if [[ {delete_data} = true ]]; then
        rm -rf /opt/libra/data/*
      fi
      if [[ {fluentbit_enabled} = true ]]; then
        libra_log_file="libra.log"
      else
        libra_log_file="non_existent_file"
      fi
      mkdir -p /opt/libra/data/fluent-bit/
      cat << EOF > /opt/libra/data/fluent-bit/parsers.conf
      [PARSER]
        Name        events
        Format      json
        Time_Key    timestamp
        Time_Format %Y-%m-%d %H:%M:%S
      EOF
      cat << EOF > /opt/libra/data/fluent-bit/fluent-bit.conf
      [SERVICE]
          Flush         5
          Log_Level     info
          Daemon        off
          Parsers_File  /opt/libra/data/fluent-bit/parsers.conf

      [INPUT]
          Name              tail
          Tag               validator
          Path              /opt/libra/data/${{libra_log_file}}
          Mem_Buf_Limit     5MB
          Refresh_Interval  10
          Skip_Long_Lines   On

      [INPUT]
          Name              tail
          Tag               validator-events
          Path              /opt/libra/data/events.log
          Mem_Buf_Limit     5MB
          Refresh_Interval  10
          Skip_Long_Lines   On
          Parser            events

      [FILTER]
          Name record_modifier
          Match *
          Record kubernetes.pod_name fn-{validator_index}-{fullnode_index}

      [OUTPUT]
          Name            es
          Match           *
          Host            elasticsearch-master
          Port            9200
          Logstash_Format On
          Replace_Dots    Off
          Retry_Limit     False
          Logstash_Prefix kubernetes_cluster
      EOF
      CFG_SEED_PEER_IP=$(kubectl get pod/val-{validator_index} -o=jsonpath='{{.status.podIP}}');
      while [ -z "${{CFG_SEED_PEER_IP}}" ]; do
        sleep 5;
        CFG_SEED_PEER_IP=$(kubectl get pod/val-{validator_index} -o=jsonpath='{{.status.podIP}}');
        echo "Waiting for pod/val-{validator_index} IP Address";
      done;
      echo -n "${{CFG_SEED_PEER_IP}}" > /opt/libra/data/seed_peer_ip
  containers:
  - name: fluent-bit
    image: 853397791086.dkr.ecr.us-west-2.amazonaws.com/fluent-bit:1.3.9
    imagePullPolicy: IfNotPresent
    command: ["/fluent-bit/bin/fluent-bit", "-c", "/opt/libra/data/fluent-bit/fluent-bit.conf"]
    volumeMounts:
    - mountPath: /opt/libra/data
      name: data
  - name: main
    image: 853397791086.dkr.ecr.us-west-2.amazonaws.com/libra_validator:{image_tag}
    imagePullPolicy: Always
    resources:
      requests:
        cpu: 7800m
    ports:
    - containerPort: 6180
    - containerPort: 6181
    - containerPort: 8000
    - containerPort: 9101
    - containerPort: 6191
    volumeMounts:
    - mountPath: /opt/libra/data
      name: data
    env:
    - name: CFG_NUM_VALIDATORS
      value: "{num_validators}"
    - name: CFG_NUM_FULLNODES
      value: "{num_fullnodes}"
    - name: CFG_FULLNODE_INDEX
      value: "{fullnode_index}"
    - name: CFG_SEED
      value: "{cfg_seed}"
    - name: CFG_FULLNODE_SEED
      value: "{cfg_fullnode_seed}"
    - name: RUST_LOG
      value: "warn"
    - name: STRUCT_LOG_FILE
      value: "/opt/libra/data/events.log"
    - name: STRUCT_LOG_LEVEL
      value: "debug"
    - name: RUST_BACKTRACE
      value: "1"
    - name: CFG_OVERRIDES
      value: "{cfg_overrides}"
    - name: MY_POD_IP
      valueFrom:
        fieldRef:
          fieldPath: status.podIP
    command:
      - "bash"
      - "-c"
      - |
        set -x;
        export CFG_LISTEN_ADDR=$MY_POD_IP;
        export CFG_SEED_PEER_IP=$(cat /opt/libra/data/seed_peer_ip);
        exec bash /docker-run-dynamic-fullnode.sh &> /opt/libra/data/libra.log
  volumes:
  - name: data
    hostPath:
      path: /data
      type: Directory
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: libra-node
            operator: Exists
        topologyKey: "kubernetes.io/hostname"
  terminationGracePeriodSeconds: 5
  tolerations:
  - key: "validators"
    operator: "Exists"
    effect: "NoSchedule"
  - key: "node.kubernetes.io/not-ready"
    operator: "Exists"
    effect: "NoSchedule"
