#!/bin/bash

if [ $# -ne 1 ]; then
  echo "Incorrect number of arguments"
fi

NODE=$1
DIR=$2

echo "Resetting $DIR on ${NODE}"

NODEIP=$(kubectl get nodes -o wide | grep ${NODE} | grep -v "INTERNAL-IP" | awk '{print $6}')

echo "Node IP: $NODEIP"

echo "Creating ceph-wipe pod to clear $DIR on $NODE (${NODEIP})"
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: ceph-wipe-${NODE}
spec:
  restartPolicy: Never
  nodeName: ${NODE}
  containers:
  - name: ceph-wipe-${NODE}
    image: busybox
    securityContext:
      privileged: true
    command: ["/bin/sh", "-c", "rm -rf ${DIR}"]
EOF

kubectl wait --timeout=900s --for=jsonpath='{.status.phase}=Succeeded' pod ceph-wipe-${NODE}
kubectl delete pod ceph-wipe-${NODE}

