apiVersion: apps/v1
kind: Deployment
metadata:
  name: coordinator
  labels:
    app: presto
    presto: coordinator
spec:
  replicas: 1
  selector:
    matchLabels:
      app: presto
  template:
    metadata:
      labels:
        app: presto
        presto: coordinator
    spec:
      securityContext:
        runAsNonRoot: true
      serviceAccount: presto
      initContainers:
      - name: setup-coordinator-properties
        image: quay.io/tflannag/presto:334
        imagePullPolicy: IfNotPresent
        command:
        - /bin/bash
        - -c
        - |
          #! /bin/bash
          
          set -eou pipefail

          echo "Copying ConfigMap volume contents into an empty directory"
          cp -v -L -r -f /presto-etc/* /opt/presto/presto-server/etc/

          # add node id to node config
          NODE_CONFIG=/opt/presto/presto-server/etc/node.properties
          # ensure there's a newline between the last item in the config and what we add
          echo "" >> $NODE_CONFIG
          if ! grep -q -F 'node.id' "$NODE_CONFIG"; then
            NODE_ID="node.id=$MY_NODE_ID"
            echo "Adding $NODE_ID to $NODE_CONFIG"
            echo "$NODE_ID" >> "$NODE_CONFIG"
          fi
        env:
        - name: MY_NODE_ID
          valueFrom:
            fieldRef:
              fieldPath: metadata.uid
        volumeMounts:
        - name: presto-etc
          mountPath: /opt/presto/presto-server/etc
        - name: config-properties
          mountPath: /presto-etc
      containers:
      - name: coordinator
        image: quay.io/tflannag/presto:334
        imagePullPolicy: IfNotPresent
        command:
        - /bin/bash
        - -c
        - |
          #! /bin/bash
          set -euo pipefail

          function importCert() {
            PEM_FILE=$1
            PASSWORD=$2
            KEYSTORE=$3
            # number of certs in the PEM file
            CERTS=$(grep 'END CERTIFICATE' $PEM_FILE| wc -l)

            # For every cert in the PEM file, extract it and import into the JKS keystore
            # awk command: step 1, if line is in the desired cert, print the line
            #              step 2, increment counter when last line of cert is found
            for N in $(seq 0 $(($CERTS - 1))); do
              ALIAS="${PEM_FILE%.*}-$N"
              cat $PEM_FILE |
                awk "n==$N { print }; /END CERTIFICATE/ { n++ }" |
                keytool -noprompt -import -trustcacerts \
                  -alias $ALIAS -keystore $KEYSTORE -storepass $PASSWORD
            done
          }

          # Add the mounted ConfigMap containing the Service serving CA bundle
          # if that bundle exists. Note: in 4.5+ there's no controller responsible
          # for injecting this CA bundle for us, so we need to manually do the creation
          # of the ConfigMap and mounting of that ConfigMap ourselves.
          #
          # TODO: We should probably require this to be added in either case if we don't
          # care about backwards compatibility.
          if [ -a /var/run/configmaps/service-ca-bundle/service-ca.crt ]; then
            echo "Adding /var/run/configmaps/service-ca-bundle/service-ca.crt to $JAVA_HOME/lib/security/cacerts"
            importCert /var/run/configmaps/service-ca-bundle/service-ca.crt changeit $JAVA_HOME/lib/security/cacerts
          fi

          /opt/presto/presto-server/bin/launcher run
        ports:
        - name: api
          containerPort: 8080
        - name: metrics
          containerPort: 8082
        resources:
          requests:
            cpu: 1
            memory: 1Gi
        volumeMounts:
        - name: presto-service-ca-bundle
          mountPath: /var/run/configmaps/service-ca-bundle
          readOnly: true
        - name: presto-etc
          mountPath: /opt/presto/presto-server/etc
        - name: catalog-properties
          mountPath: /opt/presto/presto-server/etc/catalog
        - name: presto-data
          mountPath: /var/presto/data
        - name: presto-logs
          mountPath: /var/presto/logs
        - name: jmx-properties
          mountPath: /opt/jmx_exporter/config
        - name: metastore
          mountPath: /var/hive/metastore
      volumes:
      - name: config-properties
        configMap:
          name: config
      - name: catalog-properties
        configMap:
          name: catalog  
      - name: jmx-properties
        configMap:
          name: jmx
      - name: presto-service-ca-bundle
        configMap:
          name: coordinator-trusted-ca-bundle
          optional: true
      - name: presto-data
        emptyDir: {}
      - name: presto-logs
        emptyDir: {}
      - name: metastore
        emptyDir: {}
      - name: presto-etc
        emptyDir: {}
