---
apiVersion: v1
kind: ConfigMap
metadata:
  name: deleteunreadynodes
  namespace: kube-system
data:
  entrypoint.sh: |-
    #!/bin/bash
    while true; do
      echo "Checking NotReady nodes"
      for node in $(kubectl get nodes -o json | jq '.items[].metadata.name' --raw-output); do
        echo "Checking node $node"

        taint=$(kubectl get nodes $node -o json | jq '.spec | select(.taints!=null) | .taints[] | select(.key=="kubemark") | select (.!=null) | select(.value=="true")' | wc -l)
        if [ $taint -eq 0 ]; then
          echo "Skipping $node, no 'kubemark' taint found"
          continue
        fi

        status=$(kubectl get node $node -o json | jq '.status.conditions[] | select(.type=="Ready") | .status' --raw-output)
        if [ $status != "Unknown" ]; then
          continue
        fi

        # Delete node
        echo "Deleting node $node"
        kubectl delete node $node
      done
      sleep 10s
    done
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: machineapi-kubemark-controllers
  namespace: kube-system
  labels:
    api: machineapi
    k8s-app: kubemark
spec:
  selector:
    matchLabels:
      api: machineapi
      k8s-app: kubemark
  replicas: 1
  template:
    metadata:
      labels:
        api: machineapi
        k8s-app: kubemark
    spec:
      nodeSelector:
        node-role.kubernetes.io/master: ""
      tolerations:
      - effect: NoSchedule
        key: node-role.kubernetes.io/master
      - key: CriticalAddonsOnly
        operator: Exists
      - effect: NoExecute
        key: node.kubernetes.io/not-ready
        operator: Exists
        tolerationSeconds: 120
      - effect: NoExecute
        key: node.kubernetes.io/unreachable
        operator: Exists
        tolerationSeconds: 120
      containers:
      - name: unready-nodes-gb
        image: gofed/kubemark-machine-controllers:v1.0
        command:
          - /bin/entrypoint.sh
        volumeMounts:
        - name: deleteunreadynodes
          mountPath: /bin/entrypoint.sh
          readOnly: true
          subPath: entrypoint.sh
      volumes:
      - name: deleteunreadynodes
        configMap:
          defaultMode: 0700
          name: deleteunreadynodes
