apiVersion: v1
kind: ConfigMap
metadata:
  name: data
  namespace: csr-auto-approver
data:
  nodes: |-
    cnv-node0.alice.lab.uc2.io
    cnv-node1.alice.lab.uc2.io
    cnv-node2.alice.lab.uc2.io
    infra-node0.alice.lab.uc2.io
    infra-node1.alice.lab.uc2.io
    infra-node2.alice.lab.uc2.io
    ocs-node0.alice.lab.uc2.io
    ocs-node1.alice.lab.uc2.io
    ocs-node2.alice.lab.uc2.io
    worker0.alice.lab.uc2.io
    worker1.alice.lab.uc2.io
    worker2.alice.lab.uc2.io
  approver.sh: |-
    #!/bin/bash
    EXPECTED_NODES=`cat /data/nodes`

    echo "Approving CSRs for the following nodes:"
    echo $EXPECTED_NODES

    while [ true ] ;
    do
        PENDING_CSRS=`oc get csr | grep Pending | awk '{ print $1 }'`

        for CSR in $PENDING_CSRS
        do
            NAME=`oc describe csr $CSR | grep "Common Name" | sed 's/.*system:node://'`

            echo "Checking if $NAME is part of EXPECTED_NODES"

            for NODE in $EXPECTED_NODES
            do
                if [ "$NAME" = "$NODE" ] ; then
                    echo "$CSR $NAME == $NODE"
                    oc adm certificate approve $CSR
                fi
            done
        done

        echo "Sleeping for 30 seconds..."
        sleep 30
    done
