diff --git a/scripts/cluster/run-production-acceptance.sh b/scripts/cluster/run-production-acceptance.sh index 58b9a40..66e5a0c 100755 --- a/scripts/cluster/run-production-acceptance.sh +++ b/scripts/cluster/run-production-acceptance.sh @@ -95,6 +95,7 @@ stable_profile=P24 active_profile=P24 failure_reason= failure_recorded=false +acceptance_admin_base= cleanup() { local status=$? @@ -130,21 +131,52 @@ admin_request() { local path=$2 local body=${3:-} local output=$4 - local admin_base=${AI_GATEWAY_ACCEPTANCE_GATEWAYS%%,*} - local status - if [[ -n $body ]]; then - status=$(curl -sS --max-time 30 -o "$output" -w '%{http_code}' \ - -X "$method" \ - -H "Authorization: Bearer $AI_GATEWAY_ACCEPTANCE_ADMIN_TOKEN" \ - -H 'Content-Type: application/json' \ - --data-binary "$body" \ - "$admin_base$path") - else - status=$(curl -sS --max-time 30 -o "$output" -w '%{http_code}' \ - -X "$method" \ - -H "Authorization: Bearer $AI_GATEWAY_ACCEPTANCE_ADMIN_TOKEN" \ - "$admin_base$path") + local status response payload token_encoded body_encoded has_body=false + local service_ip service_port remote_script remote_command + if [[ -z $acceptance_admin_base ]]; then + service_ip=$(remote_kubectl get service api-ningbo-edge -n "$namespace" \ + -o 'jsonpath={.spec.clusterIP}') + service_port=$(remote_kubectl get service api-ningbo-edge -n "$namespace" \ + -o 'jsonpath={.spec.ports[0].port}') + [[ $service_ip =~ ^[0-9.]+$ && $service_port =~ ^[0-9]+$ ]] || { + echo 'acceptance admin Service has an invalid cluster address' >&2 + return 1 + } + acceptance_admin_base=http://$service_ip:$service_port fi + token_encoded=$(printf '%s' "$AI_GATEWAY_ACCEPTANCE_ADMIN_TOKEN" | + openssl base64 -A) + body_encoded=$(printf '%s' "$body" | openssl base64 -A) + [[ -n $body ]] && has_body=true + # shellcheck disable=SC2016 # This script is quoted for execution on the remote node. + remote_script=' +set -euo pipefail +admin_base=$1 +method=$2 +path=$3 +token=$(printf "%s" "$4" | base64 -d) +body=$(printf "%s" "$5" | base64 -d) +has_body=$6 +if [[ $has_body == true ]]; then + curl -sS --max-time 30 -w "\n%{http_code}" -X "$method" \ + -H "Authorization: Bearer $token" \ + -H "Content-Type: application/json" \ + --data-binary "$body" "$admin_base$path" +else + curl -sS --max-time 30 -w "\n%{http_code}" -X "$method" \ + -H "Authorization: Bearer $token" "$admin_base$path" +fi +' + printf -v remote_command 'bash -c %q -- %q %q %q %q %q %q' \ + "$remote_script" "$acceptance_admin_base" "$method" "$path" \ + "$token_encoded" "$body_encoded" "$has_body" + if ! response=$(cluster_ssh "$CLUSTER_NINGBO_HOST" "$remote_command"); then + echo "acceptance control API transport failed: method=$method path=$path" >&2 + return 1 + fi + status=${response##*$'\n'} + payload=${response%$'\n'*} + printf '%s' "$payload" >"$output" [[ $status =~ ^2[0-9][0-9]$ ]] || { echo "acceptance control API failed: method=$method path=$path status=$status" >&2 return 1