/
make-metrics-doc.sh
executable file
·143 lines (122 loc) · 5.32 KB
/
make-metrics-doc.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
#!/usr/bin/env bash
# Copyright 2020 Antrea Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# The script deploys a Kind cluster with Prometheus metrics enabled. Then queries Antrea endpoints for metrics, and
# outputs it to the console.
# When a doc filename parameter is specified, the script updates the metrics list within the document with the metrics
# from the Kind deployment.
set -eo pipefail
THIS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
function exit_handler() {
echo "Cleaning up..."
if [ -f $certfile ]; then
rm -rf certfile
$THIS_DIR/../ci/kind/kind-setup.sh destroy kind
fi
}
function get_metrics_url() {
pod_name=$1
host_port=$(kubectl get pod -n kube-system $pod_name -o jsonpath="{.spec.containers[*].ports[*].hostPort}")
echo "https://localhost:$host_port/metrics"
}
function format_metrics() {
sorted_metrics=$1
# Gather list of metric names
metrics_types_unarranged=$(awk '/# TYPE/{print $3}' <<< $sorted_metrics)
# Put Antrea-specific metrics at the beginning, push 3rd parties after
metrics_types=$(grep antrea <<< $metrics_types_unarranged)$'\n'$(grep -v antrea <<< $metrics_types_unarranged)
# Gather metrics descriptions
metrics_help=$(grep '# HELP' <<< $sorted_metrics | sed 's/\[.*\] //i')
last_pfx=""
echo 'Below is a list of metrics, provided by the components and by 3rd parties.'
echo
echo "### Antrea Metrics"
for metric in $metrics_types; do
metric_pfx=$(sed 's/_/ /g' <<< $metric | awk '{print $1}')
if [ "$metric_pfx" == 'antrea' ]; then
# For Antrea metrics, add Agent, Controller to title
metric_pfx=$(sed 's/_/ /g' <<< $metric | awk '{print $1" "$2}')
fi
if [ "$last_pfx" != "$metric_pfx" ]; then
echo
# Ouptut metrics title
# Ouptut 3rd party metrics title
if [[ "$last_pfx" =~ ^antrea.* ]] && [[ ! "$metric_pfx" =~ ^antrea.* ]]; then
echo "### Common Metrics Provided by Infrastructure"
echo
fi
# Ouptut metrics title
echo "#### "$(sed -e "s/\b\(.\)/\u\1/g" <<< $metric_pfx)" Metrics"
echo
last_pfx=$metric_pfx
fi
metric_help=$(grep " $metric " <<< $metrics_help | sed "s/.*$metric //")
echo "- **$metric:** $metric_help"
done
}
function print_usage {
echo 'Usage: make-metrics-doc.sh [-h|--help|<metrics_document>]'
exit 0
}
metrics_doc=""
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
print_usage
exit 0
;;
*) # unknown option
metrics_doc="$1"
break
;;
esac
done
if [ "$metrics_doc" != "" ] && [ ! -f $metrics_doc ]; then
echo "Metrics document not found at $metrics_doc"
exit 1
fi
trap exit_handler INT EXIT
# Initialize a Kind Antrea cluster
$THIS_DIR/../ci/kind/kind-setup.sh create kind --prometheus --num-workers 0 --antrea-cni
# Wait for Antrea components to be ready, allow Antrea inits to complete
kubectl -n kube-system wait --for=condition=ready --timeout=120s pod -l app=antrea
sleep 30
# Extract Antrea credentials
certfile=$(mktemp /tmp/cacert.XXXXXX)
mv "${certfile}" "${certfile}.ca"
certfile="${certfile}.ca"
# this name is specified in build/yamls/antrea-prometheus-rbac.yml
secret_name="prometheus-service-account-token"
kubectl get secrets -n monitoring $secret_name -o jsonpath="{.data.ca\.crt}" | base64 -d > $certfile
token=$(kubectl get secrets -n monitoring $secret_name --template "{{.data.token}}" | base64 -d)
# Find agent, controller pods
controller_pod=$(kubectl get pod -n kube-system | awk '/antrea-controller/{print $1}')
agent_pod=$(kubectl get pod -n kube-system | awk '/antrea-agent/{print $1}' | head -n1)
agent_metrics_url=$(get_metrics_url $agent_pod)
controller_metrics_url=$(get_metrics_url $controller_pod)
# Retrieve agent and controller metrics
agent_metrics=$(curl -fsk -H "Authorization: Bearer $token" --cacert $certfile $agent_metrics_url | grep '^#')
controller_metrics=$(curl -fsk -H "Authorization: Bearer $token" --cacert $certfile $controller_metrics_url | grep '^#')
# Sort metrics, eliminate duplicates e.g apiserver etc
sorted_metrics=$(sort -u <<< "${agent_metrics}"$'\n'"${controller_metrics}")
# Format metrics
formatted_metrics=$(format_metrics "$sorted_metrics")
if [ "$metrics_doc" == "" ]; then
fmt -w 80 -s <<< $formatted_metrics
else
sed -i '/^Below is a list of metrics, provided by the components and by 3rd parties.$/,$d' $metrics_doc
fmt -w 80 -s <<< $formatted_metrics >> $metrics_doc
fi