Skip to content

Remove deleted resources from gateways that have not re-registered - #4466

Open
HiranyaKavishani wants to merge 1 commit into
wso2:mainfrom
HiranyaKavishani:operator-delete-after-restart
Open

HiranyaKavishani wants to merge 1 commit into
wso2:mainfrom
HiranyaKavishani:operator-delete-after-restart

Conversation

@HiranyaKavishani

Copy link
Copy Markdown
Contributor

Purpose

The gateway operator keeps its gateway registry (internal/registry/gateway_registry.go) only in memory. After an operator restart the registry is empty until each gateway reconciles and registers again. RestApi deletion (cleanupAPIDeployments) and generic-resource deletion (cleanupAllGateways) found their gateways only through registry.FindMatchingGateways, so a resource deleted in that window found no gateway, its finalizer was removed, and the API or resource stayed deployed on the gateway.

The gap was raised in review on #4446 and is fixed here separately so that PR stays focused.

Goals

  • A RestApi, Agent, ApiKey, Subscription, SubscriptionPlan, LLM provider/template/proxy, MCP proxy, ManagedSecret or Certificate deleted after an operator restart, before its gateway re-registers, is removed from that gateway rather than left deployed.
  • A failed cleanup keeps the finalizer and is retried.

Approach

  • Durable record. Before deploying, the RestApi controller and the generic reconciler record the target gateway on the CR in the gateway.api-platform.wso2.com/deployed-to-gateways annotation: a JSON object mapping namespace/name to the gateway kind (APIGateway or Gateway). This follows the deployed-gateways annotation HTTPRoute already uses. Writing it before the deploy call means a deploy that reaches the gateway is never left unrecorded. The patch uses an optimistic lock so a stale cache cannot drop an earlier entry.
  • Deletion. Cleanup targets are the recorded gateways plus the registered gateways that select the resource today, which still covers resources deployed before this change. For each target:
    • registered: delete as before.
    • not registered, but the APIGateway, or a Gateway API Gateway of a managed class, still exists: keep the finalizer and requeue (requeueGatewayNotRegistered) until it registers.
    • gateway object gone, or being deleted: nothing left to remove. A Gateway of a class the operator does not manage never registers, so it is not waited on.
    • Resources with no record delete as before.
  • Errors. Cleanup errors now keep the finalizer for RestApi too (they used to be logged and dropped). A 404 from the gateway counts as deleted, as the existing clients already do.
  • The record is left out of the annotations forwarded to the gateway in deploy payloads.
  • RestApiReconciler now calls the gateway REST API through the same restAPIClient interface as the HTTPRoute reconciler, so tests can stand in a fake.

Trade-off: deletion of a resource whose gateway object exists but never registers again (for example a gateway whose runtime stays broken) waits until the gateway registers or is deleted, the same as HTTPRoute deletion does today.

User stories

As a platform operator, when I delete an API or a management resource while the operator is restarting, it is removed from the gateway instead of staying live without a backing CR.

Documentation

N/A. Internal controller behaviour; no user-facing configuration changes.

Automation tests

  • Unit tests

    New tests in internal/controller/deployed_gateways_record_test.go:

    • RestApi and ManagedSecret (generic reconciler) deleted with an empty registry and an existing gateway keep the finalizer and requeue, then complete once the gateway registers (regression test; it fails against the old cleanup).
    • A cleanup error keeps the finalizer; the next pass completes.
    • A resource with no record deletes immediately.
    • A resource whose recorded gateway object is gone deletes.
    • A Gateway API Gateway of a managed class is waited on; one of another class is not.
    • Deploy records the gateway, and the record is not sent in the payload.

    GOWORK=off go test over every operator package except test/e2e, go vet ./... and gofmt -l all pass.

  • Integration tests

    None added.

Security checks

Database changes

  • I have applied the same table, column, and index changes to the API Platform SaaS (Bijira Cloud) databases in all relevant environments (dev, staging, production) and regions (US, EU).

Samples

N/A

Related PRs

Test environment

Go unit tests on macOS (darwin/arm64), GOWORK=off.

🤖 Generated with Claude Code

The gateway registry is held only in memory. After an operator restart it
is empty until each gateway reconciles again, and RestApi and generic
resource deletion found their gateways only through the registry. A
resource deleted in that window lost its finalizer while it stayed
deployed on the gateway.

Record every gateway a RestApi or generic resource is sent to in the
gateway.api-platform.wso2.com/deployed-to-gateways annotation, written
before the deploy call. On deletion, clean up the recorded gateways plus
the registered gateways that select the resource:

- registered: delete as before; errors now keep the finalizer (RestApi
  used to drop them) and a 404 counts as deleted
- not registered, but the APIGateway, or a Gateway of a managed class,
  still exists: keep the finalizer and requeue until it registers
- gateway object gone or being deleted: nothing left to remove

The record is not forwarded to the gateway in payload annotations.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

This review ran on the open-source allowance, not this organization's plan, because the pull request author doesn't have an assigned seat. Waiting won't change this — ask an organization admin to assign them a seat, or add seats in Billing if every seat is already assigned, then retry.

Next included review available in 46 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b59c7f9 and a906a6b.

Files selected for processing: 10

  • kubernetes/gateway-operator/internal/controller/agent_controller.go
  • kubernetes/gateway-operator/internal/controller/deployed_gateways_record.go
  • kubernetes/gateway-operator/internal/controller/deployed_gateways_record_test.go
  • kubernetes/gateway-operator/internal/controller/generic_reconciler.go
  • kubernetes/gateway-operator/internal/controller/llmprovider_controller.go
  • kubernetes/gateway-operator/internal/controller/llmprovidertemplate_controller.go
  • kubernetes/gateway-operator/internal/controller/llmproxy_controller.go
  • kubernetes/gateway-operator/internal/controller/managedsecret_controller.go
  • kubernetes/gateway-operator/internal/controller/mcp_controller.go
  • kubernetes/gateway-operator/internal/controller/restapi_controller.go
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@codecov-commenter

codecov-commenter commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.40%. Comparing base (6ee2573) to head (a906a6b).
⚠️ Report is 21 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4466      +/-   ##
==========================================
- Coverage   68.10%   67.40%   -0.71%     
==========================================
  Files        1361     1346      -15     
  Lines      210436   219052    +8616     
  Branches     6750     6750              
==========================================
+ Hits       143326   147645    +4319     
- Misses      58010    62170    +4160     
- Partials     9100     9237     +137     
Flag Coverage Δ
api-portal-server-it 81.50% <ø> (+0.01%) ⬆️
gateway-controller-integration 55.01% <ø> (-0.15%) ⬇️
gateway-controller-unit 58.82% <ø> (+0.01%) ⬆️
platform-api-integration 50.86% <ø> (-0.07%) ⬇️
platform-api-unit 50.44% <ø> (ø)
policy-engine-integration 39.59% <ø> (+0.94%) ⬆️
policy-engine-unit 64.64% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants