-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions
49 lines (42 loc) · 1.8 KB
/
functions
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
#!/usr/bin/env bash
get_mongodb_uri() {
local namespace=$1
local cluster=$2
local replset=$3
local username=$4
local password=$5
local endpoint=$6
local database=$7
local sharding=""
local replset_exposed=""
if [[ -z ${password} && ${username} == "backup" ]]; then
password=$(kubectl get secrets $(kubectl get psmdb ${cluster} -ojsonpath='{.spec.secrets.users}') -otemplate='{{.data.MONGODB_BACKUP_PASSWORD | base64decode}}')
elif [[ -n ${password} && -z ${username} ]]; then
echo "When specifying password please specify --user also." >&2
return 1
fi
sharding=$(kubectl get psmdb "${cluster}" -ojsonpath='{.spec.sharding.enabled}')
if [[ ${sharding} == "false" && -z ${replset} ]]; then
replset=$(kubectl get psmdb "${cluster}" -ojsonpath='{.spec.replsets[0].name}' ${namespace:+--namespace $namespace})
fi
if [[ -n ${replset} ]]; then
if [[ ${replset} != "cfg" ]]; then
replset_exposed=$(kubectl get psmdb "${cluster}" -ojson | jq -r ".spec.replsets | select(.[].name | contains (\"${replset}\")) | .[].expose.enabled" | head -n1)
else
replset_exposed=$(kubectl get psmdb "${cluster}" -ojson | jq -r ".spec.sharding.configsvrReplSet.expose.enabled")
fi
if [[ -z ${replset_exposed} ]]; then
echo "Replica set name not found!" >&2
return 1
fi
fi
if [[ -z ${endpoint} ]]; then
endpoint=$(kubectl get psmdb "${cluster}" -ojsonpath="{.status.host}" ${namespace:+--namespace $namespace})
if [[ -n ${replset} && ${replset_exposed} == "false" ]]; then
endpoint="${cluster}-${replset}.${namespace}.svc.cluster.local"
elif [[ -n ${replset} && ${replset_exposed} == "true" ]]; then
endpoint="${cluster}-${replset}-0.${namespace}.svc.cluster.local"
fi
fi
echo "mongodb://${username}:${password}@${endpoint}/${database}?ssl=false&authSource=admin${replset:+&replicaSet=$replset}"
}