#!/bin/bash

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

NODE=$1

echo "Resetting ${NODE}"

NODEIP=$(kubectl get nodes -o wide | grep ${NODE} | grep -v "INTERNAL-IP" | awk '{print $6}')
#DISKS=$(talosctl disks -n ${NODEIP} | grep -v DEV | grep -v usb | grep -v "2.0 TB" | grep -v "/dev/mmc" | awk '{print $2}')
DISKS=$(talosctl disks -n ${NODEIP} | grep -v DEV | grep -v '*$' | awk '{print $2}')

talosctl disks -n ${NODEIP} | grep -v DEV | grep -v '*$'

echo "Disks: $DISKS"
echo "Node IP: $NODEIP"

for d in $DISKS ; do

echo "Creating disk-wipe pod to clear $d on $NODE (${NODEIP})"
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: disk-wipe
spec:
  restartPolicy: Never
  nodeName: ${NODE}
  containers:
  - name: disk-wipe
    image: busybox
    securityContext:
      privileged: true
    command: ["/bin/sh", "-c", "dd if=/dev/zero bs=1M count=100 oflag=direct of=${d}"]
EOF

kubectl wait --timeout=900s --for=jsonpath='{.status.phase}=Succeeded' pod disk-wipe

kubectl delete pod disk-wipe

done

#talosctl -n ${NODEIP} reboot
