-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathgather_utils
More file actions
137 lines (114 loc) · 3.98 KB
/
gather_utils
File metadata and controls
137 lines (114 loc) · 3.98 KB
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
#! /bin/bash
BASE_COLLECTION_PATH=${BASE_COLLECTION_PATH:-"./must-gather"}
# log produces a formatted log string with component, level, and message positional arguments:
# - 1 argument: Log message (default log level)
# - 2 arguments: Custom log level and message
log() {
lvl="${1}"
msg="${2}"
if [[ "$#" == "1" ]]; then
msg="${1}"
lvl="INFO"
fi
case ${lvl} in
INFO | WARN | DEBUG | ERROR);;
*) lvl="INFO";;
esac
printf "%s\tacm-must-gather\t%s\n" "${lvl}" "${msg}"
}
# print_header prints an underlined heading with a trailing newline
print_with_header() {
header=${1}
content=${2}
# shellcheck disable=SC2001
underline=$(echo "${header}" | sed 's/./-/g')
printf "%s\n%s\n%s\n\n" "${header}" "${underline}" "${content}"
}
# run_inspect wraps `oc adm inspect` by logging the args and running it with common flags
run_inspect() {
args="$*"
log "Inspecting ${args}"
# shellcheck disable=SC2086
# - BASE_COLLECTION_PATH can't be quoted because `oc` reads the quotes as part of the path
oc adm inspect --dest-dir=${BASE_COLLECTION_PATH} "$@"
}
# parse_args parses the arguments passed to the must-gather in the form of <key>=<value>.
# NOTE: Currently this only applies to the Hypershift must-gather.
parse_args() {
# get the hosted cluster name and optionally namespace
while [ "${1}" != "" ]; do
local key value
key=${1%=*}
value=${1#*=}
case ${key} in
hosted-cluster-name) # Get the hosted cluster name
export HC_NAME=${value}
;;
hosted-cluster-namespace) # Get the hosted cluster namespace
export HC_NAMESPACE=${value}
;;
*)
log "ERROR" "unknown parameter \"${key}\""
exit 1
;;
esac
shift
done
}
ensure_hypershift_cli() {
# First, check if the container image already contains the /usr/bin/hypershift
if [ -f /usr/bin/hypershift ]; then
log "Using hypershift binary at /usr/bin/hypershift."
export HYPERSHIFT="/usr/bin/hypershift"
return 0
fi
log "/usr/bin/hypershift is not found. Trying to extract it from the hypershift operator."
# Now, try to extract the hypershift CLI from the hypershift operator
if extract_hypershift_cli; then
log "Successfully extracted the hypershift CLI binary to /tmp/hypershift."
export HYPERSHIFT="/tmp/hypershift"
return 0
fi
log "Extracting the hypershift binary from the hypershift operator failed."
return 1
}
extract_hypershift_cli() {
if ! oc get namespace hypershift; then
log "hypershift namespace not found"
return 1
fi
# Get a running hypershift operator pod
HO_POD_NAME=$(oc get pod -n hypershift --no-headers=true --field-selector=status.phase=Running -l app=operator -o name | head -n 1)
if [[ -n $HO_POD_NAME ]]; then
log "Found a running hypershift operator pod: \"$HO_POD_NAME\""
else
log "WARN" "No running hypershift operator pod found."
return 1
fi
# Extract the hypershift CLI from the hypershift operator pod
oc rsync -c operator -n hypershift ${HO_POD_NAME}:/usr/bin/hypershift /tmp
chmod 755 /tmp/hypershift
return 0
}
dump_hostedcluster() {
if [[ -z $HC_NAME ]]; then
log "Hosted cluster name was not provided. Skip collecting hosted cluster must-gather."
return 0
fi
HC=$(oc get hostedcluster $HC_NAME -n $HC_NAMESPACE)
if [[ -z $HC ]]; then
log "ERROR" "hosted cluster \"$HC_NAME\" not found in \"$HC_NAMESPACE\" namespace"
return 1
fi
# This could be the second time to be here if both MCE and spoke gathers are run.
if [ -f $BASE_COLLECTION_PATH/hypershift-dump.tar.gz ]; then
log "Must-gather for hosted cluster \"$HC_NAME\" in namespace \"$HC_NAMESPACE\" has already been collected."
return 0
fi
if ! ensure_hypershift_cli; then
log "ERROR" "Failed to find the hypershift CLI binary."
return 1
fi
log "Collecting must-gather for hosted cluster \"$HC_NAME\" in namespace \"$HC_NAMESPACE\""
${HYPERSHIFT} dump cluster --dump-guest-cluster --artifact-dir $BASE_COLLECTION_PATH --name $HC_NAME --namespace $HC_NAMESPACE
}