Skip to content

Commit 282bea0

Browse files
authored
Merge pull request #12 from willisren/concurrent-onboard-clusters
support onboarding managed clusters in parallel
2 parents cfbe35f + cdb5809 commit 282bea0

2 files changed

Lines changed: 79 additions & 35 deletions

File tree

048-base-managed_clusters-onboard.sh

Lines changed: 78 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ PLACEHOLDER_TEXT="/path/to/the/real/mc_kubeconfig/file"
1212
MIN_VERSION="1.28.0"
1313
ONBOARDED_CLUSTER_INDEX_FILE="$CLUSTER_DATA_DIR/onboarded-clusters-name-index"
1414

15+
# Configuration for parallel processing
16+
CLUSTERS_ONBOARD_BATCH_SIZE=${CLUSTERS_ONBOARD_BATCH_SIZE:-1}
17+
1518
function init () {
1619
# If the $MC_KUBECONFIG_INDEX_FILE file is NOT completely updated, then stop to proceed.
1720
if grep -q "$PLACEHOLDER_TEXT" "$MC_KUBECONFIG_INDEX_FILE"; then
@@ -287,6 +290,50 @@ compare_versions() {
287290
[ "$(printf "%s\n%s" "$2" "$1" | sort -V | head -n1)" = "$2" ]
288291
}
289292

293+
onboard_workload_cluster() {
294+
local wc_file="$1"
295+
local i="$2"
296+
local name mgmt prov group proxy registry
297+
name=$(yq eval ".clusters[$i].fullName.name" "$wc_file")
298+
mgmt=$(yq eval ".clusters[$i].fullName.managementClusterName" "$wc_file")
299+
prov=$(yq eval ".clusters[$i].fullName.provisionerName" "$wc_file")
300+
group=$(yq eval ".clusters[$i].spec.clusterGroupName" "$wc_file")
301+
proxy=$(yq eval ".clusters[$i].spec.proxyName // \"\"" "$wc_file")
302+
registry=$(yq eval ".clusters[$i].spec.imageRegistry // \"\"" "$wc_file")
303+
304+
local version clean_version
305+
version=$(yq eval ".clusters[$i].spec.topology.version" "$wc_file")
306+
clean_version=$(sanitize_version "$version")
307+
if ! compare_versions "$clean_version" "$MIN_VERSION"; then
308+
log warn "Skipping: the version '$version' of cluster '$name' in namespace '$prov' is NOT supported (< v$MIN_VERSION)"
309+
return 0
310+
fi
311+
312+
# Build the command
313+
local cmd
314+
cmd="tanzu tmc mc wc manage \"$name\" -m \"$mgmt\" -p \"$prov\" --cluster-group \"$group\""
315+
[[ -n "$proxy" ]] && cmd+=" --proxy-name \"$proxy\""
316+
[[ -n "$registry" ]] && cmd+=" --image-registry \"$registry\""
317+
318+
log info "Run $cmd"
319+
if ! eval "$cmd"; then
320+
log error "❌ Failed to manage $name, please re-run this script later (required)"
321+
return 1
322+
else
323+
if wait_cluster_ready "$mgmt" "$prov" "$name"; then
324+
onboarded_cluster_name="$mgmt.$prov.$name"
325+
if ! grep -qxF "$onboarded_cluster_name" "$ONBOARDED_CLUSTER_INDEX_FILE"; then
326+
# If it doesn't exist, append it
327+
echo "$onboarded_cluster_name" >>"$ONBOARDED_CLUSTER_INDEX_FILE"
328+
fi
329+
return 0
330+
else
331+
log error "❌ Cluster $name is not ready, please double check it in the TMC-SM and ensure it's ready and then re-run this script again (required)"
332+
return 1
333+
fi
334+
fi
335+
}
336+
290337
function onboard_workload_clusters () {
291338
# Ensure the index file exists before grepping
292339
[ -f "$ONBOARDED_CLUSTER_INDEX_FILE" ] || touch "$ONBOARDED_CLUSTER_INDEX_FILE"
@@ -306,46 +353,42 @@ function onboard_workload_clusters () {
306353

307354
# Count how many workload clusters exist in the file
308355
cluster_count=$(yq eval '.clusters | length' "$wc_file")
309-
310-
for ((i=0; i<cluster_count; i+=1)); do
311-
local name mgmt prov group proxy registry
312-
name=$(yq eval ".clusters[$i].fullName.name" "$wc_file")
313-
mgmt=$(yq eval ".clusters[$i].fullName.managementClusterName" "$wc_file")
314-
prov=$(yq eval ".clusters[$i].fullName.provisionerName" "$wc_file")
315-
group=$(yq eval ".clusters[$i].spec.clusterGroupName" "$wc_file")
316-
proxy=$(yq eval ".clusters[$i].spec.proxyName // \"\"" "$wc_file")
317-
registry=$(yq eval ".clusters[$i].spec.imageRegistry // \"\"" "$wc_file")
318-
319-
local version clean_version
320-
version=$(yq eval ".clusters[$i].spec.topology.version" "$wc_file")
321-
clean_version=$(sanitize_version "$version")
322-
if ! compare_versions "$clean_version" "$MIN_VERSION"; then
323-
log warn "Skipping: the version '$version' of cluster '$name' in namespace '$prov' is NOT supported (< v$MIN_VERSION)"
324-
continue
356+
log info "Processing $cluster_count clusters in parallel batches of $CLUSTERS_ONBOARD_BATCH_SIZE"
357+
358+
local batch_num=0
359+
for ((start=0; start<cluster_count; start+=CLUSTERS_ONBOARD_BATCH_SIZE)); do
360+
batch_num=$((batch_num + 1))
361+
local end=$((start + CLUSTERS_ONBOARD_BATCH_SIZE - 1))
362+
if [[ $end -ge $cluster_count ]]; then
363+
end=$((cluster_count - 1))
325364
fi
326-
327-
# Build the command
328-
local cmd
329-
cmd="tanzu tmc mc wc manage \"$name\" -m \"$mgmt\" -p \"$prov\" --cluster-group \"$group\""
330-
[[ -n "$proxy" ]] && cmd+=" --proxy-name \"$proxy\""
331-
[[ -n "$registry" ]] && cmd+=" --image-registry \"$registry\""
332-
333-
log info "Run $cmd"
334-
if ! eval "$cmd"; then
335-
log error "❌ Failed to manage $name, please re-run this script later (required)"
336-
else
337-
if wait_cluster_ready "$mgmt" "$prov" "$name"; then
338-
onboarded_cluster_name="$mgmt.$prov.$name"
339-
if ! grep -qxF "$onboarded_cluster_name" "$ONBOARDED_CLUSTER_INDEX_FILE"; then
340-
# If it doesn't exist, append it
341-
echo "$onboarded_cluster_name" >>"$ONBOARDED_CLUSTER_INDEX_FILE"
342-
fi
343-
else
344-
log error "❌ Cluster $name is not ready, please double check it in the TMC-SM and ensure it's ready and then re-run this script again (required)"
365+
366+
log info "Processing batch $batch_num: clusters $((start + 1))-$((end + 1)) of $cluster_count"
367+
local pids=()
368+
for ((i=start; i<=end; i++)); do
369+
# Start the cluster processing in background
370+
onboard_workload_cluster "$wc_file" "$i" &
371+
pids+=($!)
372+
done
373+
374+
local failed_jobs=0
375+
for pid in "${pids[@]}"; do
376+
if ! wait "$pid"; then
377+
failed_jobs=$((failed_jobs + 1))
345378
fi
379+
done
380+
381+
if [[ $failed_jobs -gt 0 ]]; then
382+
log warn "Batch $batch_num completed with $failed_jobs failed jobs out of $((end - start + 1)) total jobs"
383+
else
384+
log info "Batch $batch_num completed successfully"
346385
fi
347386
done
387+
388+
log info "All clusters processing completed for management cluster: $mc_name"
348389
done <$REGISTERED_FILE
390+
391+
log info "All management clusters processing completed"
349392
}
350393

351394
function main() {

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ Operation includes:
371371

372372
12. Run script [048-base-managed\_clusters-input\_from\_user.sh](./048-base-managed_clusters-input_from_user.sh) to generate a Kubeconfig index file for the onboarding management clusters. Replace the path placeholders `/path/to/the/real/mc_kubeconfig/file` in the generated Kubeconfig index file.
373373
Then run script [048-base-managed\_clusters-onboard.sh](./048-base-managed_clusters-onboard.sh) to onboard the exported clusters onto SM.
374+
By default, clusters are onboarded sequentially. Set the CLUSTERS_ONBOARD_BATCH_SIZE environment variable to control the parallel batch size."
374375
375376
13. Run script [049-base-attached\_clusters-input\_from_user.sh](./049-base-attached_clusters-input_from_user.sh) to generate a Kubeconfig index file for the attached clusters. Replace the path placeholders `/path/to/the/real/wc_kubeconfig/file` in the generated Kubeconfig index file.
376377

0 commit comments

Comments
 (0)