-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathrun
More file actions
executable file
·244 lines (197 loc) · 9.03 KB
/
run
File metadata and controls
executable file
·244 lines (197 loc) · 9.03 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
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
#!/bin/bash
set -o errexit
set -o xtrace
test_dir=$(realpath "$(dirname "$0")")
. "${test_dir}/../functions"
function stop_cluster() {
local cluster_name=$1
local max_wait_time=${2:-120}
local passed_time=0
local sleep_time=1
kubectl_bin patch psmdb ${cluster_name} --type json -p='[{"op":"add","path":"/spec/pause","value":true}]'
set +x
echo -n 'Waiting for cluster stop'
until [[ $(kubectl_bin get psmdb ${cluster_name} -o jsonpath='{.status.mongos.ready}') -le 0 ]] \
&& [[ $(kubectl_bin get deployment ${cluster_name}-mongos -o jsonpath='{.status.replicas}') -le 0 ]] \
&& [[ $(kubectl_bin get psmdb ${cluster_name} -o jsonpath='{.status.replsets.cfg.ready}') -le 0 ]] \
&& [[ $(kubectl_bin get psmdb ${cluster_name} -o jsonpath='{.status.replsets.rs0.ready}') -le 0 ]]; do
echo -n .
let passed_time="${passed_time}+${sleep_time}"
sleep ${passed_time}
if [[ ${passed_time} -gt ${max_wait_time} ]]; then
echo "We've been waiting for cluster stop for too long. Exiting..."
exit 1
fi
done
echo
set -x
}
function start_cluster() {
local cluster_name=$1
kubectl_bin patch psmdb ${cluster_name} --type json -p='[{"op":"add","path":"/spec/pause","value":false}]'
wait_cluster_consistency ${cluster_name}
}
function compare_mongo_config() {
cluster=$1
namespace=$2
enable_expose=${3:-"true"}
desc "Compare mongo config"
cfg_0_endpoint="$cluster-cfg-0.$cluster-cfg.$namespace.svc.cluster.local"
cfg_0_endpoint_actual=$(run_mongo 'var host;var x=0;rs.conf().members.forEach(function(d){ if(d.tags.podName=="some-name-cfg-0"){ host=rs.conf().members[x].host;print(host)};x=x+1; })' "clusterAdmin:clusterAdmin123456@${cluster}-cfg.${namespace}" | grep -E -v 'I NETWORK|W NETWORK|Error saving history file|Percona Server for MongoDB|connecting to:|Unable to reach primary for set|Implicit session:|versions do not match|Error saving history file:|bye')
rs0_0_endpoint="$cluster-rs0-0.$cluster-rs0.$namespace.svc.cluster.local"
rs0_0_endpoint_actual=$(run_mongo 'var host;var x=0;rs.conf().members.forEach(function(d){ if(d.tags.podName=="some-name-rs0-0"){ host=rs.conf().members[x].host;print(host)};x=x+1; })' "clusterAdmin:clusterAdmin123456@${cluster}-rs0.${namespace}" | grep -E -v 'I NETWORK|W NETWORK|Error saving history file|Percona Server for MongoDB|connecting to:|Unable to reach primary for set|Implicit session:|versions do not match|Error saving history file:|bye')
if [[ $rs0_0_endpoint_actual != "$rs0_0_endpoint:27017" || $cfg_0_endpoint_actual != "$cfg_0_endpoint:27017" ]]; then
desc "Actual values rs $rs0_0_endpoint_actual and cfg $cfg_0_endpoint_actual do not match expected rs $rs0_0_endpoint:27017 and cfg $cfg_0_endpoint:27017"
exit 1
fi
}
function expose_cluster() {
expose_type=$1
expose_status=${2:-true}
kubectl_bin patch psmdb ${cluster} --type=json --patch '[
{
"op": "replace",
"path": "/spec/replsets/0/expose",
"value": {
"enabled": '$expose_status',
"type" : "'"${expose_type}"'"
}
},
{
"op": "replace",
"path": "/spec/sharding/mongos/expose",
"value": {
"type" : "'"${expose_type}"'"
}
},
{
"op": "replace",
"path": "/spec/sharding/configsvrReplSet/expose",
"value": {
"enabled": '${expose_status}',
"type" : "'"${expose_type}"'"
}
}]'
}
function main() {
create_infra "$namespace"
desc 'create first PSMDB cluster'
cluster="some-name"
kubectl_bin apply \
-f "$conf_dir/secrets.yml" \
-f "$conf_dir/client.yml"
apply_s3_storage_secrets
if version_gt "1.19" && [ $EKS -ne 1 ]; then
cat "$conf_dir/container-rc.yaml" | $sed 's/docker/runc/g' | kubectl_bin apply -f -
elif version_gt "1.24" && [ $EKS -eq 1 ]; then
cat "$conf_dir/container-rc.yaml" | $sed 's/docker/runc/g' | kubectl_bin apply -f -
else
kubectl_bin apply -f "$conf_dir/container-rc.yaml"
fi
apply_cluster "$test_dir/conf/$cluster-rs0.yml"
desc 'check if all 3 Pods started'
wait_for_running $cluster-rs0 3
wait_for_running $cluster-cfg 3 "false"
wait_for_running $cluster-mongos 3
wait_cluster_consistency "${cluster}"
desc 'check if service and statefulset created with expected config'
compare_kubectl statefulset/$cluster-rs0
compare_kubectl statefulset/$cluster-cfg
compare_kubectl statefulset/$cluster-mongos ""
desc 'disabling sharding'
kubectl_bin patch psmdb $cluster --type='json' -p='[{"op": "replace", "path": "/spec/sharding/enabled", "value": false}]'
sleep 10
wait_cluster_consistency "$cluster" 60
compare_kubectl statefulset/$cluster-rs0 "-sharding-disabled"
if [[ "$(kubectl_bin get sts -o yaml | yq '.items | length')" != 1 ]]; then
echo "Expected to have only 1 statefulset $cluster-rs0"
exit 1
fi
desc 'enabling sharding'
kubectl_bin patch psmdb $cluster --type='json' -p='[{"op": "replace", "path": "/spec/sharding/enabled", "value": true}]'
sleep 10
wait_cluster_consistency "$cluster" 60
compare_kubectl statefulset/$cluster-rs0 "-sharding-enabled"
compare_kubectl statefulset/$cluster-cfg
compare_kubectl statefulset/$cluster-mongos ""
desc 'write data, read from all'
run_mongos \
'db.createUser({user:"myApp",pwd:"myPass",roles:[{db:"myApp",role:"readWrite"}]})' \
"userAdmin:userAdmin123456@$cluster-mongos.$namespace"
run_mongo 'db.createUser({user: "myApp", pwd: "myPass", roles: [{ db: "myApp", role: "readWrite" }]})' \
"userAdmin:userAdmin123456@$cluster-rs0-0.$cluster-rs0.${namespace}" "mongodb"
run_mongos \
'sh.enableSharding("myApp","rs0")' \
"clusterAdmin:clusterAdmin123456@$cluster-mongos.$namespace"
run_mongos \
'use myApp\n db.test.insert({ x: 100500 })' \
"myApp:myPass@$cluster-mongos.$namespace"
compare_mongos_cmd "find" "myApp:myPass@$cluster-mongos.$namespace"
desc 'Unexposed -> Exposed, ClusterIP'
expose_cluster "ClusterIP"
wait_for_running $cluster-rs0 3
wait_for_running $cluster-cfg 3 "false"
wait_for_running $cluster-mongos 3
wait_cluster_consistency "${cluster}"
run_mongos \
'use myApp\n db.test.insert({ x: 100501 })' \
"myApp:myPass@$cluster-mongos.$namespace"
compare_mongos_cmd "find" "myApp:myPass@$cluster-mongos.$namespace" "-2nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-0.$cluster-rs0.$namespace" "-2nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-1.$cluster-rs0.$namespace" "-2nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-2.$cluster-rs0.$namespace" "-2nd"
compare_mongo_config ${cluster} $namespace
desc 'Exposed, ClusterIP -> LoadBalancer'
expose_cluster "LoadBalancer"
wait_for_running $cluster-rs0 3
wait_for_running $cluster-cfg 3 "false"
wait_for_running $cluster-mongos 3
wait_cluster_consistency "${cluster}"
run_mongos \
'use myApp\n db.test.insert({ x: 100502 })' \
"myApp:myPass@$cluster-mongos.$namespace"
compare_mongos_cmd "find" "myApp:myPass@$cluster-mongos.$namespace" "-3nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-0.$cluster-rs0.$namespace" "-3nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-1.$cluster-rs0.$namespace" "-3nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-2.$cluster-rs0.$namespace" "-3nd"
sleep 60 # Wait LB ip setup
desc "Pause Exposed cluster (LoadBalancer)"
stop_cluster ${cluster}
start_cluster ${cluster}
run_mongos \
'use myApp\n db.test.insert({ x: 100503 })' \
"myApp:myPass@$cluster-mongos.$namespace"
compare_mongos_cmd "find" "myApp:myPass@$cluster-mongos.$namespace" "-4nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-0.$cluster-rs0.$namespace" "-4nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-1.$cluster-rs0.$namespace" "-4nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-2.$cluster-rs0.$namespace" "-4nd"
desc "Exposed, LoadBalancer -> ClusterIP"
expose_cluster "ClusterIP"
wait_for_running $cluster-rs0 3
wait_for_running $cluster-cfg 3 "false"
wait_for_running $cluster-mongos 3
wait_cluster_consistency "${cluster}"
run_mongos \
'use myApp\n db.test.insert({ x: 100504 })' \
"myApp:myPass@$cluster-mongos.$namespace"
compare_mongos_cmd "find" "myApp:myPass@$cluster-mongos.$namespace" "-5nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-0.$cluster-rs0.$namespace" "-5nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-1.$cluster-rs0.$namespace" "-5nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-2.$cluster-rs0.$namespace" "-5nd"
desc "Exposed -> Unexposed"
expose_cluster "ClusterIP" "false"
wait_for_running $cluster-rs0 3
wait_for_running $cluster-cfg 3 "false"
wait_for_running $cluster-mongos 3
wait_cluster_consistency "${cluster}"
run_mongos \
'use myApp\n db.test.insert({ x: 100505 })' \
"myApp:myPass@$cluster-mongos.$namespace"
compare_mongos_cmd "find" "myApp:myPass@$cluster-mongos.$namespace" "-6nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-0.$cluster-rs0.$namespace" "-6nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-1.$cluster-rs0.$namespace" "-6nd"
compare_mongo_cmd "find" "myApp:myPass@$cluster-rs0-2.$cluster-rs0.$namespace" "-6nd"
kubectl_bin delete -f "$conf_dir/container-rc.yaml"
destroy "$namespace"
}
main