-
Notifications
You must be signed in to change notification settings - Fork 0
/
kubectl-pods
56 lines (46 loc) · 2.92 KB
/
kubectl-pods
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash # Function to show usage
usage() {
echo "Usage: $0 {logs|describe}"
exit 1
} # Check for correct number of arguments
if [ "$#" -ne 1 ]; then
usage
fi command=$1
# Validate command argument
if [[ "$command" != "logs" && "$command" != "describe" ]]; then
usage
fi
# Get the current namespace
current_namespace=$(kubectl config view --minify --output 'jsonpath={..namespace}')
if [ -z "$current_namespace" ]; then
current_namespace="default"
fi
# Print the current namespace
echo "Current namespace: $current_namespace"
echo
# List pods with their statuses, node names, and pod IPs
pod_list=$(kubectl get pods -o wide | awk 'NR==1 {print $1,$2,$3,$4,$5,$7} NR>1 {print $1,$2,$3,$4,$5, $7}' | column -t|sort)
#$(kubectl get pods -n $current_namespace -o custom-columns="NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName,IP:.status.podIP" --no-headers | sort|sed 's/Failed/Evicted/')
# Format pod list for fzf and add serial numbers
formatted_pod_list=$(echo "$pod_list" | nl -w 2 -s '. ')
# Count the number of pods
pod_count=$(echo "$formatted_pod_list" | wc -l)
# Set fzf height dynamically based on pod count, with a maximum height of 40
fzf_height=$((pod_count < 40 ? 40 : pod_count))
# Use fzf for selection with dynamic height
selected_item=$(echo "$formatted_pod_list" | fzf --height ${fzf_height}% --layout=reverse --prompt="Select a pod: ")
# Check if a pod was selected
if [ -z "$selected_item" ]; then
echo "No pod selected. Exiting."
exit 1
fi
# Extract pod name from selection
selected_pod=$(echo "$selected_item" | awk '{print $2}')
echo "Selected Pod: $selected_pod"
echo
# Execute the appropriate kubectl command based on the argument
if [ "$command" == "logs" ]; then
kubectl logs -n $current_namespace $selected_pod
elif [ "$command" == "describe" ]; then
kubectl describe pod -n $current_namespace $selected_pod
fi