forked from longtao.wu/droboticflow
🔎 Auto-discover POC strict import endpoint
This commit is contained in:
parent
cf603e9829
commit
3faf97fd76
@ -231,7 +231,7 @@ For the final no-mock POC gate, prefer the strict orchestrator:
|
||||
DEPLOY_ENV=poc npm run acceptance:poc-strict
|
||||
```
|
||||
|
||||
`acceptance:poc-strict` refuses dependency-isolation scopes by requiring `POC_ACCEPTANCE_REQUIRED_EXTERNALS` to include `bos,dcloud,mongo`. It runs `acceptance:poc-readiness`, probes the two D-Cloud FAT contracts from `droboticflow-dcloud-secret`, and only then launches `acceptance:poc-operators`. Readiness or D-Cloud probe failures emit a standard `DROBOTICFLOW_POC_OPERATOR_ACCEPTANCE=<json>` preflight-failure result and stop before the Kubernetes acceptance Job is created; the D-Cloud probe reports status, response shape, configured-header booleans, and redacted diagnostic fields such as upstream status/message/location, never secret values or business payloads. When `POC_ACCEPTANCE_IMPORT_RESULT=true` and `POC_ACCEPTANCE_IMPORT_API_BASE_URL`, `API_BASE_URL`, or `BASE_URL` is configured, those blocked preflights are imported into the `POC 算子验收` platform history exactly like Job results. Use narrower `POC_ACCEPTANCE_REQUIRED_EXTERNALS` only when intentionally running `acceptance:poc-operators` as dependency-isolation diagnostics, not as final strict evidence.
|
||||
`acceptance:poc-strict` refuses dependency-isolation scopes by requiring `POC_ACCEPTANCE_REQUIRED_EXTERNALS` to include `bos,dcloud,mongo`. It runs `acceptance:poc-readiness`, probes the two D-Cloud FAT contracts from `droboticflow-dcloud-secret`, and only then launches `acceptance:poc-operators`. Readiness or D-Cloud probe failures emit a standard `DROBOTICFLOW_POC_OPERATOR_ACCEPTANCE=<json>` preflight-failure result and stop before the Kubernetes acceptance Job is created; the D-Cloud probe reports status, response shape, configured-header booleans, and redacted diagnostic fields such as upstream status/message/location, never secret values or business payloads. With `POC_ACCEPTANCE_IMPORT_RESULT=true`, POC strict acceptance imports those blocked preflights into the `POC 算子验收` platform history exactly like Job results; it uses `POC_ACCEPTANCE_IMPORT_API_BASE_URL`, `API_BASE_URL`, or `BASE_URL` when provided, otherwise it discovers the `droboticflow-api-public` LoadBalancer URL and loads `DROBOTICFLOW_API_TOKEN` from `droboticflow-auth-secret`. Use narrower `POC_ACCEPTANCE_REQUIRED_EXTERNALS` only when intentionally running `acceptance:poc-operators` as dependency-isolation diagnostics, not as final strict evidence.
|
||||
|
||||
`scripts/poc-operator-full-acceptance.sh` loads the `poc` deployment environment on `dev`, reads the current `OPERATOR_RUNNER_IMAGE`, workspace PVC, Mongo secret, and BOS workspace secret references from Kubernetes, mounts `scripts/poc_operator_full_acceptance_job.py` into a temporary Job, and prints one `DROBOTICFLOW_POC_OPERATOR_ACCEPTANCE=<json>` result. The Job executes builtin modules from the deployed runner image and writes evidence under `/workspace/acceptance/<run-id>/`; `<run-id>` is set from `POC_OPERATOR_ACCEPTANCE_JOB_NAME` so the raw result, Kubernetes Job, imported platform run source, and workspace evidence directory stay traceable to the same acceptance execution. If the PVC is misconfigured and the runner cannot write there, the run fails by default instead of silently accepting temporary evidence. Set `POC_ACCEPTANCE_ALLOW_TMP_FALLBACK=true` only for diagnostic runs that intentionally write to `/tmp/droboticflow-acceptance/<run-id>/` and report `workspaceFallbackReason`.
|
||||
|
||||
|
||||
@ -7,8 +7,13 @@ export DEPLOY_ENV
|
||||
|
||||
# shellcheck source=scripts/load-deploy-env.sh
|
||||
source "$ROOT_DIR/scripts/load-deploy-env.sh"
|
||||
# shellcheck source=scripts/lib/service-endpoints.sh
|
||||
source "$ROOT_DIR/scripts/lib/service-endpoints.sh"
|
||||
|
||||
K8S_NAMESPACE="${K8S_NAMESPACE:-droboticflow}"
|
||||
API_PUBLIC_SERVICE_NAME="${API_PUBLIC_SERVICE_NAME:-droboticflow-api-public}"
|
||||
API_TOKEN_SECRET_NAME="${POC_ACCEPTANCE_API_TOKEN_SECRET_NAME:-droboticflow-auth-secret}"
|
||||
API_TOKEN_SECRET_KEY="${POC_ACCEPTANCE_API_TOKEN_SECRET_KEY:-DROBOTICFLOW_API_TOKEN}"
|
||||
POC_OPERATOR_ACCEPTANCE_JOB_NAME="${POC_OPERATOR_ACCEPTANCE_JOB_NAME:-droboticflow-strict-acceptance-$(date +%Y%m%d%H%M%S)}"
|
||||
POC_ACCEPTANCE_DCLOUD_SECRET_NAME="${POC_ACCEPTANCE_DCLOUD_SECRET_NAME:-droboticflow-dcloud-secret}"
|
||||
POC_ACCEPTANCE_REQUIRED_EXTERNALS="${POC_ACCEPTANCE_REQUIRED_EXTERNALS:-bos,dcloud,mongo}"
|
||||
@ -26,6 +31,7 @@ export POC_ACCEPTANCE_DCLOUD_SECRET_NAME
|
||||
export POC_ACCEPTANCE_REQUIRED_EXTERNALS
|
||||
export POC_ACCEPTANCE_IMPORT_RESULT
|
||||
export POC_ACCEPTANCE_IMPORT_API_BASE_URL
|
||||
export API_PUBLIC_SERVICE_NAME
|
||||
|
||||
required_external_enabled() {
|
||||
local target="$1"
|
||||
@ -44,6 +50,13 @@ required_external_enabled() {
|
||||
return 1
|
||||
}
|
||||
|
||||
bool_enabled() {
|
||||
local raw="$1"
|
||||
local normalized
|
||||
normalized="$(printf '%s' "$raw" | tr '[:upper:]' '[:lower:]')"
|
||||
[[ "$normalized" == "true" || "$normalized" == "1" || "$normalized" == "yes" || "$normalized" == "on" ]]
|
||||
}
|
||||
|
||||
validate_file() {
|
||||
local name="$1"
|
||||
local value="$2"
|
||||
@ -53,6 +66,92 @@ validate_file() {
|
||||
fi
|
||||
}
|
||||
|
||||
discover_import_api_base_url() {
|
||||
if [[ -n "${POC_ACCEPTANCE_IMPORT_API_BASE_URL:-}" || -n "${API_BASE_URL:-}" || -n "${BASE_URL:-}" ]]; then
|
||||
droboticflow_first_service_url "${POC_ACCEPTANCE_IMPORT_API_BASE_URL:-}" "${API_BASE_URL:-}" "${BASE_URL:-}"
|
||||
return
|
||||
fi
|
||||
|
||||
kubectl -n "$K8S_NAMESPACE" get svc "$API_PUBLIC_SERVICE_NAME" -o json 2>/dev/null | python3 -c '
|
||||
import ipaddress
|
||||
import json
|
||||
import sys
|
||||
|
||||
try:
|
||||
payload = json.load(sys.stdin)
|
||||
except json.JSONDecodeError:
|
||||
raise SystemExit(0)
|
||||
|
||||
items = payload.get("status", {}).get("loadBalancer", {}).get("ingress", [])
|
||||
hosts = [item.get("ip") or item.get("hostname") for item in items if isinstance(item, dict)]
|
||||
hosts = [item for item in hosts if item]
|
||||
if not hosts:
|
||||
raise SystemExit(0)
|
||||
|
||||
def is_private(host: str) -> bool:
|
||||
try:
|
||||
return ipaddress.ip_address(host).is_private
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
for host in hosts:
|
||||
if not is_private(host):
|
||||
print(f"http://{host}")
|
||||
raise SystemExit(0)
|
||||
print(f"http://{hosts[0]}")
|
||||
' 2>/dev/null || true
|
||||
}
|
||||
|
||||
load_import_api_token() {
|
||||
if [[ -n "${DROBOTICFLOW_API_TOKEN:-}" || -n "${DROBOTICFLOW_ACCEPTANCE_EMAIL:-}" ]]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
local api_token_secret_json
|
||||
local token
|
||||
api_token_secret_json="$(kubectl -n "$K8S_NAMESPACE" get secret "$API_TOKEN_SECRET_NAME" -o json 2>/dev/null || true)"
|
||||
token="$(
|
||||
printf "%s" "$api_token_secret_json" | API_TOKEN_SECRET_KEY="$API_TOKEN_SECRET_KEY" python3 -c '
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
try:
|
||||
payload = json.load(sys.stdin)
|
||||
except json.JSONDecodeError:
|
||||
raise SystemExit(0)
|
||||
data = payload.get("data", {})
|
||||
if not isinstance(data, dict):
|
||||
raise SystemExit(0)
|
||||
raw = data.get(os.environ["API_TOKEN_SECRET_KEY"])
|
||||
if not isinstance(raw, str) or not raw:
|
||||
raise SystemExit(0)
|
||||
try:
|
||||
print(base64.b64decode(raw, validate=True).decode("utf-8", errors="replace").strip(), end="")
|
||||
except Exception:
|
||||
raise SystemExit(0)
|
||||
'
|
||||
)"
|
||||
if [[ -n "$token" ]]; then
|
||||
export DROBOTICFLOW_API_TOKEN="$token"
|
||||
fi
|
||||
}
|
||||
|
||||
prepare_platform_import_defaults() {
|
||||
if ! bool_enabled "$POC_ACCEPTANCE_IMPORT_RESULT"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [[ -z "$POC_ACCEPTANCE_IMPORT_API_BASE_URL" ]]; then
|
||||
POC_ACCEPTANCE_IMPORT_API_BASE_URL="$(discover_import_api_base_url)"
|
||||
export POC_ACCEPTANCE_IMPORT_API_BASE_URL
|
||||
fi
|
||||
if [[ -n "$POC_ACCEPTANCE_IMPORT_API_BASE_URL" ]]; then
|
||||
load_import_api_token
|
||||
fi
|
||||
}
|
||||
|
||||
config_value() {
|
||||
local key="$1"
|
||||
kubectl -n "$K8S_NAMESPACE" get configmap droboticflow-config -o "jsonpath={.data.$key}" 2>/dev/null || true
|
||||
@ -74,13 +173,11 @@ validate_strict_scope() {
|
||||
|
||||
import_preflight_result_if_configured() {
|
||||
local result_file="$1"
|
||||
local import_result_normalized
|
||||
import_result_normalized="$(printf '%s' "$POC_ACCEPTANCE_IMPORT_RESULT" | tr '[:upper:]' '[:lower:]')"
|
||||
if [[ "$import_result_normalized" != "true" && "$import_result_normalized" != "1" && "$import_result_normalized" != "yes" && "$import_result_normalized" != "on" ]]; then
|
||||
if ! bool_enabled "$POC_ACCEPTANCE_IMPORT_RESULT"; then
|
||||
return 0
|
||||
fi
|
||||
if [[ -z "$POC_ACCEPTANCE_IMPORT_API_BASE_URL" ]]; then
|
||||
echo "POC strict preflight platform import skipped: set POC_ACCEPTANCE_IMPORT_API_BASE_URL, API_BASE_URL, or BASE_URL to enable it." >&2
|
||||
echo "POC strict preflight platform import skipped: no API URL was configured and ${K8S_NAMESPACE}/${API_PUBLIC_SERVICE_NAME} has no discoverable LoadBalancer ingress." >&2
|
||||
return 0
|
||||
fi
|
||||
|
||||
@ -142,6 +239,7 @@ validate_file POC_STRICT_OPERATOR_ACCEPTANCE_SCRIPT_PATH "$POC_STRICT_OPERATOR_A
|
||||
validate_file POC_STRICT_PREFLIGHT_RESULT_SCRIPT_PATH "$POC_STRICT_PREFLIGHT_RESULT_SCRIPT_PATH"
|
||||
validate_file POC_STRICT_IMPORT_SCRIPT_PATH "$POC_STRICT_IMPORT_SCRIPT_PATH"
|
||||
validate_strict_scope
|
||||
prepare_platform_import_defaults
|
||||
|
||||
run_preflight_or_exit "readiness" "readiness" "$POC_STRICT_READINESS_SCRIPT_PATH"
|
||||
run_preflight_or_exit \
|
||||
|
||||
@ -29,10 +29,20 @@ def run_script(
|
||||
operator = write_executable(tmp_path / "operator.sh", operator_content)
|
||||
env = os.environ.copy()
|
||||
for key in (
|
||||
"API_BASE_URL",
|
||||
"API_PUBLIC_SERVICE_NAME",
|
||||
"BASE_URL",
|
||||
"DEPLOY_ENV",
|
||||
"DROBOTICFLOW_ACCEPTANCE_EMAIL",
|
||||
"DROBOTICFLOW_ACCEPTANCE_PASSWORD",
|
||||
"DROBOTICFLOW_API_TOKEN",
|
||||
"DROBOTICFLOW_DEPLOY_ENV_LOADED",
|
||||
"K8S_NAMESPACE",
|
||||
"POC_ACCEPTANCE_API_TOKEN_SECRET_KEY",
|
||||
"POC_ACCEPTANCE_API_TOKEN_SECRET_NAME",
|
||||
"POC_ACCEPTANCE_DCLOUD_SECRET_NAME",
|
||||
"POC_ACCEPTANCE_IMPORT_API_BASE_URL",
|
||||
"POC_ACCEPTANCE_IMPORT_RESULT",
|
||||
"POC_ACCEPTANCE_REQUIRED_EXTERNALS",
|
||||
"POC_STRICT_DCLOUD_PROBE_SCRIPT_PATH",
|
||||
"POC_STRICT_OPERATOR_ACCEPTANCE_SCRIPT_PATH",
|
||||
@ -254,3 +264,69 @@ def test_strict_acceptance_imports_preflight_blocker_when_platform_url_is_config
|
||||
assert imported["preflightStage"] == "runner-image-readiness"
|
||||
assert imported["blockingMissingCoverage"]
|
||||
assert not (tmp_path / "operator-called").exists()
|
||||
|
||||
|
||||
def test_strict_acceptance_auto_discovers_platform_import_endpoint_and_token(tmp_path):
|
||||
fake_bin = tmp_path / "bin"
|
||||
fake_bin.mkdir()
|
||||
kubectl = fake_bin / "kubectl"
|
||||
kubectl.write_text(
|
||||
"#!/usr/bin/env python3\n"
|
||||
"import json\n"
|
||||
"import sys\n"
|
||||
"args = sys.argv[1:]\n"
|
||||
"if args == ['-n', 'droboticflow', 'get', 'svc', 'droboticflow-api-public', '-o', 'json']:\n"
|
||||
" print(json.dumps({'status': {'loadBalancer': {'ingress': [{'ip': '192.168.0.74'}, {'ip': '120.48.164.19'}]}}}))\n"
|
||||
" raise SystemExit(0)\n"
|
||||
"if args == ['-n', 'droboticflow', 'get', 'secret', 'droboticflow-auth-secret', '-o', 'json']:\n"
|
||||
" print(json.dumps({'data': {'DROBOTICFLOW_API_TOKEN': 'cG9jLXRva2Vu'}}))\n"
|
||||
" raise SystemExit(0)\n"
|
||||
"if args[:5] == ['-n', 'droboticflow', 'get', 'configmap', 'droboticflow-config']:\n"
|
||||
" print('registry.example.com/dataflow-poc/droboticflow-operator-runner:test')\n"
|
||||
" raise SystemExit(0)\n"
|
||||
"print('unexpected kubectl call: ' + ' '.join(args), file=sys.stderr)\n"
|
||||
"raise SystemExit(97)\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
kubectl.chmod(0o755)
|
||||
|
||||
import_script = tmp_path / "import.py"
|
||||
import_script.write_text(
|
||||
"import json, os, pathlib, sys\n"
|
||||
"payload = {\n"
|
||||
" 'apiBaseUrl': os.environ.get('API_BASE_URL'),\n"
|
||||
" 'apiToken': os.environ.get('DROBOTICFLOW_API_TOKEN'),\n"
|
||||
" 'argApiBaseUrl': sys.argv[sys.argv.index('--api-base-url') + 1],\n"
|
||||
"}\n"
|
||||
"pathlib.Path(os.environ['STRICT_TEST_TMP'], 'import-env.json').write_text(json.dumps(payload))\n"
|
||||
"print(json.dumps({'imported': True, 'runId': 'run-auto-preflight'}))\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
result = run_script(
|
||||
tmp_path,
|
||||
readiness_content=(
|
||||
"#!/usr/bin/env bash\n"
|
||||
"printf '%s\\n' '{\"status\":\"blocked\",\"checks\":["
|
||||
"{\"name\":\"dcloud-secret\",\"status\":\"failed\",\"required\":true,"
|
||||
"\"message\":\"D-Cloud FAT secret droboticflow/droboticflow-dcloud-secret is missing\","
|
||||
"\"evidence\":{\"secretName\":\"droboticflow-dcloud-secret\"}}]}'\n"
|
||||
"exit 1\n"
|
||||
),
|
||||
dcloud_probe_content="print('should not run')\n",
|
||||
operator_content="#!/usr/bin/env bash\ntouch \"$STRICT_TEST_TMP/operator-called\"\n",
|
||||
extra_env={
|
||||
"PATH": f"{fake_bin}{os.pathsep}{os.environ.get('PATH', '')}",
|
||||
"POC_STRICT_IMPORT_SCRIPT_PATH": str(import_script),
|
||||
},
|
||||
)
|
||||
|
||||
assert result.returncode == 1
|
||||
assert '{"imported": true, "runId": "run-auto-preflight"}' in result.stdout
|
||||
import_env = json.loads((tmp_path / "import-env.json").read_text(encoding="utf-8"))
|
||||
assert import_env == {
|
||||
"apiBaseUrl": "http://120.48.164.19",
|
||||
"apiToken": "poc-token",
|
||||
"argApiBaseUrl": "http://120.48.164.19",
|
||||
}
|
||||
assert not (tmp_path / "operator-called").exists()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user