Skip to content

[TT-16455] Add loaded API and policy IDs to MDCB node registration - #7695

Merged
mativm02 merged 3 commits into
masterfrom
TT-16455
Jan 27, 2026
Merged

[TT-16455] Add loaded API and policy IDs to MDCB node registration #7695
mativm02 merged 3 commits into
masterfrom
TT-16455

Conversation

@mativm02

@mativm02 mativm02 commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

Description

Extends the gateway's node registration data sent to MDCB to include the IDs of currently loaded APIs and policies.

Changes:

  • Add LoadedAPIInfo and LoadedPolicyInfo structs to apidef/rpc.go
  • Extend GWStats with loaded_apis and loaded_policies fields (omitempty for backward
    compatibility)
  • Add GetLoadedAPIIDs() and GetLoadedPolicyIDs() helper methods to Gateway
  • Update buildNodeInfo() to populate loaded resource IDs

This enables MDCB operators to see exactly which APIs and policies each gateway node has loaded via the /dataplanes endpoint.

Related Issue

https://tyktech.atlassian.net/browse/TT-16455?atlOrigin=eyJpIjoiZTI2ODVmZGU2ZjE5NDkxY2FiMTIzZTQzYzAzMzYwMDQiLCJwIjoiaiJ9

Motivation and Context

https://tyktech.atlassian.net/browse/TT-16455?atlOrigin=eyJpIjoiZTI2ODVmZGU2ZjE5NDkxY2FiMTIzZTQzYzAzMzYwMDQiLCJwIjoiaiJ9

How This Has Been Tested

Manually tested, and automated tests were added

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • I ensured that the documentation is up to date
  • I explained why this PR updates go.mod in detail with reasoning why it's required
  • I would like a code coverage CI quality gate exception and have explained why

Ticket Details

TT-16455
Status In Dev
Summary MDCB Dataplane Loaded APIs/Policies

Generated at: 2026-01-27 11:56:44

This commit introduces unit tests for the JSON serialization of LoadedAPIInfo and LoadedPolicyInfo structs, ensuring that they correctly marshal and unmarshal to and from JSON format. Additionally, it updates the GWStats struct to include LoadedAPIs and LoadedPolicies, enhancing the reporting of loaded resources. The tests verify that empty loaded resources are omitted from the JSON output when applicable.

- Added tests for LoadedAPIInfo and LoadedPolicyInfo serialization.
- Updated GWStats to include LoadedAPIs and LoadedPolicies.
- Ensured proper omission of empty fields in JSON output.
@probelabs

probelabs Bot commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

This PR enhances the gateway's node registration payload sent to the Multi-Datacenter Bridge (MDCB) by including the specific IDs of all loaded APIs and policies. This provides operators with detailed visibility into the configuration state of each gateway node, showing exactly which resources are active.

Files Changed Analysis

The changes are logically structured across three main areas:

  • apidef/: The RPC data contract is updated in rpc.go by introducing LoadedAPIInfo and LoadedPolicyInfo structs and adding them as omitempty slices to the GWStats struct. New tests in rpc_test.go validate the JSON serialization.
  • gateway/: The Gateway struct gains two new methods, GetLoadedAPIIDs() and GetLoadedPolicyIDs(), to retrieve the currently loaded resource IDs. The rpc_storage_handler.go integrates these methods into buildNodeInfo() to populate the registration payload. Corresponding tests have been added and updated to ensure correctness.
  • internal/model/: Type aliases for the new apidef structs are created in rpc.go for internal use, with new tests in rpc_test.go to verify them.

Overall, the changes are well-tested, with 298 additions (mostly tests) versus only 4 deletions, covering data structures, business logic, and payload construction.

Architecture & Impact Assessment

  • What this PR accomplishes: It enriches the data plane observability by reporting exactly which APIs and policies are loaded on each gateway. This allows MDCB to act as a more accurate source of truth for the live state of the gateways.

  • Key technical changes introduced:

    1. The GWStats data structure, part of the RPC communication protocol between the gateway and MDCB, has been extended.
    2. New public methods on the Gateway service expose the IDs of loaded APIs and policies.
    3. The node registration payload constructed by RPCStorageHandler is now populated with this new information.
  • Affected system components: The change primarily affects the Tyk Gateway (as the data producer) and MDCB (as the data consumer). The use of omitempty ensures backward compatibility, so older MDCB versions will not break and will simply ignore the new fields.

Data Flow Diagram

sequenceDiagram
    participant Operator
    participant MDCB
    participant Gateway

    Gateway->>Gateway: buildNodeInfo()
    Note right of Gateway: Collects node stats, including <br> a list of loaded API IDs <br> and Policy IDs.
    Gateway->>MDCB: Register/Heartbeat with extended NodeData
    MDCB->>MDCB: Store enriched node information

    Operator->>MDCB: GET /dataplanes
    MDCB-->>Operator: Return node data, including loaded APIs/Policies
Loading

Scope Discovery & Context Expansion

  • The impact of this PR is confined to the gateway's reporting and registration mechanism with MDCB. It does not alter core proxying behavior. The full benefit is unlocked when the corresponding MDCB service is updated to parse, store, and expose these new loaded_apis and loaded_policies fields.
  • To fully understand the end-to-end impact, one would need to examine the MDCB codebase to see how it processes the NodeData payload from gateways and how that data is exposed via its management API (e.g., /dataplanes).
  • The function buildNodeInfo in gateway/rpc_storage_handler.go is central to this change and is called during the gateway's registration and periodic heartbeat process with MDCB.
Metadata
  • Review Effort: 2 / 5
  • Primary Label: feature

Powered by Visor from Probelabs

Last updated: 2026-01-27T11:58:09.455Z | Triggered by: pr_updated | Commit: d1ba70b

💡 TIP: You can chat with Visor using /visor ask <your question>

@github-actions

github-actions Bot commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

API Changes

--- prev.txt	2026-01-27 11:57:25.654272260 +0000
+++ current.txt	2026-01-27 11:57:16.036310320 +0000
@@ -574,8 +574,10 @@
     https://tyk.io/docs/basic-config-and-security/security/authentication-authorization/ext-oauth-middleware/.
 
 type GWStats struct {
-	APIsCount     int `json:"apis_count"`
-	PoliciesCount int `json:"policies_count"`
+	APIsCount      int                `json:"apis_count"`
+	PoliciesCount  int                `json:"policies_count"`
+	LoadedAPIs     []LoadedAPIInfo    `json:"loaded_apis,omitempty"`
+	LoadedPolicies []LoadedPolicyInfo `json:"loaded_policies,omitempty"`
 }
 
 type GlobalRateLimit struct {
@@ -946,6 +948,16 @@
 	Values []string
 }
 
+type LoadedAPIInfo struct {
+	APIID string `json:"api_id"`
+}
+    LoadedAPIInfo represents a loaded API with its metadata.
+
+type LoadedPolicyInfo struct {
+	PolicyID string `json:"policy_id"`
+}
+    LoadedPolicyInfo represents a loaded policy with its metadata.
+
 type LogEventHandlerConf struct {
 	// Disabled indicates whether the handler is inactive.
 	Disabled bool `bson:"disabled" json:"disabled"`
@@ -10198,6 +10210,14 @@
 
 func (gw *Gateway) GetConfig() config.Config
 
+func (gw *Gateway) GetLoadedAPIIDs() []model.LoadedAPIInfo
+    GetLoadedAPIIDs returns a list of all loaded API IDs with metadata. This is
+    used for reporting loaded resources to MDCB.
+
+func (gw *Gateway) GetLoadedPolicyIDs() []model.LoadedPolicyInfo
+    GetLoadedPolicyIDs returns a list of all loaded policy IDs with metadata.
+    This is used for reporting loaded resources to MDCB.
+
 func (gw *Gateway) GetNodeID() string
     GetNodeID reads NodeID safely.
 

@probelabs

probelabs Bot commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

Security Issues (2)

Severity Location Issue
🟢 Info gateway/rpc_storage_handler.go:183-184
The node registration data sent to MDCB now includes a complete list of loaded API and Policy IDs. This increases the sensitivity of the data transmitted between the Gateway and MDCB, as it provides a detailed view of the gateway's configuration.
💡 SuggestionEnsure that the communication channel between the Gateway and MDCB is encrypted (e.g., using mTLS) and that access to MDCB data is strictly controlled to prevent unauthorized disclosure of the gateway's configuration. This change elevates the importance of securing the control plane communication.
🟢 Info gateway/gateway.go:24-45
The methods `GetLoadedAPIIDs` and `GetLoadedPolicyIDs` are called on each node registration heartbeat. For gateways with an extremely large number of APIs or policies, iterating over the maps and allocating new slices on every call could introduce performance overhead and increase memory pressure.
💡 SuggestionFor deployments with an exceptionally high number of APIs/policies per gateway, the performance impact of this data collection should be monitored. While the implementation is efficient for typical use cases, consider potential future optimizations if this becomes a bottleneck at extreme scale.

✅ Architecture Check Passed

No architecture issues found – changes LGTM.

Performance Issues (2)

Severity Location Issue
🟢 Info gateway/gateway.go:27-36
The read lock on `gw.apisMu` is held for the entire duration of the loop that populates the `apis` slice. For a large number of APIs, this can increase lock contention and potentially delay write operations that require an exclusive lock.
💡 SuggestionTo minimize lock duration, copy the API IDs to a temporary slice under the lock, release the lock, and then construct the final slice of `LoadedAPIInfo` structs from the temporary slice. This ensures the lock is held for the shortest time necessary.
🟢 Info gateway/gateway.go:41-50
The read lock on `gw.policiesMu` is held for the entire duration of the loop that populates the `policies` slice. For a large number of policies, this can increase lock contention and potentially delay write operations that require an exclusive lock.
💡 SuggestionTo minimize lock duration, copy the policy IDs to a temporary slice under the lock, release the lock, and then construct the final slice of `LoadedPolicyInfo` structs from the temporary slice. This ensures the lock is held for the shortest time necessary.

Quality Issues (1)

Severity Location Issue
🟢 Info gateway/gateway_test.go:2031-2042
The test case named "returns correct struct type" is slightly redundant, and its name could be clearer. It validates behavior with a single item, a scenario already covered by the more comprehensive multi-item test case ("returns loaded API IDs"). The name is also misleading as it asserts a value, not a type. A similar redundant test exists for policies starting at line 2077.
💡 SuggestionTo improve clarity and reduce redundancy, consider removing this test case as its logic is covered by the multi-item test. If kept, renaming it to "returns correct ID for a single loaded API" would more accurately describe its function.

Powered by Visor from Probelabs

Last updated: 2026-01-27T11:58:12.929Z | Triggered by: pr_updated | Commit: d1ba70b

💡 TIP: You can chat with Visor using /visor ask <your question>

@mativm02 mativm02 changed the title Add loaded API and policy IDs to MDCB node registration [TT-16455] Add loaded API and policy IDs to MDCB node registration Jan 26, 2026
This commit updates the comments for the LoadedAPIInfo and LoadedPolicyInfo structs to clarify their purpose and remove references to future extensibility regarding checksums. The changes enhance the readability and maintainability of the code without altering any functionality.
@mativm02
mativm02 enabled auto-merge (squash) January 27, 2026 11:56
@sonarqubecloud

Copy link
Copy Markdown

@mativm02
mativm02 merged commit a007bb9 into master Jan 27, 2026
53 of 55 checks passed
@mativm02
mativm02 deleted the TT-16455 branch January 27, 2026 13:15
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.

3 participants