-
Notifications
You must be signed in to change notification settings - Fork 282
/
Copy pathkubectl-rabbitmq
executable file
·307 lines (285 loc) · 8.22 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
#!/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() {
echo "USAGE:"
echo " Install RabbitMQ Cluster Operator (optionally provide image to use a relocated image or a specific version)"
echo " kubectl rabbitmq install-cluster-operator [IMAGE]"
echo
echo " Open Management UI for an instance"
echo " kubectl rabbitmq manage INSTANCE"
echo
echo " Print default-user secrets for an instance"
echo " kubectl rabbitmq secrets INSTANCE"
echo
echo " List all RabbitMQ clusters"
echo " kubectl rabbitmq list"
echo
echo " Delete a RabbitMQ cluster (or multiple clusters)"
echo " kubectl rabbitmq delete INSTANCE ..."
echo
echo " Create a RabbitMQ custom resource - INSTANCE name required, all other flags optional"
echo " kubectl rabbitmq create INSTANCE --replicas 1 --service ClusterIP --image rabbitmq:3.8.9-management --image-pull-secret mysecret"
echo " --tls-secret secret-name --storage-class mystorageclass"
echo
echo " Get a RabbitMQ custom resource and dependant objects"
echo " kubectl rabbitmq get INSTANCE"
echo
echo " Set log level to 'debug' on all nodes"
echo " kubectl rabbitmq debug INSTANCE"
echo
echo " Run 'rabbitmq-diagnostics observer' on a specific INSTANCE NODE"
echo " kubectl rabbitmq observe INSTANCE 0"
echo
echo " Enable all feature flags on an INSTANCE"
echo " kubectl rabbitmq enable-all-feature-flags INSTANCE"
echo
echo " Run perf-test against an instance - you can pass as many perf test parameters as you want"
echo " kubectl rabbitmq perf-test INSTANCE --rate 100"
echo
echo "If you want to monitor perf-test, create the following ServiceMonitor"
echo " apiVersion: monitoring.coreos.com/v1"
echo " kind: ServiceMonitor"
echo " metadata:"
echo " name: kubectl-perf-test"
echo " spec:"
echo " endpoints:"
echo " - interval: 15s"
echo " targetPort: 8080"
echo " selector:"
echo " matchLabels:"
echo " app: perf-test"
}
get_instance_details() {
instance=${1}
username=$(kubectl get secret "${instance}-rabbitmq-default-user" -o jsonpath="{.data.username}" | base64 --decode)
password=$(kubectl get secret "${instance}-rabbitmq-default-user" -o jsonpath="{.data.password}" | base64 --decode)
service=${instance}-rabbitmq-client
}
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
#statements
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
;;
*)
# Unrecognised or unsupported option
echo "Option '$1' not recongnised"
shift 1
;;
esac
done
kubectl apply -f "$rabbitmq_manifest_file"
}
delete() {
for cluster in "$@"; do
kubectl delete rabbitmqcluster "${cluster}"
done
}
observe() {
kubectl exec -it "${1}-rabbitmq-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}-rabbitmq-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 1 ]]; then
usage
exit 1
fi
install_cluster_operator "${1:-}"
;;
*)
usage
exit 1
;;
esac
}
if [[ "$#" -ge 1 ]]; then
main "$@"
else
usage
fi