Skip to content

Commit 8a60409

Browse files
authored
Tool for editing cluster config secret (#1039)
* Script for editing secret with cluster config * Updated script to handle json parsing errors * Added usage example as comment * Fixed typo
1 parent e868003 commit 8a60409

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

scripts/dev/edit_cluster_config.sh

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env bash
2+
3+
# This script is intended for editing the raw cluster configuration that is used by the automation agents.
4+
# For example if we want to test new features implemented by the automation agent, we can check its behavior by configuring new settings directly in the raw cluster config.
5+
# Steps to do that:
6+
# 1. Deploy replica set, named here "my-replica-set" in "mongodb" namespace.
7+
# 2. Stop the operator, e.g. by scaling operator deployment to 0. Without this step the operator will overwrite any changes made to the cluster config in the secret.
8+
# 3. Edit the cluster config by running: ./edit_cluster_config.sh mongodb my-replica-set, or EDITOR=my-editor ./edit_cluster_config.sh mongodb my-replica-set (if you don't want to use vim)
9+
# 4. It will download the cluster config from the secret and open it in the editor.
10+
# 5. Make some changes to the cluster config, e.g. add new settings. Remember to increment version field, otherwise the changes won't be applied.
11+
# 6. Save the changes and exit the editor. The config will be checked if it's a correct json and will be uploaded to the secret.
12+
# 7. Observe the changes made by the mongodb-agent. Be aware, that starting the operator again will overwrite the changes.
13+
14+
namespace=$1
15+
replicaset_name=$2
16+
secret_name=${replicaset_name}-config
17+
18+
if [[ "${namespace}" == "" || "${replicaset_name}" == "" ]]; then
19+
echo "Edit automation config secret for given replicaset."
20+
echo "It looks for the secret named '<replicaset_name>-secret' in the given namespace."
21+
echo "Requires jq to be installed and uses current kubectl context."
22+
echo
23+
echo "Usage:"
24+
printf "\t%s <namespace> <replicaset_name>\n" "$(basename "$0")"
25+
printf "\tEDITOR=<custom editor> %s <namespace> <replicaset_name> to edit cluster config with a different editor.\n" "$(basename "$0")"
26+
27+
exit 1
28+
fi
29+
30+
cluster_config_file=$(mktemp ./edit_cluster_config.sh.cluster_config.XXXXX)
31+
# rename to have .json extension for syntax highlighting in the editor
32+
mv "${cluster_config_file}" "${cluster_config_file}.json"
33+
cluster_config_file="${cluster_config_file}.json"
34+
cluster_config_file_base64="${cluster_config_file}.base64"
35+
36+
function cleanup() {
37+
rm -f "${cluster_config_file}" "${cluster_config_file_base64}"
38+
}
39+
trap cleanup EXIT
40+
41+
function get_secret() {
42+
local namespace=$1
43+
local secret_name=$2
44+
kubectl get secret "${secret_name}" -n "${namespace}" -o json | jq -r '.data."cluster-config.json"' | base64 -D
45+
}
46+
47+
echo "Saving config to a temporary file: ${cluster_config_file}"
48+
get_secret "${namespace}" "${secret_name}" | jq . -r >"${cluster_config_file}"
49+
error_code=$?
50+
51+
if [[ ${error_code} != 0 ]]; then
52+
echo "Cluster config is invalid, edit without parsing with jq:"
53+
get_secret "${namespace}" "${secret_name}" >"${cluster_config_file}"
54+
fi
55+
56+
if [[ "${EDITOR}" == "" ]]; then
57+
EDITOR=vim
58+
fi
59+
60+
old_config=$(cat "${cluster_config_file}")
61+
while true; do
62+
${EDITOR} "${cluster_config_file}"
63+
new_config=$(jq . < "${cluster_config_file}")
64+
error_code=$?
65+
if [[ ${error_code} != 0 ]]; then
66+
read -n 1 -rsp $"Press any key to continue editing or ^C to abort..."
67+
echo
68+
continue
69+
fi
70+
break
71+
done
72+
73+
if diff -q <(echo -n "${old_config}") <(echo -n "${new_config}"); then
74+
echo "No changes made to cluster config."
75+
exit 0
76+
else
77+
echo "Cluster config was changed with following diff:"
78+
diff --normal <(echo -n "${old_config}") <(echo -n "${new_config}")
79+
fi
80+
81+
base64 < "${cluster_config_file}" > "${cluster_config_file_base64}"
82+
83+
# shellcheck disable=SC2086
84+
patch=$(cat <<EOF | jq -rc
85+
[
86+
{ "op" : "replace",
87+
"path" : "/data/cluster-config.json",
88+
"value" : "$(cat ${cluster_config_file_base64})"
89+
}
90+
]
91+
EOF
92+
)
93+
94+
kubectl patch secret -n "${namespace}" "${secret_name}" --type='json' -p="${patch}"

0 commit comments

Comments
 (0)