@@ -24,9 +24,14 @@ MONGODB_APP_PASSWORD=datapass
2424MONGODB_APP_DATABASE=${ZENKO_MONGODB_DATABASE:- datadb}
2525MONGODB_RS_KEY=0123456789abcdef
2626
27- MONGODB_SHARD_COUNT=${MONGODB_SHARD_COUNT:- 1}
27+ # Constants for valid topologies for CI tests
28+ # We support multiple shards per host, or one shard per host, up to 9 nodes
29+ # The first number is the number of nodes, the second is the number of shards
30+ readonly MONGODB_VALID_TOPOLOGIES=(
31+ " 1:1" " 1:2" " 3:1" " 3:3" " 6:1" " 6:2" " 6:6" " 9:1" " 9:3" " 9:9" " 12:1" " 12:4" " 12:12"
32+ )
2833
29- source " ${DIR} /generate-kustomization.sh " && generate_kustomization " ${NODE_COUNT :- 1}" " ${MONGODB_SHARD_COUNT} "
34+ MONGODB_SHARD_COUNT= ${MONGODB_SHARD_COUNT :- 1}
3035
3136ENABLE_KEYCLOAK_HTTPS=${ENABLE_KEYCLOAK_HTTPS:- ' false' }
3237
@@ -116,6 +121,84 @@ stringData:
116121 mongodb-replica-set-key: $MONGODB_RS_KEY
117122EOF
118123
124+ # Validate that the current topology is correct
125+ validate_mongodb_topology () {
126+ local node_count=$1
127+ local shard_count=$2
128+ # DIR is expected to be set by the sourcing script (e.g., install-kind-dependencies.sh)
129+ # It defines where kustomization.yaml will be created and the base for relative paths.
130+ if [ -z " ${DIR+x} " ]; then # Check if DIR is set
131+ echo " Error: DIR variable is not set. This script expects DIR to be defined by the calling environment."
132+ exit 1
133+ fi
134+ local kustomization_file=" ${DIR} /kustomization.yaml"
135+ local base_yaml_name=" mongodb-sharded-${node_count} -node"
136+
137+ # Validate topology
138+ local topology_key=" ${node_count} :${shard_count} "
139+ if [[ ! " ${MONGODB_VALID_TOPOLOGIES[*]} " =~ " ${topology_key} " ]]; then
140+ echo " Error: Invalid topology - ${node_count} nodes, ${shard_count} shards"
141+ exit 1
142+ fi
143+
144+ # Adjust base YAML name if there are multiple shards
145+ [[ " $shard_count " -gt 1 ]] && base_yaml_name=" ${base_yaml_name} -${shard_count} -shards"
146+ base_yaml_name=" ${base_yaml_name} .yaml"
147+
148+ # ensure base file exists
149+ local base_yaml_path=" ${DIR} /_build/root/deploy/${base_yaml_name} "
150+ if [ ! -f " $base_yaml_path " ]; then
151+ echo " Error: Base YAML file not found at ${base_yaml_path} "
152+ exit 1
153+ fi
154+
155+ # return the file path
156+ echo " $base_yaml_path "
157+ }
158+
159+ # MongoDB selectors are not supported in the CI.
160+ # So we remove them and let the provisioner handle the
161+ # volume provisioning.
162+ patch_mongodb_selector () {
163+ local node_count=$1
164+ local shard_count=$2
165+ # validate topology and get the base yaml path
166+ local base_yaml_path=$( validate_mongodb_topology $node_count $shard_count )
167+
168+ cat >> " $base_yaml_path " << EOF
169+ patches:
170+ - target:
171+ group: apps
172+ version: v1
173+ kind: StatefulSet
174+ name: data-db-mongodb-sharded-mongos
175+ patch: |-
176+ - op: remove
177+ path: /spec/volumeClaimTemplates/0/spec/selector
178+ - target:
179+ group: apps
180+ version: v1
181+ kind: StatefulSet
182+ name: data-db-mongodb-sharded-configsvr
183+ patch: |-
184+ - op: remove
185+ path: /spec/volumeClaimTemplates/0/spec/selector
186+ EOF
187+
188+ for (( i= 0 ; i< shard_count; i++ )) ; do
189+ cat >> " $base_yaml_path " << EOF
190+ - target:
191+ group: apps
192+ version: v1
193+ kind: StatefulSet
194+ name: data-db-mongodb-sharded-shard${i} -data
195+ patch: |-
196+ - op: remove
197+ path: /spec/volumeClaimTemplates/0/spec/selector
198+ EOF
199+ done
200+ }
201+
119202build_solution_base_manifests () {
120203 echo ' build solution-base manifests'
121204 MANIFEST_ONLY=true $SOLUTION_BASE_DIR /build.sh
@@ -139,24 +222,6 @@ build_solution_base_manifests() {
139222 sed -i " s/MONGODB_SHARDSERVER_RAM_LIMIT/${MONGODB_SHARDSERVER_RAM_LIMIT} /g" $DIR /_build/root/deploy/*
140223 sed -i " s/MONGODB_SHARDSERVER_RAM_REQUEST/${MONGODB_SHARDSERVER_RAM_REQUEST} /g" $DIR /_build/root/deploy/*
141224 sed -i " s/MONGODB_MONGOS_RAM_REQUEST/${MONGODB_MONGOS_RAM_REQUEST} /g" $DIR /_build/root/deploy/*
142-
143- # Limits and requests for MongoDB are computed based on the current system
144- # Detect total system RAM in GiB
145- TOTAL_RAM_GB=$( awk ' /MemTotal/ {printf "%.0f", $2/1024/1024}' /proc/meminfo)
146-
147- # Compute MongoDB settings based on the total RAM
148- MONGODB_WIRETIGER_CACHE_SIZE_GB=$(( TOTAL_RAM_GB * 335 / 1000 ))
149- MONGODB_MONGOS_RAM_LIMIT=$(( TOTAL_RAM_GB * 165 / 1000 )) Gi
150- MONGODB_SHARDSERVER_RAM_LIMIT=$(( 2 * MONGODB_WIRETIGER_CACHE_SIZE_GB)) Gi
151- MONGODB_SHARDSERVER_RAM_REQUEST=${MONGODB_WIRETIGER_CACHE_SIZE_GB} Gi
152- MONGODB_MONGOS_RAM_REQUEST=$(( TOTAL_RAM_GB * 33 / 1000 )) Gi
153-
154- # Replace values before deploying
155- sed -i " s/MONGODB_SHARDSERVER_EXTRA_FLAGS/--wiredTigerCacheSizeGB=${MONGODB_WIRETIGER_CACHE_SIZE_GB} /g" $DIR /_build/root/deploy/*
156- sed -i " s/MONGODB_MONGOS_RAM_LIMIT/${MONGODB_MONGOS_RAM_LIMIT} /g" $DIR /_build/root/deploy/*
157- sed -i " s/MONGODB_SHARDSERVER_RAM_LIMIT/${MONGODB_SHARDSERVER_RAM_LIMIT} /g" $DIR /_build/root/deploy/*
158- sed -i " s/MONGODB_SHARDSERVER_RAM_REQUEST/${MONGODB_SHARDSERVER_RAM_REQUEST} /g" $DIR /_build/root/deploy/*
159- sed -i " s/MONGODB_MONGOS_RAM_REQUEST/${MONGODB_MONGOS_RAM_REQUEST} /g" $DIR /_build/root/deploy/*
160225}
161226
162227get_image_from_deps () {
@@ -199,6 +264,8 @@ mongodb_sharded() {
199264 $SOLUTION_REGISTRY /os-shell=$( get_image_from_deps mongodb-shell) \
200265 $SOLUTION_REGISTRY /mongodb-exporter=$( get_image_from_deps mongodb-sharded-exporter)
201266
267+ patch_mongodb_selector " ${NODE_COUNT:- 1} " " ${MONGODB_SHARD_COUNT} "
268+
202269 kubectl apply -k " ${DIR} "
203270
204271 kubectl rollout status statefulset data-db-mongodb-sharded-mongos --timeout=5m
0 commit comments