-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnamespace-data-collector.sh
executable file
·319 lines (287 loc) · 10.6 KB
/
namespace-data-collector.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env bash
set -e
set -o pipefail
# This is a rather minimal example Argbash potential
# Example taken from http://argbash.readthedocs.io/en/stable/example.html
#
# ARG_OPTIONAL_SINGLE([namespace],[n],[namespace name],["default"])
# ARG_OPTIONAL_BOOLEAN([debug],[d],[enable debug])
# ARG_HELP([The general script's help msg])
# ARGBASH_GO()
# needed because of Argbash --> m4_ignore([
### START OF CODE GENERATED BY Argbash v2.9.0 one line above ###
# Argbash is a bash code generator used to get arguments parsing right.
# Argbash is FREE SOFTWARE, see https://argbash.io for more info
# Generated online by https://argbash.io/generate
# # When called, the process ends.
# Args:
# $1: The exit message (print to stderr)
# $2: The exit code (default is 1)
# if env var _PRINT_HELP is set to 'yes', the usage is print to stderr (prior to $1)
# Example:
# test -f "$_arg_infile" || _PRINT_HELP=yes die "Can't continue, have to supply file as an argument, got '$_arg_infile'" 4
die()
{
local _ret="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo "$1" >&2
exit "${_ret}"
}
# Function that evaluates whether a value passed to it begins by a character
# that is a short option of an argument the script knows about.
# This is required in order to support getopts-like short options grouping.
begins_with_short_option()
{
local first_option all_short_options='ndh'
first_option="${1:0:1}"
test "$all_short_options" = "${all_short_options/$first_option/}" && return 1 || return 0
}
# THE DEFAULTS INITIALIZATION - OPTIONALS
_arg_namespace="default"
_arg_debug="off"
# Function that prints general usage of the script.
# This is useful if users asks for it, or if there is an argument parsing error (unexpected / spurious arguments)
# and it makes sense to remind the user how the script is supposed to be called.
print_help()
{
printf '%s\n\n' "The general script's help msg"
printf 'Usage: %s -n|--namespace <arg> [-d|--(no-)debug] [-h|--help]\n\n' "$0"
printf '\t%s\t\t\t%s\n' "-n, --namespace" "namespace name (default: 'default')"
printf '\t%s\t\t%s\n' "-d, --debug, --no-debug" "enable debug (off by default)"
printf '\t%s\t\t\t%s\n' "-h, --help" "Prints help"
}
# The parsing of the command-line
parse_commandline()
{
while test $# -gt 0
do
_key="$1"
case "$_key" in
# We support whitespace as a delimiter between option argument and its value.
# Therefore, we expect the --namespace or -n value.
# so we watch for --namespace and -n.
# Since we know that we got the long or short option,
# we just reach out for the next argument to get the value.
-n|--namespace)
test $# -lt 2 && die "Missing value for the optional argument '$_key'." 1
_arg_namespace="$2"
shift
;;
# We support the = as a delimiter between option argument and its value.
# Therefore, we expect --namespace=value, so we watch for --namespace=*
# For whatever we get, we strip '--namespace=' using the ${var##--namespace=} notation
# to get the argument value
--namespace=*)
_arg_namespace="${_key##--namespace=}"
;;
# We support getopts-style short arguments grouping,
# so as -n accepts value, we allow it to be appended to it, so we watch for -n*
# and we strip the leading -n from the argument string using the ${var##-n} notation.
-n*)
_arg_namespace="${_key##-n}"
;;
# The debug argument doesn't accept a value,
# we expect the --debug or -d, so we watch for them.
-d|--no-debug|--debug)
_arg_debug="on"
test "${1:0:5}" = "--no-" && _arg_debug="off"
;;
# We support getopts-style short arguments clustering,
# so as -d doesn't accept value, other short options may be appended to it, so we watch for -d*.
# After stripping the leading -d from the argument, we have to make sure
# that the first character that follows corresponds to a short option.
-d*)
_arg_debug="on"
_next="${_key##-d}"
if test -n "$_next" -a "$_next" != "$_key"
then
{ begins_with_short_option "$_next" && shift && set -- "-d" "-${_next}" "$@"; } || die "The short option '$_key' can't be decomposed to ${_key:0:2} and -${_key:2}, because ${_key:0:2} doesn't accept value and '-${_key:2:1}' doesn't correspond to a short option."
fi
;;
# See the comment of option '--debug' to see what's going on here - principle is the same.
-h|--help)
print_help
exit 0
;;
# See the comment of option '-d' to see what's going on here - principle is the same.
-h*)
print_help
exit 0
;;
*)
_PRINT_HELP=yes die "FATAL ERROR: Got an unexpected argument '$1'" 1
;;
esac
shift
done
}
# Now call all the functions defined above that are needed to get the job done
parse_commandline "$@"
# OTHER STUFF GENERATED BY Argbash
### END OF CODE GENERATED BY Argbash (sortof) ### ])
[ "${_arg_debug}" == "on" ] && set -x
if [ "${_arg_namespace}" == "" ]; then
printf "error: missing namespace parameter\n\n"
print_help
exit 1
fi
DATA_DIR="data-collector/${_arg_namespace}"
rm -rf "${DATA_DIR}"
mkdir -p "${DATA_DIR}"
echo ""
echo "# Collecting data from namespace ${_arg_namespace} #"
echo ""
echo "Collecting events"
kubectl -n "${_arg_namespace}" get events --sort-by='{.lastTimestamp}' > "${DATA_DIR}/events.log" 2>&1
echo "Collecting namespace details"
kubectl get namespace "${_arg_namespace}" -o yaml > "${DATA_DIR}/namespace.yaml"
RESOURCES=(
# kubernetes
apiservices.apiregistration.k8s.io
clusterrolebindings.rbac.authorization.k8s.io
clusterroles.rbac.authorization.k8s.io
configmaps
controllerrevisions.apps
cronjobs.batch
csidrivers.storage.k8s.io
csinodes.storage.k8s.io
csistoragecapacities.storage.k8s.io
customresourcedefinitions.apiextensions.k8s.io
daemonsets.apps
deployments.apps
endpoints
endpointslices.discovery.k8s.io
events
events.events.k8s.io
flowschemas.flowcontrol.apiserver.k8s.io
horizontalpodautoscalers.autoscaling
ingressclasses.networking.k8s.io
ingresses.networking.k8s.io
jobs.batch
leases.coordination.k8s.io
limitranges
mutatingwebhookconfigurations.admissionregistration.k8s.io
namespaces
networkpolicies.networking.k8s.io
nodes
nodes.metrics.k8s.io
persistentvolumeclaims
persistentvolumes
poddisruptionbudgets.policy
pods
pods.metrics.k8s.io
podtemplates
priorityclasses.scheduling.k8s.io
prioritylevelconfigurations.flowcontrol.apiserver.k8s.io
replicasets.apps
replicationcontrollers
resourcequotas
rolebindings.rbac.authorization.k8s.io
roles.rbac.authorization.k8s.io
runtimeclasses.node.k8s.io
secrets
serviceaccounts
servicemonitors.monitoring.coreos.com
services
statefulsets.apps
storageclasses.storage.k8s.io
validatingadmissionpolicies.admissionregistration.k8s.io
validatingadmissionpolicybindings.admissionregistration.k8s.io
validatingwebhookconfigurations.admissionregistration.k8s.io
volumeattachments.storage.k8s.io
# openshift
routes
# prometheus
alertmanagerconfigs.monitoring.coreos.com
alertmanagers.monitoring.coreos.com
podmonitors.monitoring.coreos.com
probes.monitoring.coreos.com
prometheusagents.monitoring.coreos.com
prometheuses.monitoring.coreos.com
prometheusrules.monitoring.coreos.com
scrapeconfigs.monitoring.coreos.com
servicemonitors.monitoring.coreos.com
thanosrulers.monitoring.coreos.com
# operator lifecycle manager (olm)
catalogsources.operators.coreos.com
clusterserviceversions.operators.coreos.com
installplans.operators.coreos.com
olmconfigs.operators.coreos.com
operatorconditions.operators.coreos.com
operatorgroups.operators.coreos.com
operators.operators.coreos.com
packagemanifests.packages.operators.coreos.com
subscriptions.operators.coreos.com
# mariadb
backups.k8s.mariadb.com
connections.k8s.mariadb.com
databases.k8s.mariadb.com
grants.k8s.mariadb.com
mariadbs.k8s.mariadb.com
maxscales.k8s.mariadb.com
restores.k8s.mariadb.com
sqljobs.k8s.mariadb.com
users.k8s.mariadb.com
# activeMQArtemis
activemqartemisaddresses.broker.amq.io
activemqartemises.broker.amq.io
activemqartemisscaledowns.broker.amq.io
activemqartemissecurities.broker.amq.io
# cert-manager and trust-manager
bundles.trust.cert-manager.io
certificaterequests.cert-manager.io
certificates.cert-manager.io
certificatesigningrequests.certificates.k8s.io
challenges.acme.cert-manager.io
clusterissuers.cert-manager.io
issuers.cert-manager.io
orders.acme.cert-manager.io
)
for resource_type in "${RESOURCES[@]}"; do
# get the list of resource for the given resource type
resource_list=$(kubectl get "${resource_type}" -n "${_arg_namespace}" -o name 2>/dev/null || true)
if [ "${resource_list}" ] && [[ ! "${resource_list}" == *"/<unknown>"* ]]; then
echo "Collecting data for resource type ${resource_type}"
# create resource type dir
resource_type_dir="${DATA_DIR}/${resource_type}"
mkdir -p "${resource_type_dir}"
# for the given resource type get the list of resources
kubectl -n "${_arg_namespace}" get "${resource_type}" > "${resource_type_dir}/_${resource_type}.log"
for i in $resource_list; do
# dump the resource yaml for the given resource
name=${i#*/}
kubectl get "${resource_type}" "${name}" -o yaml -n "${_arg_namespace}" > "${resource_type_dir}/${name}.yaml"
# for pod resources, get some contents from it
if [ "${resource_type}" == "pods" ]; then
# get pod logs
pod_containers=$(kubectl -n "${_arg_namespace}" get pod "${name}" -o yaml | yq '.spec.initContainers[].name, .spec.containers[].name' || true)
echo "${pod_containers}" | while read -r c; do
kubectl -n "${_arg_namespace}" logs "${name}" -c "${c}" > "${resource_type_dir}/${name}-${c}".log
done
# if pod is an artemis pod, collect the artemis instance data
is_artemis_pod=$(kubectl -n "${_arg_namespace}" exec "${name}" -- env 2>/dev/null | grep AMQ_NAME || true)
if [ "${is_artemis_pod}" ]; then
pod_dir="${resource_type_dir}/${name}"
mkdir -p "${pod_dir}"
kubectl -n "${_arg_namespace}" exec "${name}" -- tar cf - amq-broker 2>/dev/null | tar xf - -C "${pod_dir}"
fi
fi
# for secrets, decode them
if [ "${resource_type}" == "secrets" ]; then
secret_dir="${resource_type_dir}/${name}"
mkdir -p "${secret_dir}"
kubectl -n "${_arg_namespace}" get secrets "${name}" -o yaml | yq -r '.data' | while read -r j; do
secret_data_name=$(echo "$j" | awk -F": " '{ print $1 }')
secret_data_value=$(echo "$j" | awk -F": " '{ print $2 }')
if [ -z "${secret_data_value}" ] || [ "${secret_data_value}" == "\"\"" ]; then
echo "${secret_data_value}" > "${secret_dir}/${secret_data_name}"; \
else
echo "${secret_data_value}" | base64 -d > "${secret_dir}/${secret_data_name}"
fi
done
fi
done
else
echo "No data for resource type ${resource_type}"
fi
done