Skip to content

fix(k8s): tunnel sync and url setup still overwrite settings they should keep #794

Closed

Description

@dan-petty

Context & Rationale

Since ed6428f (#676), cloudflare tunnel sync reads the current tunnel config and merges into it. It still loses settings in four cases, all reproduced on release/v0.2.24:

  • Per-route settings are dropped. Routes it keeps lose their own originRequest settings, such as noTLSVerify and httpHostHeader, because the route model keeps only service, hostname and path (cloudflare/client.py:84-102, :507-514).
  • The catch-all is replaced. A custom catch-all becomes http_status:404 (cloudflare/client.py:148).
  • A failed read wipes the config. If reading the current config fails, the error is swallowed (cloudflare/client.py:609-610) and sync falls back to replacing the whole tunnel config. That wipes warp-routing and every route it didn't generate.
  • --dry-run hides removals. It shows additions and preserved routes but no removals or replaced services (commands/cloudflare.py:502-525, :590-598). After a read error it silently shows additions only.

test_cloudflare_client_sync_tunnel_routes_preserves_rules checks only the mocked return value, so it passes on the old replace-all code.

URL setup has the same overwrite problem. Gateway discovery still matches the bare substrings "ai" and "llm-gateway" instead of CONST_K8S_INGRESS_SERVICE_PATTERNS (config/constants.py:2176, unused). As a result, a domain containing "ai" picks whichever ingress comes first. _should_update_url (commands/k8s/networking.py:278) also lets a discovered FQDN overwrite a custom remote ai.gateway_url.

The deleted-tunnel filter and the valkey host and port guard from #666 work, but have no tests. Found while checking the #666 fix.

Scope & Deliverables

  • Deliverable:
    • Sync keeps every field of the rules it preserves and keeps a custom catch-all.
    • When the current config can't be read, sync stops with an error instead of replacing it.
    • --dry-run shows additions, removals and replaced services from the merged result.
    • Gateway discovery uses the service patterns with exact service and namespace matches, and only unset or local URLs are updated.
    • Tests assert the payload sync sends, including originRequest, the catch-all and warp-routing. They also cover the read failure, deleted tunnels and the valkey guard.
  • Constraint: sync never sends a config that drops a route it didn't generate.

Strategic Alignment

  • Milestone: Backlog (no release)
  • Design: none
  • Depends on: none

Generated by Claude Code

Update 2026-10-08: refinement

The defects hold; #897 landed. The dry-run deliverable conflicts with #412: --dry-run makes no request and prints the GET and PUT it would send; a removal preview needs a separate reading mode, out of scope.

  • The PUT body keeps originRequest, a custom catch-all and warp-routing.
  • A failed GET sends no PUT; --dry-run makes no request.
  • A remote ai.gateway_url of https://example.com/v1 is kept.

Proposed design

Problem statement

Cloudflare tunnel sync and Kubernetes URL setup overwrite settings they should preserve. Tunnel sync drops per-route originRequest fields (noTLSVerify, httpHostHeader), replaces a custom catch-all with http_status:404, and when the GET of the current config fails it silently falls back to a full replace that wipes warp-routing and user-added routes. URL setup matches ingress services by bare substrings ("ai", "llm-gateway") instead of the defined CONST_K8S_INGRESS_SERVICE_PATTERNS, and _should_update_url lets a discovered FQDN overwrite a user-set remote ai.gateway_url. The fix must make sync preserve every field of routes it keeps, abort on a failed read, and make URL setup respect existing remote values.

Acceptance criteria

  • Criterion 1: The PUT body sent by tunnel sync preserves originRequest settings (noTLSVerify, httpHostHeader) on every route it keeps, preserves a custom catch-all rule, and preserves warp-routing.
    Verification: A unit test asserts the exact PUT payload: for a pre-existing route with originRequest.noTLSVerify=true and originRequest.httpHostHeader set, the payload contains those fields; a custom catch-all (e.g. service pointing to a custom backend) is present unchanged; warp-routing block is present unchanged.
  • Criterion 2: When the GET of the current tunnel config fails, sync raises an error and sends no PUT request.
    Verification: A unit test mocks the GET to raise an exception and asserts that the PUT method is never called and that the command exits with a non-zero status and an error message.
  • Criterion 3: --dry-run makes no network request (neither GET nor PUT) and prints the GET and PUT it would send.
    Verification: A unit test mocks the HTTP client, runs sync with --dry-run, and asserts zero calls to the HTTP client while stdout contains the serialized GET and PUT payloads.
  • Criterion 4: Gateway discovery matches ingress services using CONST_K8S_INGRESS_SERVICE_PATTERNS with exact service-name and namespace matches, not bare substring matching.
    Verification: A unit test provides two ingresses, one with service name containing 'ai' as a substring but not matching the pattern, and one matching the pattern exactly; only the latter is selected.
  • Criterion 5: _should_update_url does not overwrite a remote custom ai.gateway_url (e.g. https://example.com/v1) with a discovered FQDN.
    Verification: A unit test sets ai.gateway_url to https://example.com/v1, runs URL setup with a discovered FQDN, and asserts the setting remains https://example.com/v1.
  • Criterion 6: Tests cover the deleted-tunnel filter and the valkey host-and-port guard from fix(k8s): cloudflare sync and url configuration overwrite existing settings #666.
    Verification: Unit tests assert that a tunnel whose name matches a deleted-tunnel pattern is excluded from the sync payload, and that a valkey URL with a non-default port is not overwritten.

Key questions

  • What should happen when the GET of the current tunnel config fails?: Sync stops with an error and sends no PUT; it must not fall back to replacing the whole tunnel config. (proposed)
  • Should --dry-run show removals and replaced services?: No. Per the 2026-10-08 update, --dry-run makes no request and prints the GET and PUT it would send; a removal preview needs a separate reading mode and is out of scope (conflicts with feat(cli): reachable --dry-run previews for delegated commands #412). (proposed)
  • How should gateway discovery match ingress services?: Use CONST_K8S_INGRESS_SERVICE_PATTERNS with exact service-name and namespace matches instead of bare substring matching on 'ai' and 'llm-gateway'. (proposed) [sources: src/devops_cli/config/constants.py:2776]
  • When should _should_update_url allow overwriting an existing URL?: Only when the existing URL is unset or is a local (cluster-internal) address. A remote custom URL such as https://example.com/v1 must be kept. (proposed) [sources: src/devops_cli/commands/k8s/networking.py:285]

Open questions

  • What fields must the route model preserve when keeping an existing route? (missing verified source)
  • Does the existing test test_cloudflare_client_sync_tunnel_routes_preserves_rules adequately verify the fix? (missing verified source)

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.
[end-marker]

Proposed design

Problem statement

Cloudflare tunnel sync and Kubernetes URL setup overwrite settings they should preserve. Tunnel sync drops per-route originRequest fields (noTLSVerify, httpHostHeader), replaces a custom catch-all with http_status:404, and when the GET of the current config fails it silently falls back to a full replace that wipes warp-routing and user-added routes. URL setup matches ingress services by bare substrings ("ai", "llm-gateway") instead of the defined CONST_K8S_INGRESS_SERVICE_PATTERNS, and _should_update_url lets a discovered FQDN overwrite a user-set remote ai.gateway_url. The fix must make sync preserve every field of routes it keeps, abort on a failed read, and make URL setup respect existing remote values.

Acceptance criteria

  • Criterion 1: The PUT body sent by tunnel sync preserves originRequest settings (noTLSVerify, httpHostHeader) on every route it keeps, preserves a custom catch-all rule, and preserves warp-routing.
    Verification: A unit test asserts the exact PUT payload: for a pre-existing route with originRequest.noTLSVerify=true and originRequest.httpHostHeader set, the payload contains those fields; a custom catch-all (e.g. service pointing to a custom backend) is present unchanged; warp-routing block is present unchanged.
  • Criterion 2: When the GET of the current tunnel config fails, sync raises an error and sends no PUT request.
    Verification: A unit test mocks the GET to raise an exception and asserts that the PUT method is never called and that the command exits with a non-zero status and an error message.
  • Criterion 3: --dry-run makes no network request (neither GET nor PUT) and prints the GET and PUT it would send.
    Verification: A unit test mocks the HTTP client, runs sync with --dry-run, and asserts zero calls to the HTTP client while stdout contains the serialized GET and PUT payloads.
  • Criterion 4: Gateway discovery matches ingress services using CONST_K8S_INGRESS_SERVICE_PATTERNS with exact service-name and namespace matches, not bare substring matching.
    Verification: A unit test provides two ingresses, one with service name containing 'ai' as a substring but not matching the pattern, and one matching the pattern exactly; only the latter is selected.
  • Criterion 5: _should_update_url does not overwrite a remote custom ai.gateway_url (e.g. https://example.com/v1) with a discovered FQDN.
    Verification: A unit test sets ai.gateway_url to https://example.com/v1, runs URL setup with a discovered FQDN, and asserts the setting remains https://example.com/v1.
  • Criterion 6: Tests cover the deleted-tunnel filter and the valkey host-and-port guard from fix(k8s): cloudflare sync and url configuration overwrite existing settings #666.
    Verification: Unit tests assert that a tunnel whose name matches a deleted-tunnel pattern is excluded from the sync payload, and that a valkey URL with a non-default port is not overwritten.

Key questions

  • What should happen when the GET of the current tunnel config fails?: Sync stops with an error and sends no PUT; it must not fall back to replacing the whole tunnel config. (proposed) [sources: src/devops_cli/cloudflare/client.py:609]
  • Should --dry-run show removals and replaced services?: No. Per the 2026-10-08 update, --dry-run makes no request and prints the GET and PUT it would send; a removal preview needs a separate reading mode and is out of scope (conflicts with feat(cli): reachable --dry-run previews for delegated commands #412). (proposed) [sources: src/devops_cli/commands/cloudflare.py:502]
  • How should gateway discovery match ingress services?: Use CONST_K8S_INGRESS_SERVICE_PATTERNS with exact service-name and namespace matches instead of bare substring matching on 'ai' and 'llm-gateway'. (proposed) [sources: src/devops_cli/config/constants.py:2786]
  • When should _should_update_url allow overwriting an existing URL?: Only when the existing URL is unset or is a local (cluster-internal) address. A remote custom URL such as https://example.com/v1 must be kept. (proposed) [sources: src/devops_cli/commands/k8s/networking.py:285]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.
[end-marker]

Activity

  1. added
    type/bug
    scope/k8s
    priority/p1-high
    on Oct 1, 2026
  2. added this to the v0.2.28 milestone on Oct 1, 2026
  3. dan-petty commented on Oct 1, 2026

    @dan-petty
    OwnerAuthor

    Placement (2026-10-01): v0.2.28. Value High, Effort Medium. Not Ready yet.

    Checked against release/v0.2.24 at d38251e. What stops it being Ready:

    • No explicit acceptance-criteria section. The deliverables are testable outcomes but name no tests or verification commands.
    • Stale reference: config/constants.py:2176 is now :2368.
    • Optional: decide whether to split it into a tunnel-sync PR and a URL-discovery PR. It is two modules, but small enough for one PR.
  4. dan-petty commented on Oct 3, 2026

    @dan-petty
    OwnerAuthor

    Consolidation note (2026-10-03): #818 is merged into #1104, which rewrites the Prometheus lookups in commands/k8s/networking.py. Whichever of this item and #1104 lands first moves configure-urls onto exact (key, namespace, service) entries from CONST_K8S_INGRESS_SERVICE_PATTERNS (config/constants.py:2974, unread today), and the other reuses that table. This item's scope and v0.2.28 placement are unchanged.


    Lines cited at dcafadf.

    Other items rewrite the lines this item's URL-setup work touches in commands/k8s/networking.py. They land on that file in this order: #897 (v0.2.27), this item, #887, then #1112, #1111 and #1113.

    Local or remote URL

    Valkey guard

  5. removed this from the v0.2.28 milestone on Oct 6, 2026
  6. dan-petty-bot commented on Oct 6, 2026

    @dan-petty-bot
    Collaborator

    Moved to the backlog: it was New, not Ready, when v0.2.28 started. (devops roadmap reprioritize)

  7. added this to the v0.2.34 milestone on Oct 9, 2026
  8. dan-petty-bot commented on Oct 10, 2026

    @dan-petty-bot
    Collaborator

    Status set to Ready at 5c5df79 (see proposed design in issue body).

  9. dan-petty commented on Oct 11, 2026

    @dan-petty
    OwnerAuthor

    Merged

  10. added a commit that references this issue on Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority/p1-highscope/k8stype/bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions