Skip to content

Fix Consul API Client Service Transaction Unmarshall Error#22246

Closed
jzou-rbx wants to merge 2 commits into
hashicorp:mainfrom
jzou-rbx:txn_api_unmarshall
Closed

Fix Consul API Client Service Transaction Unmarshall Error#22246
jzou-rbx wants to merge 2 commits into
hashicorp:mainfrom
jzou-rbx:txn_api_unmarshall

Conversation

@jzou-rbx
Copy link
Copy Markdown

@jzou-rbx jzou-rbx commented Mar 28, 2025

Description

This PR fixes the Golang Consul API Client Library Unmarshalling error when using the Transaction API with Service that has TaggedAddresses, see #22180 (comment).

Below is a summary of the current Service Transaction Workflow when using the Golang Consul API Client Library:

  1. The Txn client specifies Service information to update as an api.AgentService in an api.ServiceTxnOp to submit to the Agent Txn endpoint.

  2. In the Agent Txn endpoint, it converts the api.AgentService to a structs.NodeService
    that is used internally for applying the transaction.

  3. After the transaction is applied, the structs.TxnResponse that is returned to the client contains the Service information as a structs.NodeService in the structs.TxnServiceResult.

  4. The Txn client tries to decode the returned response as an api.TxnResponse which uses api.CatalogService for the Service TxnResult.

  5. When a Service has TaggedAddresses, the decoding fails with json: cannot unmarshal object into Go struct field CatalogService.Results.Service.TaggedAddresses of type string. This is because structs.NodeService has TaggedAddresses as a map[string]structs.ServiceAddress, while api.CatalogService has TaggedAddresses as a map[string]string which is intended to be the node's TaggedAddresses and the Service's TaggedAddresses` is in ServiceTaggedAddresses field.

The proposed fix here is to change the Golang Consul Client Library to use api.AgentService for the Service TxnResult which has fields that are compatible for conversion from structs.NodeService
returned from the agent endpoint.

This change will require applications that use the Golang Consul Client Library for Service Transactions to do a small code change once they choose to upgrade the library to the new release (they will get notified with compile errors). Considering that any such clients are already in a broken state (if there are any such users besides us), it should be fine ok require such a change. api.AgentService appears to be the more appropriate Service TxnResult type, it matches the Txn input type and it is the return type for other endpoints such as health/service.

If we do still want to maintain api.CatalogService as the Service TxnResult type, then we could just add a best-effort conversion from api.AgentService to api.CatalogService before returning in the client library.

PR Checklist

  • updated test coverage
  • not a security concern

@github-actions github-actions Bot added the theme/api Relating to the HTTP API interface label Mar 28, 2025
@jzou-rbx jzou-rbx marked this pull request as ready for review March 29, 2025 00:16
@jzou-rbx jzou-rbx requested a review from a team as a code owner March 29, 2025 00:16
@jzou-rbx jzou-rbx changed the title Txn api unmarshall Fix API Client Service Transaction Unmarshall Error Mar 29, 2025
@jzou-rbx jzou-rbx changed the title Fix API Client Service Transaction Unmarshall Error Fix Consul API Client Service Transaction Unmarshall Error Mar 29, 2025
@nitin-sachdev-29
Copy link
Copy Markdown
Contributor

have raised similar PR #22248 because of vercel failure

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

Labels

theme/api Relating to the HTTP API interface

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants