Skip to content

Commit 431abf7

Browse files
committed
Improved code readability
1 parent 9926451 commit 431abf7

File tree

40 files changed

+15212
-66
lines changed

40 files changed

+15212
-66
lines changed

.githooks/pre-commit

Lines changed: 69 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ fi
1818

1919
mkdir -p "$(go env GOPATH)/bin"
2020

21-
function update_mco_tests() {
21+
update_mco_tests() {
2222
echo "Regenerating MCO evergreen tests configuration"
2323
python scripts/evergreen/e2e/mco/create_mco_tests.py >.evergreen-mco.yml
2424
git add .evergreen-mco.yml
2525
}
2626

2727
# Generates a yaml file to install the operator from the helm sources.
28-
function generate_standalone_yaml() {
28+
generate_standalone_yaml() {
2929
HELM_OPTS=$@
3030

3131
charttmpdir=$(mktemp -d 2>/dev/null || mktemp -d -t 'charttmpdir')
@@ -73,7 +73,7 @@ function generate_standalone_yaml() {
7373

7474
}
7575

76-
function python_formatting() {
76+
python_formatting() {
7777
# installing Black
7878
if ! command -v "black" >/dev/null; then
7979
pip install -r requirements.txt
@@ -85,15 +85,15 @@ function python_formatting() {
8585
black .
8686
}
8787

88-
function generate_manifests() {
88+
generate_manifests() {
8989
make manifests
9090

9191
git add config/crd/bases
9292
git add helm_chart/crds
9393
git add public/crds.yaml
9494
}
9595

96-
function update_values_yaml_files() {
96+
update_values_yaml_files() {
9797
# ensure that all helm values files are up to date.
9898
# shellcheck disable=SC2154
9999
python scripts/evergreen/release/update_helm_values_files.py
@@ -107,7 +107,7 @@ function update_values_yaml_files() {
107107
git add go.sum
108108
}
109109

110-
function update_release_json() {
110+
update_release_json() {
111111
# ensure that release.json is up 2 date
112112
# shellcheck disable=SC2154
113113
python scripts/evergreen/release/update_release.py
@@ -116,7 +116,7 @@ function update_release_json() {
116116
git add release.json
117117
}
118118

119-
function regenerate_public_rbac_multi_cluster() {
119+
regenerate_public_rbac_multi_cluster() {
120120
if echo "$git_last_changed" | grep -q -e 'cmd/kubectl-mongodb' -e 'pkg/kubectl-mongodb'; then
121121
echo 'regenerating multicluster RBAC public example'
122122
pushd pkg/kubectl-mongodb/common/
@@ -126,15 +126,15 @@ function regenerate_public_rbac_multi_cluster() {
126126
fi
127127
}
128128

129-
function update_licenses() {
129+
update_licenses() {
130130
if [[ "${MDB_UPDATE_LICENSES:-""}" == "true" ]]; then
131131
echo 'regenerating licenses'
132132
time scripts/evergreen/update_licenses.sh 2>&1 | prepend "update_licenses"
133133
git add LICENSE-THIRD-PARTY
134134
fi
135135
}
136136

137-
function check_erroneous_kubebuilder_annotations() {
137+
check_erroneous_kubebuilder_annotations() {
138138
# Makes sure there are not erroneous kubebuilder annotations that can
139139
# end up in CRDs as descriptions.
140140
if grep "// kubebuilder" ./* -r --exclude-dir=vendor --include=\*.go; then
@@ -143,13 +143,30 @@ function check_erroneous_kubebuilder_annotations() {
143143
fi
144144
}
145145

146-
function check_incorrect_makefile_variable_brackets() {
146+
check_incorrect_makefile_variable_brackets() {
147147
if find . -name "Makefile" | grep -v vendor | xargs grep "\${"; then
148148
echo -e "${RED}ERROR: Makefiles should NEVER contain curly brackets variables${NO_COLOR}"
149149
exit 1
150150
fi
151151
}
152152

153+
update_jobs() {
154+
# Update release.json first in case there is a newer version
155+
time update_release_json
156+
# We need to generate the values files first
157+
time update_values_yaml_files
158+
# The values files are used for generating the standalone yaml
159+
time generate_standalone_yaml
160+
}
161+
162+
lint_code() {
163+
scripts/evergreen/lint_code.sh
164+
}
165+
166+
# bg_job_ vars are global; capture_bg_job function is appending to them on each call
167+
bg_job_pids=()
168+
bg_job_pids_with_names=()
169+
153170
# Helper function to capture background job PID with job name
154171
capture_bg_job() {
155172
local job_name="$1"
@@ -166,66 +183,52 @@ get_job_name() {
166183
echo "${match#*:}" # Remove everything up to and including the colon
167184
}
168185

169-
function pre_commit() {
170-
# Array to store background job PIDs
171-
# IMPORTANT: ensure each task is executed in it's own subshell following the pattern:
172-
# time new_check 2>&1 | prepend "new_check" &
173-
# capture_bg_job "new_check"
174-
175-
# those bg_job_ vars are global and capture_bg_job is appending to them on each call
176-
bg_job_pids=()
177-
bg_job_pids_with_names=()
178-
179-
{
180-
# Update release.json first in case there is a newer version
181-
time update_release_json
182-
# We need to generate the values files first
183-
time update_values_yaml_files
184-
# The values files are used for generating the standalone yaml
185-
time generate_standalone_yaml
186-
} 2>&1 | prepend "update_jobs" &
187-
capture_bg_job "update_jobs"
188-
189-
time update_licenses 2>&1 | prepend "update_licenses" &
190-
capture_bg_job "update_licenses"
191-
192-
time scripts/evergreen/lint_code.sh 2>&1 | prepend "lint_code.sh" &
193-
capture_bg_job "lint_code.sh"
194-
195-
time start_shellcheck 2>&1 | prepend "shellcheck" &
196-
capture_bg_job "shellcheck"
197-
198-
time regenerate_public_rbac_multi_cluster 2>&1 | prepend "regenerate_public_rbac_multi_cluster" &
199-
capture_bg_job "regenerate_public_rbac_multi_cluster"
200-
201-
# Run black and isort on python files that have changed
202-
time python_formatting 2>&1 | prepend "python_formatting" &
203-
capture_bg_job "python_formatting"
186+
# Executes function given on the first argument as background job.
187+
# It's ensuring logs are properly prefixed by the name and
188+
# the job's pid is captured in bg_jobs array in order to wait for completion.
189+
run_job_in_background() {
190+
job_name=$1
191+
time ${job_name} 2>&1 | prepend "${job_name}" &
192+
capture_bg_job "${job_name}"
193+
}
204194

205-
time check_erroneous_kubebuilder_annotations 2>&1 | prepend "check_erroneous_kubebuilder_annotations" &
206-
capture_bg_job "check_erroneous_kubebuilder_annotations"
195+
# Waits for all background jobs stored in bg_job_pids and check their exit codes.
196+
wait_for_all_background_jobs() {
197+
failures=()
198+
for pid in "${bg_job_pids[@]}"; do
199+
wait "${pid}" || {
200+
job_name=$(get_job_name "${pid}")
201+
failures+=(" ${RED}${job_name} (PID ${pid})${NO_COLOR}")
202+
}
203+
done
207204

208-
# add any other checks above
205+
if [[ ${#failures[@]} -gt 0 ]]; then
206+
echo -e "${RED}Some checks have failed:${NO_COLOR}"
207+
for failure in "${failures[@]}"; do
208+
echo -e "$failure"
209+
done
210+
echo -e "${RED}To see the details look for the job's logs by it's prefixed name (e.g. \"shellcheck:\").${NO_COLOR}"
211+
return 1
212+
fi
209213

210-
# Wait for all background jobs and check their exit codes
211-
failures=()
212-
for pid in "${bg_job_pids[@]}"; do
213-
wait "${pid}" || {
214-
job_name=$(get_job_name "${pid}")
215-
failures+=(" ${RED}${job_name} (PID ${pid})${NO_COLOR}")
216-
}
217-
done
214+
return 0
215+
}
218216

219-
if [[ ${#failures[@]} -gt 0 ]]; then
220-
echo -e "${RED}Some checks have failed:${NO_COLOR}"
221-
for failure in "${failures[@]}"; do
222-
echo -e "$failure"
223-
done
224-
echo -e "${RED}To see the details look for the job's logs by it's prefixed name (e.g. \"shellcheck:\").${NO_COLOR}"
225-
exit 1
217+
pre_commit() {
218+
run_job_in_background "update_jobs"
219+
run_job_in_background "update_licenses"
220+
run_job_in_background "lint_code"
221+
run_job_in_background "start_shellcheck"
222+
run_job_in_background "regenerate_public_rbac_multi_cluster"
223+
run_job_in_background "python_formatting"
224+
run_job_in_background "check_erroneous_kubebuilder_annotations"
225+
226+
if wait_for_all_background_jobs; then
227+
echo -e "${GREEN}pre-commit: All checks passed!${NO_COLOR}"
228+
return 0
229+
else
230+
return 1
226231
fi
227-
228-
echo -e "${GREEN}pre-commit: All checks passed!${NO_COLOR}"
229232
}
230233

231234
# Function to run shellcheck on a single file
@@ -261,5 +264,5 @@ elif [[ "${cmd}" == "pre-commit" ]]; then
261264
elif [[ "${cmd}" == "shellcheck" ]]; then
262265
start_shellcheck
263266
elif [[ "${cmd}" == "lint" ]]; then
264-
source scripts/evergreen/lint_code.sh
267+
lint_code
265268
fi
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
01_0045_create_namespaces
2+
01_0046_create_image_pull_secrets
3+
01_0090_helm_add_mogodb_repo
4+
01_0305_create_mongodb_community_user_secrets
5+
01_0310_create_mongodb_community_resource
6+
01_0315_wait_for_community_resource
7+
01_0320_create_mongodb_search_resource
8+
01_0100_install_operator
9+
01_0325_wait_for_search_resource
10+
01_0330_wait_for_community_resource
11+
01_0335_show_running_pods
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
03_0410_run_mongodb_tools_pod
2+
03_0420_import_movies_mflix_database
3+
03_0430_create_search_index
4+
03_0435_create_vector_search_index
5+
03_0440_wait_for_search_index_ready
6+
03_0444_list_search_indexes
7+
03_0445_list_vector_search_indexes
8+
03_0450_execute_search_query
9+
03_0455_execute_vector_search_query

logs/0_diagnostics.txt

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
--------------------------------------------------
3+
MongoDB Resources
4+
--------------------------------------------------
5+
apiVersion: v1
6+
items: []
7+
kind: List
8+
metadata:
9+
resourceVersion: ""
10+
11+
--------------------------------------------------
12+
MongoDBCommunity Resources
13+
--------------------------------------------------
14+
apiVersion: v1
15+
items: []
16+
kind: List
17+
metadata:
18+
resourceVersion: ""
19+
20+
--------------------------------------------------
21+
MongoDBUser Resources
22+
--------------------------------------------------
23+
apiVersion: v1
24+
items: []
25+
kind: List
26+
metadata:
27+
resourceVersion: ""
28+
29+
--------------------------------------------------
30+
MongoDBOpsManager Resources
31+
--------------------------------------------------
32+
apiVersion: v1
33+
items: []
34+
kind: List
35+
metadata:
36+
resourceVersion: ""
37+
38+
--------------------------------------------------
39+
MongoDB Multi Resources
40+
--------------------------------------------------
41+
apiVersion: v1
42+
items: []
43+
kind: List
44+
metadata:
45+
resourceVersion: ""
46+
47+
--------------------------------------------------
48+
MongoDB Search Resources
49+
--------------------------------------------------
50+
apiVersion: v1
51+
items: []
52+
kind: List
53+
metadata:
54+
resourceVersion: ""
55+
56+
--------------------------------------------------
57+
All namespace resources
58+
--------------------------------------------------
59+
NAME READY STATUS RESTARTS AGE
60+
pod/mongodb-kubernetes-operator-68b79dbfc5-kdc7l 0/1 CrashLoopBackOff 6 (2m18s ago) 8h
61+
62+
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
63+
service/operator-webhook ClusterIP 10.96.20.31 <none> 443/TCP 21m
64+
65+
NAME READY UP-TO-DATE AVAILABLE AGE
66+
deployment.apps/mongodb-kubernetes-operator 0/1 1 0 8h
67+
68+
NAME DESIRED CURRENT READY AGE
69+
replicaset.apps/mongodb-kubernetes-operator-68b79dbfc5 1 1 0 8h

logs/events.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LAST SEEN TYPE REASON OBJECT MESSAGE
2+
47m Warning Failed pod/mongodb-kubernetes-operator-68b79dbfc5-kdc7l Failed to pull image "268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/mongodb-kubernetes:68876175f5ad6d0007fdc1d4": failed to pull and unpack image "268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/mongodb-kubernetes:68876175f5ad6d0007fdc1d4": failed to resolve reference "268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/mongodb-kubernetes:68876175f5ad6d0007fdc1d4": unexpected status from HEAD request to https://268558157000.dkr.ecr.us-east-1.amazonaws.com/v2/dev/mongodb-kubernetes/manifests/68876175f5ad6d0007fdc1d4: 403 Forbidden
3+
7m45s Normal BackOff pod/mongodb-kubernetes-operator-68b79dbfc5-kdc7l Back-off pulling image "268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/mongodb-kubernetes:68876175f5ad6d0007fdc1d4"
4+
7m31s Warning Failed pod/mongodb-kubernetes-operator-68b79dbfc5-kdc7l Error: ImagePullBackOff
5+
2m19s Warning BackOff pod/mongodb-kubernetes-operator-68b79dbfc5-kdc7l Back-off restarting failed container mongodb-kubernetes-operator in pod mongodb-kubernetes-operator-68b79dbfc5-kdc7l_ls(ce1d5fa8-ba4c-4a92-be51-fce4a2c4116a)
6+
2m7s Normal Pulling pod/mongodb-kubernetes-operator-68b79dbfc5-kdc7l Pulling image "268558157000.dkr.ecr.us-east-1.amazonaws.com/dev/mongodb-kubernetes:68876175f5ad6d0007fdc1d4"

0 commit comments

Comments
 (0)