@@ -12,6 +12,9 @@ PLACEHOLDER_TEXT="/path/to/the/real/mc_kubeconfig/file"
1212MIN_VERSION=" 1.28.0"
1313ONBOARDED_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+
1518function 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+
290337function 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
351394function main() {
0 commit comments