Skip to content

Instantly share code, notes, and snippets.

@nmarchini
Forked from viggin543/measure_pod_start.sh
Last active October 26, 2023 19:24
Show Gist options
  • Save nmarchini/e254c3731f451721ee560d8bfa1baee6 to your computer and use it in GitHub Desktop.
Save nmarchini/e254c3731f451721ee560d8bfa1baee6 to your computer and use it in GitHub Desktop.
a script that outputs start time of a scheduled pod and the time taken to become ready
#!/usr/bin/env bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <namespace>"
exit 1
fi
namespace="$1"
# Get the list of pod names in the specified namespace
pod_names=($(kubectl get pods -n "$namespace" -o=jsonpath='{.items[*].metadata.name}'))
echo "Pods in $1 namespace"
# Initialize arrays to store data
pod_data=()
scheduled_data=()
launch_data=()
# Add column headings
pod_data+=("Pod Name")
scheduled_data+=("Scheduled Time")
launch_data+=("Launch Time (Seconds)")
for pod in "${pod_names[@]}"; do
scheduled=$(kubectl get pod "$pod" -n "$namespace" -o json | jq -r '.status.conditions[] | select(.type=="PodScheduled") | .lastTransitionTime')
ready=$(kubectl get pod "$pod" -n "$namespace" -o json | jq -r '.status.conditions[] | select(.type=="Ready") | .lastTransitionTime')
scheduled_epoch=$(date -j -u -f "%Y-%m-%dT%H:%M:%SZ" "$scheduled" "+%s" 2>/dev/null)
ready_epoch=$(date -j -u -f "%Y-%m-%dT%H:%M:%SZ" "$ready" "+%s" 2>/dev/null)
if [ -z "$scheduled_epoch" ] || [ -z "$ready_epoch" ]; then
echo "Error: Failed to parse timestamps for $pod."
continue
fi
scheduled_time=$(date -j -r "$scheduled_epoch" "+%H:%M:%S")
load_seconds=$((ready_epoch - scheduled_epoch))
# Print data
echo "${pod} scheduled at ${scheduled_time}"
echo "${pod} launch time: ${load_seconds} seconds"
echo
done
@nmarchini
Copy link
Author

nmarchini commented Oct 26, 2023

To run this do the following

./measure_pod_start.sh <namespace>

e.g
./measure_pod_start.sh grafana

To make executable run
chmod +x measure_pod_start.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment