forked from eldada/kubernetes-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runCommandOnPods.sh
executable file
·116 lines (93 loc) · 2.61 KB
/
runCommandOnPods.sh
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
# UNCOMMENT this line to enable debugging
# set -xv
## Execute a command on a list of pods
EXEC="df -h"
PODS=
CONTAINER=
SCRIPT_NAME=$0
######### Functions #########
errorExit () {
echo -e "\nERROR: $1\n"
exit 1
}
usage () {
cat << END_USAGE
${SCRIPT_NAME} - Run a given command on all pods matching criteria
Usage: ${SCRIPT_NAME} <options>
-n | --namespace <name> : Namespace to analyse
-p | --pods <string> : Pods to run on (pattern)
-c | --container <string> : Container to run on (name)
-x | --exec <string> : Command to execute
-h | --help : Show this usage
Examples:
========
${SCRIPT_NAME} --namespace demo -pods artifactory-primary -exec "uptime" -container artifactory-app
END_USAGE
exit 1
}
# Process command line options. See usage above for supported options
processOptions () {
while [[ $# -gt 0 ]]; do
case "$1" in
-n | --namespace)
NAMESPACE="-n $2"
shift 2
;;
-x | --exec)
EXEC="$2"
shift 2
;;
-p | --pods)
PODS="$2"
shift 2
;;
-c | --container)
CONTAINER="$2"
shift 2
;;
-h | --help)
usage
exit 0
;;
*)
usage
;;
esac
done
[[ -z "${NAMESPACE}" ]] && errorExit "Must provide a namespace"
[[ -z "${CONTAINER}" ]] && echo "WARNING: No container set. Kubectl will default to the first container in the pod"
}
# Test connection to a cluster by kubectl
testConnection () {
kubectl cluster-info > /dev/null || errorExit "Connection to cluster failed"
}
runCommandOnPods () {
local list=
if [[ -n "${PODS}" ]]; then
list=$(kubectl get pods ${NAMESPACE} -o custom-columns=:metadata.name | grep ${PODS})
else
list=$(kubectl get pods ${NAMESPACE} -o custom-columns=:metadata.name)
fi
echo "${list}"
for l in ${list}; do
echo -en "\n---- Pod: $l"
# set -x
if [[ -n "${CONTAINER}" ]]; then
echo " (Container: ${CONTAINER})"
kubectl exec ${NAMESPACE} ${l} -c ${CONTAINER} -- ${EXEC}
else
echo
kubectl exec ${NAMESPACE} ${l} -- ${EXEC}
fi
# set +x
done
}
main () {
processOptions "$@"
[ "${QUITE}" == true ] || echo "Running command '${EXEC}' on pods"
testConnection
runCommandOnPods
}
######### Main #########
main "$@"