-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathkubectl-rabbitmq
executable file
·349 lines (313 loc) · 8.77 KB
/
kubectl-rabbitmq
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/bash
#
# RabbitMQ Cluster Operator
#
# Copyright 2020 VMware, Inc. All Rights Reserved.
#
# This product is licensed to you under the Mozilla Public license, Version 2.0 (the "License"). You may not use this product except in compliance with the Mozilla Public License.
#
# This product may include a number of subcomponents with separate copyright notices and license terms. Your use of these subcomponents is subject to the terms and conditions of the subcomponent's license, as noted in the LICENSE file.
set -euo pipefail
instance=""
username=""
password=""
service=""
usage() {
usage=$(cat <<-END
USAGE:
Install (latest released) RabbitMQ Cluster Operator
kubectl rabbitmq install-cluster-operator
Create a RabbitMQ custom resource - INSTANCE name required, all other flags optional
kubectl rabbitmq create INSTANCE --replicas 1 --service ClusterIP --image rabbitmq:3.8.9-management --image-pull-secret mysecret
--tls-secret secret-name --storage-class mystorageclass
Get a RabbitMQ custom resource and dependant objects
kubectl rabbitmq get INSTANCE
List all RabbitMQ clusters
kubectl rabbitmq list
Delete a RabbitMQ cluster (or multiple clusters)
kubectl rabbitmq delete INSTANCE ...
Print default-user secrets for an instance
kubectl rabbitmq secrets INSTANCE
Open Management UI for an instance
kubectl rabbitmq manage INSTANCE
Set log level to 'debug' on all nodes
kubectl rabbitmq debug INSTANCE
Run 'rabbitmq-diagnostics observer' on a specific INSTANCE NODE
kubectl rabbitmq observe INSTANCE 0
Enable all feature flags on an INSTANCE
kubectl rabbitmq enable-all-feature-flags INSTANCE
Run perf-test against an instance - you can pass as many perf test parameters as you want
kubectl rabbitmq perf-test INSTANCE --rate 100
If you want to monitor perf-test, create the following ServiceMonitor:
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: kubectl-perf-test
spec:
endpoints:
- interval: 15s
targetPort: 8080
selector:
matchLabels:
app: perf-test
Print this help
kubectl rabbitmq help
Print kubectl-rabbitmq plugin version
kubectl rabbitmq version
END
)
echo "$usage"
}
version() {
# Since we require to install this plugin via krew, we get the version from krew instead of hardcoding a version to this file.
failure_msg="version cannot be determined because plugin was not installed via krew"
if ! command -v kubectl-krew &> /dev/null
then
echo "$failure_msg"
exit 1
fi
# We can't use `krew info` because it provides versions about available - not installed - plugins.
# `krew list` provides versions of installed plugins.
# We can't redirect stdout of `krew list` because this will suppress the version number (see `kubectl krew list --help`)
# Therefore, we have to get the version number from the `krew list` logs.
version_line=$(kubectl krew -v 4 list 2>&1 | grep 'rabbitmq: version=' || true)
if [[ -z "$version_line" ]];
then
echo "$failure_msg"
exit 1
fi
version="${version_line##*rabbitmq: version=}"
echo "kubectl-rabbitmq $version"
}
get_instance_details() {
instance=${1}
username=$(kubectl get secret "${instance}-default-user" -o jsonpath="{.data.username}" | base64 --decode)
password=$(kubectl get secret "${instance}-default-user" -o jsonpath="{.data.password}" | base64 --decode)
service="$instance"
}
perf_test() {
get_instance_details "$@"
shift 1
perftestopts=$*
kubectl run perf-test \
--expose=true \
--port=8080 \
--labels="app=perf-test,run=perf-test" \
--image=pivotalrabbitmq/perf-test \
-- --uri "amqp://${username}:${password}@${service}" \
--metrics-prometheus ${perftestopts}
}
manage() {
get_instance_details "$@"
(
sleep 2
open "http://localhost:15672/"
) &
kubectl port-forward "service/${service}" 15672
}
list_rabbitmq_clusters() {
kubectl get rabbitmqclusters
}
create() {
local rabbitmq_manifest_file="rabbitmq.yml"
set -u
cd "$(mktemp -d)" || exit 1
{
echo "apiVersion: rabbitmq.com/v1beta1"
echo "kind: RabbitmqCluster"
echo "metadata:"
echo " name: $1"
echo "spec:"
} >"$rabbitmq_manifest_file"
shift 1
# special case when no options are provided
if [[ "$#" -eq 0 ]]; then
echo " {}" >>"$rabbitmq_manifest_file"
fi
while [[ "$#" -ne 0 ]]; do
case "$1" in
"--replicas")
shift 1
echo " replicas: $1" >>"$rabbitmq_manifest_file"
shift 1
;;
"--service")
shift 1
echo " service:" >>"$rabbitmq_manifest_file"
echo " type: $1" >>"$rabbitmq_manifest_file"
shift 1
;;
"--image")
shift 1
echo " image: $1" >>"$rabbitmq_manifest_file"
shift 1
;;
"--image-pull-secret")
shift 1
echo " imagePullSecret: $1" >>"$rabbitmq_manifest_file"
shift 1
;;
"--unlimited")
shift 1
echo " resources:" >>"$rabbitmq_manifest_file"
echo " requests: {}" >>"$rabbitmq_manifest_file"
echo " limits: {}" >>"$rabbitmq_manifest_file"
;;
"--tls-secret")
shift 1
echo " tls:" >>"$rabbitmq_manifest_file"
echo " secretName: $1" >>"$rabbitmq_manifest_file"
shift 1
;;
"--storage-class")
shift 1
echo " persistence:" >>"$rabbitmq_manifest_file"
echo " storageClassName: $1" >>"$rabbitmq_manifest_file"
shift 1
;;
*)
echo "Option '$1' not recongnised"
exit 1
;;
esac
done
kubectl apply -f "$rabbitmq_manifest_file"
}
delete() {
for cluster in "$@"; do
kubectl delete rabbitmqcluster "${cluster}"
done
}
observe() {
kubectl exec -it "${1}-server-${2}" -- rabbitmq-diagnostics observer
}
get() {
kubectl get pods,cm,sts,svc,secrets,rs -l "app.kubernetes.io/name=$1"
}
debug() {
for node in $(kubectl get pods -l "app.kubernetes.io/name=${1}" -ocustom-columns=name:.metadata.name --no-headers); do
echo -n "${node}: "
kubectl exec "${node}" -- rabbitmqctl set_log_level debug
done
}
enable_all_feature_flags() {
kubectl exec "${1}-server-0" -- bash -c "rabbitmqctl list_feature_flags | grep disabled | cut -f 1 | xargs -r -L1 rabbitmqctl enable_feature_flag"
}
secrets() {
get_instance_details "$@"
echo "username: ${username}"
echo "password: ${password}"
}
install_cluster_operator() {
kubectl apply -f https://github.com/rabbitmq/cluster-operator/releases/latest/download/cluster-operator.yml
}
main() {
case "$1" in
"perf-test")
shift 1
if [[ "$#" -eq 0 ]] || [[ "$1" =~ (--[a-z-]) ]]; then
echo "Missing instance name"
usage
exit 1
fi
perf_test "$@"
;;
"manage")
shift 1
if [[ "$#" -eq 0 ]] || [[ "$1" =~ (--[a-z-]*) ]]; then
echo "Missing instance name"
usage
exit 1
fi
manage "$@"
;;
"list")
list_rabbitmq_clusters
;;
"create")
shift 1
if [[ "$#" -eq 0 ]]; then
usage
exit 1
fi
create "$@"
;;
"delete")
shift 1
if [[ "$#" -eq 0 ]]; then
usage
exit 1
fi
delete "$@"
;;
"get")
shift 1
if [[ "$#" -ne 1 ]]; then
usage
exit 1
fi
get "$1"
;;
"debug")
shift 1
if [[ "$#" -ne 1 ]]; then
usage
exit 1
fi
debug "$1"
;;
"observe")
shift 1
if [[ "$#" -ne 2 ]]; then
usage
exit 1
fi
observe "$1" "$2"
;;
"secrets")
shift 1
if [[ "$#" -ne 1 ]]; then
usage
exit 1
fi
secrets "$1"
;;
"enable-all-feature-flags")
shift 1
if [[ "$#" -ne 1 ]]; then
usage
exit 1
fi
enable_all_feature_flags "$1"
;;
"install-cluster-operator")
shift 1
if [[ "$#" -gt 0 ]]; then
usage
exit 1
fi
install_cluster_operator
;;
"help")
usage
;;
"--help")
usage
;;
"-h")
usage
;;
"version")
version
;;
*)
usage
exit 1
;;
esac
}
if [[ "$#" -ge 1 ]]; then
main "$@"
else
usage
fi