From f4acde1b5037e4d6cf63d36eba4d8a29b00c1c37 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:24:47 +0700 Subject: [PATCH 1/3] kind-smoke: LIVE=1 mode against real Cloudflare Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01AuvXe8Sd1zPn6vNm5hzbdH --- hack/kind-smoke.sh | 118 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 100 insertions(+), 18 deletions(-) diff --git a/hack/kind-smoke.sh b/hack/kind-smoke.sh index e523ae4..a460a42 100755 --- a/hack/kind-smoke.sh +++ b/hack/kind-smoke.sh @@ -1,8 +1,8 @@ #!/usr/bin/env bash # Smoke test of agentops-operator, ai-agent-edge-tunnel and this client together on a kind cluster. # -# It needs no Cloudflare account: the edge operator gets a fake token, so everything that talks to -# Cloudflare (tunnel, DNS, Access application, connector, public URL) is NOT covered. It covers the +# By default it needs no Cloudflare account: the edge operator gets a fake token, so everything that +# talks to Cloudflare (tunnel, DNS, Access application, connector, public URL) is NOT covered. It covers the # cluster half: installs, CRDs, binding, sessionRef waiting and pinning, termination, RBAC, access-policy refusal # and the client's Expose/Complete. # @@ -11,6 +11,13 @@ # EDGE_DIR checkout of ai-agent-edge-tunnel (default ../ai-agent-edge-tunnel) # CLUSTER kind cluster name (default agentops-smoke) # Needs docker, kind, kubectl, kustomize and go on PATH. +# +# LIVE=1 runs the same flow against real Cloudflare instead (for CI on main only, never for pull +# requests: it uses credentials). It also needs CLOUDFLARE_API_TOKEN, CLOUDFLARE_ACCOUNT_ID, +# CLOUDFLARE_ZONE_ID and CLOUDFLARE_TEST_HOSTNAME (a full hostname inside the zone; its parent domain +# is used as the base domain). It then also covers the Access application, DNS record, connector, +# the public URL's Access gate, and verifies Cloudflare holds nothing afterwards. +# CONNECTOR_IMAGE overrides the cloudflared image. set -euo pipefail HERE="$(cd "$(dirname "$0")/.." && pwd)" @@ -22,6 +29,19 @@ OP_IMG=agentops-operator:ci EDGE_IMG=ai-agent-edge-tunnel:ci AGENT_IMG=echo-agent:ci +LIVE="${LIVE:-0}" +CLIENT_NAME=viaclient +if [ "$LIVE" = 1 ]; then + : "${CLOUDFLARE_API_TOKEN:?}" "${CLOUDFLARE_ACCOUNT_ID:?}" "${CLOUDFLARE_ZONE_ID:?}" "${CLOUDFLARE_TEST_HOSTNAME:?}" + BASE_DOMAIN="${CLOUDFLARE_TEST_HOSTNAME#*.}" + CONNECTOR_IMAGE="${CONNECTOR_IMAGE:-cloudflare/cloudflared:2024.10.0}" + CLIENT_NAME="ci-${GITHUB_RUN_ID:-$(date +%s)}" + HOST="$CLIENT_NAME.$BASE_DOMAIN" +else + BASE_DOMAIN=agents.example.test + CONNECTOR_IMAGE=cloudflare/cloudflared:2024.1.0 +fi + pass=0 step() { printf '\n== %s\n' "$*"; } ok() { printf 'PASS: %s\n' "$*"; pass=$((pass + 1)); } @@ -51,6 +71,24 @@ wait_field() { return 1 } +# cf PATH [curl args] calls the Cloudflare API with the test token. +cf() { local p="$1"; shift; curl -sS --max-time 30 -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" "$@" "https://api.cloudflare.com/client/v4/$p"; } + +# cf_dns_ids and cf_access_ids print the IDs of this run's DNS records and Access applications. +cf_dns_ids() { cf "zones/$CLOUDFLARE_ZONE_ID/dns_records?name=$HOST" | python3 -c 'import sys,json; print("\n".join(r["id"] for r in json.load(sys.stdin).get("result",[])))'; } +cf_access_ids() { cf "accounts/$CLOUDFLARE_ACCOUNT_ID/access/apps?per_page=100" | HOST="$HOST" python3 -c 'import sys,json,os; print("\n".join(a["id"] for a in json.load(sys.stdin).get("result",[]) if a.get("domain")==os.environ["HOST"]))'; } + +# live_cleanup runs on exit in LIVE mode. The operator removes its own Cloudflare resources when its +# objects are deleted; then anything still left for this run's hostname is deleted through the API +# and reported, so a failed run does not leave resources behind. +live_cleanup() { + set +e + kubectl -n "$NS" delete agentsession,agentedgetunnel --all --wait=true --timeout=120s >/dev/null 2>&1 + for id in $(cf_dns_ids); do echo "LEFTOVER dns record $id for $HOST: deleting" >&2; cf "zones/$CLOUDFLARE_ZONE_ID/dns_records/$id" -X DELETE >/dev/null; done + for id in $(cf_access_ids); do echo "LEFTOVER access application $id for $HOST: deleting" >&2; cf "accounts/$CLOUDFLARE_ACCOUNT_ID/access/apps/$id" -X DELETE >/dev/null; done +} +[ "$LIVE" = 1 ] && trap live_cleanup EXIT + step "cluster" if ! kind get clusters | grep -qx "$CLUSTER"; then kind create cluster --name "$CLUSTER" --wait 120s; fi kubectl cluster-info >/dev/null @@ -68,13 +106,14 @@ kubectl -n agentops-operator-system patch deploy agentops-operator-controller-ma kubectl -n agentops-operator-system rollout status deploy/agentops-operator-controller-manager --timeout=180s ok "agentops-operator is running" -step "install ai-agent-edge-tunnel (fake Cloudflare credentials)" +step "install ai-agent-edge-tunnel ($([ "$LIVE" = 1 ] && echo real || echo fake) Cloudflare credentials)" kubectl create namespace edge-tunnel-system --dry-run=client -o yaml | kubectl apply -f - kubectl -n edge-tunnel-system create secret generic edge-tunnel-cloudflare \ - --from-literal=account-id=smoke --from-literal=api-token=smoke --dry-run=client -o yaml | kubectl apply -f - + --from-literal=account-id="${CLOUDFLARE_ACCOUNT_ID:-smoke}" --from-literal=api-token="${CLOUDFLARE_API_TOKEN:-smoke}" \ + --dry-run=client -o yaml | kubectl apply -f - kubectl -n edge-tunnel-system create configmap edge-tunnel-config \ - --from-literal=EDGE_BASE_DOMAIN=agents.example.test \ - --from-literal=CONNECTOR_IMAGE=cloudflare/cloudflared:2024.1.0 --dry-run=client -o yaml | kubectl apply -f - + --from-literal=EDGE_BASE_DOMAIN="$BASE_DOMAIN" \ + --from-literal=CONNECTOR_IMAGE="$CONNECTOR_IMAGE" --dry-run=client -o yaml | kubectl apply -f - (cd "$EDGE_DIR" && make deploy IMG="$EDGE_IMG") kubectl -n edge-tunnel-system patch deploy edge-tunnel-controller-manager --type=json \ -p='[{"op":"add","path":"/spec/template/spec/containers/0/imagePullPolicy","value":"IfNotPresent"}]' @@ -162,22 +201,65 @@ wait_field early agentedgetunnel '{.status.phase}' Terminated 120 || fail "tunne ok "tunnel Terminated after session completion" step "client: Expose creates both objects, then Complete ends the session" -# Cloudflare is fake, so Wait cannot succeed: the example must time out, clean up and exit non-zero. -if (cd "$HERE" && go run ./examples/smoke -namespace "$NS" -name viaclient -pool smoke \ - -zone-id zone -email smoke@example.com -timeout 45s); then - fail "the smoke example succeeded although Cloudflare is fake" +if [ "$LIVE" = 1 ]; then + # Real Cloudflare: the exposure must become usable. -keep leaves the session running. + (cd "$HERE" && go run ./examples/smoke -namespace "$NS" -name "$CLIENT_NAME" -pool smoke \ + -zone-id "$CLOUDFLARE_ZONE_ID" -email smoke@example.com -timeout 6m -keep) || fail "Expose/Wait did not produce a usable endpoint" + [ "$(field "$CLIENT_NAME" agentedgetunnel '{.status.phase}')" = Active ] || fail "tunnel is not Active" + [ "$(field "$CLIENT_NAME" agentedgetunnel '{.status.connectorReady}')" = true ] || fail "connector is not ready" + [ "$(field "$CLIENT_NAME" agentedgetunnel '{.status.hostname}')" = "$HOST" ] || fail "unexpected hostname '$(field "$CLIENT_NAME" agentedgetunnel '{.status.hostname}')', wanted $HOST" + ok "tunnel Active with a connected connector at $HOST" + + step "Cloudflare holds the DNS record and Access application" + [ -n "$(cf_dns_ids)" ] || fail "no DNS record for $HOST" + [ -n "$(cf_access_ids)" ] || fail "no Access application for $HOST" + ok "DNS record and Access application exist" + + step "the public URL is behind the Access login" + end=$((SECONDS + 180)); code=""; loc="" + while [ "$SECONDS" -lt "$end" ]; do + hdr="$(curl -sS --max-time 20 -o /dev/null -D - "https://$HOST/" 2>&1 || true)" + code="$(printf '%s' "$hdr" | head -1 | awk '{print $2}')" + loc="$(printf '%s' "$hdr" | tr -d '\r' | awk 'tolower($1)=="location:"{print $2}')" + case "$code:$loc" in 3??:*cloudflareaccess.com*) break ;; esac + sleep 5 + done + case "$code:$loc" in + 3??:*cloudflareaccess.com*) ok "unauthenticated request is redirected to Cloudflare Access ($code)" ;; + *) fail "expected a redirect to Cloudflare Access, got status '$code' location '$loc'. Response: $hdr" ;; + esac + + step "completing the session removes everything from Cloudflare" + tunnel_id="$(field "$CLIENT_NAME" agentedgetunnel '{.status.tunnelID}')" + [ -n "$tunnel_id" ] || fail "tunnel has no tunnelID in status" + kubectl -n "$NS" patch agentsession "$CLIENT_NAME" --type merge -p '{"spec":{"completed":true}}' + wait_field "$CLIENT_NAME" agentedgetunnel '{.status.phase}' Terminated 180 || fail "tunnel did not terminate" + end=$((SECONDS + 120)) + until [ -z "$(cf_dns_ids)" ] && [ -z "$(cf_access_ids)" ]; do + [ "$SECONDS" -lt "$end" ] || fail "Cloudflare still holds a DNS record or Access application for $HOST" + sleep 3 + done + deleted="$(cf "accounts/$CLOUDFLARE_ACCOUNT_ID/cfd_tunnel/$tunnel_id" | python3 -c 'import sys,json; r=json.load(sys.stdin); print("yes" if (not r.get("success")) or r.get("result",{}).get("deleted_at") else "no")')" + [ "$deleted" = yes ] || fail "Cloudflare tunnel $tunnel_id still exists" + ok "DNS record, Access application and tunnel are gone" +else + # Cloudflare is fake, so Wait cannot succeed: the example must time out, clean up and exit non-zero. + if (cd "$HERE" && go run ./examples/smoke -namespace "$NS" -name "$CLIENT_NAME" -pool smoke \ + -zone-id zone -email smoke@example.com -timeout 45s); then + fail "the smoke example succeeded although Cloudflare is fake" + fi + owner="$(kubectl -n "$NS" get agentedgetunnel "$CLIENT_NAME" -o jsonpath='{.metadata.ownerReferences[0].kind}')" + [ "$owner" = AgentSession ] || fail "tunnel created by the client has no AgentSession ownerReference (got '$owner')" + [ "$(field "$CLIENT_NAME" agentsession '{.spec.completed}')" = true ] || fail "the client did not complete the session after Wait failed" + suid="$(field "$CLIENT_NAME" agentsession '{.metadata.uid}')" + wait_field "$CLIENT_NAME" agentedgetunnel '{.status.sessionUID}' "$suid" 60 || fail "client-created tunnel did not pin its session" + ok "client created session and owned tunnel, and completed the session after the failed wait" fi -owner="$(kubectl -n "$NS" get agentedgetunnel viaclient -o jsonpath='{.metadata.ownerReferences[0].kind}')" -[ "$owner" = AgentSession ] || fail "tunnel created by the client has no AgentSession ownerReference (got '$owner')" -[ "$(field viaclient agentsession '{.spec.completed}')" = true ] || fail "the client did not complete the session after Wait failed" -suid="$(field viaclient agentsession '{.metadata.uid}')" -wait_field viaclient agentedgetunnel '{.status.sessionUID}' "$suid" 60 || fail "client-created tunnel did not pin its session" -ok "client created session and owned tunnel, and completed the session after the failed wait" step "deleting the session deletes its tunnel" -kubectl -n "$NS" delete agentsession viaclient --wait=true +kubectl -n "$NS" delete agentsession "$CLIENT_NAME" --wait=true end=$((SECONDS + 60)) -while kubectl -n "$NS" get agentedgetunnel viaclient >/dev/null 2>&1; do +while kubectl -n "$NS" get agentedgetunnel "$CLIENT_NAME" >/dev/null 2>&1; do [ "$SECONDS" -lt "$end" ] || fail "tunnel outlived its deleted session" sleep 2 done From 3f5cadf3448249c7e46490c69de9fb8301d25a90 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:29:36 +0700 Subject: [PATCH 2/3] kind-smoke LIVE: Cloudflare lookups fail on API errors; paginate; verify cleanup deletes Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01AuvXe8Sd1zPn6vNm5hzbdH --- hack/__pycache__/cf-ids.cpython-313.pyc | Bin 0 -> 5193 bytes hack/cf-ids.py | 69 ++++++++++++++++++++++++ hack/kind-smoke.sh | 47 ++++++++++++---- 3 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 hack/__pycache__/cf-ids.cpython-313.pyc create mode 100755 hack/cf-ids.py diff --git a/hack/__pycache__/cf-ids.cpython-313.pyc b/hack/__pycache__/cf-ids.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4aada44a4d88623322b2515f805f55f529443ff2 GIT binary patch literal 5193 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 (body.get("result_info") or {}).get("total_pages", 1): + return out + page += 1 + + +def main(): + what = sys.argv[1] + if what == "dns": + for r in paged("zones/%s/dns_records?name=%s" % (os.environ["CLOUDFLARE_ZONE_ID"], os.environ["HOST"])): + print(r["id"]) + elif what == "access": + for a in paged("accounts/%s/access/apps" % os.environ["CLOUDFLARE_ACCOUNT_ID"]): + if a.get("domain") == os.environ["HOST"]: + print(a["id"]) + elif what == "tunnel": + body = call("accounts/%s/cfd_tunnel/%s" % (os.environ["CLOUDFLARE_ACCOUNT_ID"], sys.argv[2])) + if body.get("success") is True: + print("deleted" if (body.get("result") or {}).get("deleted_at") else "exists") + elif any("not found" in str(e.get("message", "")).lower() for e in body.get("errors", [])): + print("deleted") + else: + sys.exit("cloudflare tunnel lookup: %s" % json.dumps(body.get("errors", body))[:300]) + else: + sys.exit("usage: cf-ids.py dns|access|tunnel ID") + + +main() diff --git a/hack/kind-smoke.sh b/hack/kind-smoke.sh index a460a42..fb7e4c2 100755 --- a/hack/kind-smoke.sh +++ b/hack/kind-smoke.sh @@ -74,18 +74,39 @@ wait_field() { # cf PATH [curl args] calls the Cloudflare API with the test token. cf() { local p="$1"; shift; curl -sS --max-time 30 -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" "$@" "https://api.cloudflare.com/client/v4/$p"; } -# cf_dns_ids and cf_access_ids print the IDs of this run's DNS records and Access applications. -cf_dns_ids() { cf "zones/$CLOUDFLARE_ZONE_ID/dns_records?name=$HOST" | python3 -c 'import sys,json; print("\n".join(r["id"] for r in json.load(sys.stdin).get("result",[])))'; } -cf_access_ids() { cf "accounts/$CLOUDFLARE_ACCOUNT_ID/access/apps?per_page=100" | HOST="$HOST" python3 -c 'import sys,json,os; print("\n".join(a["id"] for a in json.load(sys.stdin).get("result",[]) if a.get("domain")==os.environ["HOST"]))'; } +# cf_dns_ids, cf_access_ids and cf_tunnel_state query Cloudflare through hack/cf-ids.py, which fails on +# any API error, so an error is never read as "nothing there". +export HOST CLOUDFLARE_API_TOKEN CLOUDFLARE_ACCOUNT_ID CLOUDFLARE_ZONE_ID +cf_dns_ids() { "$HERE/hack/cf-ids.py" dns; } +cf_access_ids() { "$HERE/hack/cf-ids.py" access; } +cf_tunnel_state() { "$HERE/hack/cf-ids.py" tunnel "$1"; } +# cf_gone succeeds only when both queries worked and found nothing. +cf_gone() { local d a; d="$(cf_dns_ids)" || return 1; a="$(cf_access_ids)" || return 1; [ -z "$d$a" ]; } +# cf_delete PATH deletes through the API and says so loudly if Cloudflare did not confirm. +cf_delete() { + if ! cf "$1" -X DELETE | python3 -c 'import sys,json; sys.exit(0 if json.load(sys.stdin).get("success") is True else 1)'; then + echo "FAILED to delete $1 in Cloudflare: remove it by hand" >&2 + fi +} + +TUNNEL_ID="" # live_cleanup runs on exit in LIVE mode. The operator removes its own Cloudflare resources when its -# objects are deleted; then anything still left for this run's hostname is deleted through the API -# and reported, so a failed run does not leave resources behind. +# objects are deleted; then anything still left for this run's hostname (and the tunnel, if its ID is +# known) is deleted through the API and reported. If a lookup or delete itself fails, it says so, and +# the resource may remain: nothing here can promise otherwise. live_cleanup() { set +e kubectl -n "$NS" delete agentsession,agentedgetunnel --all --wait=true --timeout=120s >/dev/null 2>&1 - for id in $(cf_dns_ids); do echo "LEFTOVER dns record $id for $HOST: deleting" >&2; cf "zones/$CLOUDFLARE_ZONE_ID/dns_records/$id" -X DELETE >/dev/null; done - for id in $(cf_access_ids); do echo "LEFTOVER access application $id for $HOST: deleting" >&2; cf "accounts/$CLOUDFLARE_ACCOUNT_ID/access/apps/$id" -X DELETE >/dev/null; done + local ids + ids="$(cf_dns_ids)" || echo "could not list DNS records for $HOST: check Cloudflare by hand" >&2 + for id in $ids; do echo "LEFTOVER dns record $id for $HOST: deleting" >&2; cf_delete "zones/$CLOUDFLARE_ZONE_ID/dns_records/$id"; done + ids="$(cf_access_ids)" || echo "could not list Access applications for $HOST: check Cloudflare by hand" >&2 + for id in $ids; do echo "LEFTOVER access application $id for $HOST: deleting" >&2; cf_delete "accounts/$CLOUDFLARE_ACCOUNT_ID/access/apps/$id"; done + if [ -n "$TUNNEL_ID" ]; then + state="$(cf_tunnel_state "$TUNNEL_ID")" || echo "could not look up tunnel $TUNNEL_ID: check Cloudflare by hand" >&2 + if [ "$state" = exists ]; then echo "LEFTOVER tunnel $TUNNEL_ID: deleting" >&2; cf_delete "accounts/$CLOUDFLARE_ACCOUNT_ID/cfd_tunnel/$TUNNEL_ID"; fi + fi } [ "$LIVE" = 1 ] && trap live_cleanup EXIT @@ -208,6 +229,7 @@ if [ "$LIVE" = 1 ]; then [ "$(field "$CLIENT_NAME" agentedgetunnel '{.status.phase}')" = Active ] || fail "tunnel is not Active" [ "$(field "$CLIENT_NAME" agentedgetunnel '{.status.connectorReady}')" = true ] || fail "connector is not ready" [ "$(field "$CLIENT_NAME" agentedgetunnel '{.status.hostname}')" = "$HOST" ] || fail "unexpected hostname '$(field "$CLIENT_NAME" agentedgetunnel '{.status.hostname}')', wanted $HOST" + TUNNEL_ID="$(field "$CLIENT_NAME" agentedgetunnel '{.status.tunnelID}')" ok "tunnel Active with a connected connector at $HOST" step "Cloudflare holds the DNS record and Access application" @@ -235,12 +257,17 @@ if [ "$LIVE" = 1 ]; then kubectl -n "$NS" patch agentsession "$CLIENT_NAME" --type merge -p '{"spec":{"completed":true}}' wait_field "$CLIENT_NAME" agentedgetunnel '{.status.phase}' Terminated 180 || fail "tunnel did not terminate" end=$((SECONDS + 120)) - until [ -z "$(cf_dns_ids)" ] && [ -z "$(cf_access_ids)" ]; do + until cf_gone; do [ "$SECONDS" -lt "$end" ] || fail "Cloudflare still holds a DNS record or Access application for $HOST" sleep 3 done - deleted="$(cf "accounts/$CLOUDFLARE_ACCOUNT_ID/cfd_tunnel/$tunnel_id" | python3 -c 'import sys,json; r=json.load(sys.stdin); print("yes" if (not r.get("success")) or r.get("result",{}).get("deleted_at") else "no")')" - [ "$deleted" = yes ] || fail "Cloudflare tunnel $tunnel_id still exists" + end=$((SECONDS + 60)); state="" + until [ "$state" = deleted ]; do + state="$(cf_tunnel_state "$tunnel_id")" || state="" + [ "$state" = deleted ] && break + [ "$SECONDS" -lt "$end" ] || fail "Cloudflare tunnel $tunnel_id is not confirmed deleted (state '$state')" + sleep 3 + done ok "DNS record, Access application and tunnel are gone" else # Cloudflare is fake, so Wait cannot succeed: the example must time out, clean up and exit non-zero. From f216c38fa39d55d68f655bf9cac110f5cd5e0202 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 20:33:58 +0700 Subject: [PATCH 3/3] cf-ids: only a 404 counts as tunnel not found; local state in cleanup Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_01AuvXe8Sd1zPn6vNm5hzbdH --- hack/__pycache__/cf-ids.cpython-313.pyc | Bin 5193 -> 5268 bytes hack/cf-ids.py | 2 +- hack/kind-smoke.sh | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hack/__pycache__/cf-ids.cpython-313.pyc b/hack/__pycache__/cf-ids.cpython-313.pyc index 4aada44a4d88623322b2515f805f55f529443ff2..04c19516393bf6f16b87b18fde27ee7fe065f1a6 100644 GIT binary patch delta 269 zcmX@9F-4R2GcPX}0}ve3c%QXxBX1Ws(+lRw51EuU@8+&xWN~Lq%JIKMH! z3{1BILpp<|?Bq!P%UngAK<$}RfW|J?5nwbm0`gumG%#Gyt-q8}cOf}tMfRTH1A@mz z4~ibBybu&}*)jC8ZP<0&n2WYCmu=&&>BRw+O^z2l%~&~ETWB%kmC5IYk_>O~3id~I zModV*%qzD*`7*ES8s&?+^%pX7u4fcn%qX}}Sb3qk_DV+GW!?JgTJ;yT>L>dP{{jH} CJynYU delta 232 zcmbQDc~XP-GcPX}0}$*`f1fpTBX1Ws(;nu@51EuU@8+&xWJ>(G*_@Y$Q7Bpvqy_|n z#iSTQnS*#I3-Smvicg-*m&hnF`758fxOgbDDHBAEDIF9j1(U`S`s zl%Cwme|d3|0Hd)1P~A(028Ig)Nte764<26A?prXms1y3`kO->eC%y@Y+yKs`+9bSQp0!b%K&Rd+cxDXO^Aui#Hcj9G% Qr0cv%7kQH=&l3Ix0HmHozyJUM diff --git a/hack/cf-ids.py b/hack/cf-ids.py index b025998..31914a8 100755 --- a/hack/cf-ids.py +++ b/hack/cf-ids.py @@ -58,7 +58,7 @@ def main(): body = call("accounts/%s/cfd_tunnel/%s" % (os.environ["CLOUDFLARE_ACCOUNT_ID"], sys.argv[2])) if body.get("success") is True: print("deleted" if (body.get("result") or {}).get("deleted_at") else "exists") - elif any("not found" in str(e.get("message", "")).lower() for e in body.get("errors", [])): + elif body.get("_http") == 404 and any("not found" in str(e.get("message", "")).lower() for e in body.get("errors", [])): print("deleted") else: sys.exit("cloudflare tunnel lookup: %s" % json.dumps(body.get("errors", body))[:300]) diff --git a/hack/kind-smoke.sh b/hack/kind-smoke.sh index fb7e4c2..0f24ff6 100755 --- a/hack/kind-smoke.sh +++ b/hack/kind-smoke.sh @@ -98,7 +98,7 @@ TUNNEL_ID="" live_cleanup() { set +e kubectl -n "$NS" delete agentsession,agentedgetunnel --all --wait=true --timeout=120s >/dev/null 2>&1 - local ids + local ids state ids="$(cf_dns_ids)" || echo "could not list DNS records for $HOST: check Cloudflare by hand" >&2 for id in $ids; do echo "LEFTOVER dns record $id for $HOST: deleting" >&2; cf_delete "zones/$CLOUDFLARE_ZONE_ID/dns_records/$id"; done ids="$(cf_access_ids)" || echo "could not list Access applications for $HOST: check Cloudflare by hand" >&2