This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubectl-dump
executable file
·85 lines (69 loc) · 2.37 KB
/
kubectl-dump
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
#!/bin/bash
set -x
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <namespace>"
exit 1
fi
NAMESPACE="$1"
DATE=$(date '+%Y-%m-%d_%H-%M-%S')
DUMP_DIR="kubectl-dump-${NAMESPACE}-${DATE}"
OUTPUT_FORMAT="${K8S_DEBUG_OUTPUT_FORMAT:-yaml}"
mkdir -p "${DUMP_DIR}"
declare -a resources=("pods" "services" "deployments" "replicasets" "configmaps" "secrets")
function dump_resource() {
local resource="$1"
local objects="$(kubectl get "${resource}" \
--namespace="${NAMESPACE}" \
-o jsonpath='{.items[*].metadata.name}')"
echo "Dumping ${resource}..."
echo "=============================="
kubectl get "${resource}" --namespace="${NAMESPACE}" | \
tee "${DUMP_DIR}/${resource}.get.txt"
for object in ${objects}; do
# Describe the resource in txt format and print it
echo "Dumping ${resource} '${object}'..."
echo "=============================="
kubectl describe "${resource}" "${object}" \
--namespace="${NAMESPACE}" | tee \
"${DUMP_DIR}/${resource}.${object}.describe.txt"
# Describe the resource in the specified output format and save it
kubectl get "${resource}" "${object}" \
--namespace="${NAMESPACE}" \
-o "${OUTPUT_FORMAT}" > \
"${DUMP_DIR}/${resource}.${object}.describe.${OUTPUT_FORMAT}"
done
}
function collect_logs() {
local resource="$1"
local objects="$(kubectl get "${resource}" \
--namespace="${NAMESPACE}" \
-o jsonpath='{.items[*].metadata.name}')"
echo "Collecting logs from ${resource}..."
echo "=============================="
for object in ${objects}; do
LOG_DIR="${DUMP_DIR}/logs/${object}"
mkdir -p "${LOG_DIR}"
CONTAINERS=$(
kubectl get "${resource}" "${object}" \
--namespace="${NAMESPACE}" \
-o jsonpath='{.spec.containers[*].name}'
)
for CONTAINER in ${CONTAINERS}; do
kubectl logs --namespace="${NAMESPACE}" "${object}" -c "${CONTAINER}" | \
tee "${LOG_DIR}/${CONTAINER}.log"
done
done
}
function main() {
echo "Retrieving information about Kubernetes resources in namespace '${NAMESPACE}'..."
for resource in "${resources[@]}"; do
dump_resource "${resource}"
done
echo "Collecting logs from all pods in namespace '${NAMESPACE}'..."
collect_logs "pods"
echo "All information collected and saved to '${DUMP_DIR}'."
}
# If script is sourced, don't call main
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main
fi