Self disposable network troubleshooting pod

This script provides a quick way to launch a temporary network troubleshooting pod in Kubernetes, run commands interactively, and then clean it up automatically (pod is deleted when you exit the shell). Good for debugging network issues (e.g., DNS, connectivity, HTTP requests) and running ad-hoc commands in a disposable environment.

I have this aliased as 'nt'

#!/bin/bash

# Define the name of the pod
POD_NAME="nettools"

# Check if the pod exists
if kubectl get pod "$POD_NAME" -n default &> /dev/null; then
  echo "Pod $POD_NAME exists. Executing command..."
  kubectl exec -it "$POD_NAME" -n default -- /bin/bash
else
  # Create the pod
  echo "Creating pod $POD_NAME..."
  kubectl run $POD_NAME -n default --image=wbitt/network-multitool:latest --restart=Never --overrides='{"spec": {"terminationGracePeriodSeconds": 2}}' -- sleep infinity

  echo "Waiting for pod $POD_NAME to be in the 'Running' state..."
  while [[ $(kubectl get pod $POD_NAME -n default -o jsonpath='{.status.phase}') != "Running" ]]; do
      sleep 1
  done

  # Shell into the pod
  echo "Shelling into the pod $POD_NAME..."
  kubectl exec -it $POD_NAME -n default -- /bin/bash

  # Delete the pod after exiting the shell
  echo "Deleting pod $POD_NAME..."
  kubectl delete pod $POD_NAME -n default --grace-period=0 
fi