@@ -115,6 +115,64 @@ remove_all_finalizers() {
115115 done
116116}
117117
118+ wait_for_ready_containers () {
119+ local pod_prefix=" $1 "
120+ local target_count=" $2 "
121+ local namespace=" ${NAMESPACE} "
122+ local max_wait_seconds=300
123+ local check_interval=5
124+ local elapsed_time=0
125+
126+ if [[ -z " $pod_prefix " || -z " $target_count " || -z " $namespace " ]]; then
127+ echo " Error: Missing arguments." >&2
128+ echo " Usage: wait_for_ready_containers <pod_name_prefix> <target_ready_count> <namespace>" >&2
129+ return 1
130+ fi
131+
132+ echo " Waiting for pods starting with '$pod_prefix ' in namespace '$namespace ' to have $target_count ready containers (Max ${max_wait_seconds} s)..."
133+
134+ while [[ " $elapsed_time " -lt " $max_wait_seconds " ]]; do
135+ local target_pods
136+ # Get pods that match the prefix AND are running
137+ target_pods=$( kubectl get pods -n " $namespace " --field-selector=status.phase=Running --output=json | \
138+ jq -r " .items[] | select(.metadata.name | startswith(\" $pod_prefix \" )) | .metadata.name" )
139+ # If no running pods match the prefix, something might be wrong, but we'll keep waiting.
140+ if [[ -z " $target_pods " ]]; then
141+ echo " No running pods found with prefix '$pod_prefix '. Waiting..."
142+ sleep " $check_interval "
143+ elapsed_time=$(( elapsed_time + check_interval))
144+ continue
145+ fi
146+
147+ local ready_count=0
148+ local total_matches=0
149+
150+ # Check each pod individually
151+ for pod_name in $target_pods ; do
152+ total_matches=$(( total_matches + 1 ))
153+ current_ready=$( kubectl get pod " $pod_name " -n " $namespace " -o json 2> /dev/null | \
154+ jq ' .status.containerStatuses | map(select(.ready == true)) | length' )
155+
156+ if [[ " $current_ready " -eq " $target_count " ]]; then
157+ ready_count=$(( ready_count + 1 ))
158+ fi
159+ done
160+
161+ if [[ " $ready_count " -eq " $total_matches " ]]; then
162+ echo " Success: All $total_matches pods now have $target_count ready containers."
163+ return 0
164+ fi
165+
166+ echo " Current status: $ready_count of $total_matches pods have $target_count ready containers. Waiting ${check_interval} s..."
167+
168+ sleep " $check_interval "
169+ elapsed_time=$(( elapsed_time + check_interval))
170+ done
171+
172+ echo " Error: Timeout reached! After ${max_wait_seconds} seconds, not all pods reached $target_count ready containers." >&2
173+ return 1
174+ }
175+
118176destroy_operator () {
119177 kubectl -n " ${OPERATOR_NS:- $NAMESPACE } " delete deployment percona-postgresql-operator --force --grace-period=0 || true
120178 if [[ $OPERATOR_NS ]]; then
0 commit comments