Skip to content

fix: use resource_id directly for coder_metadata association #18300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

sreya
Copy link
Collaborator

@sreya sreya commented Jun 10, 2025

Previously, coder_metadata resources would associate with the wrong Terraform resource because the implementation relied on graph traversal instead of using the explicit resource_id attribute.

This fix:

  • Builds a map from resource IDs to their labels for direct lookup
  • Uses the resource_id attribute to find the target resource directly
  • Falls back to graph traversal only if resource_id lookup fails
  • Adds logging when resource_id is not found in the state

This ensures that metadata is correctly associated with the intended resource, even when there are complex dependencies in the Terraform configuration.

Fixes the issue where coder_metadata would incorrectly associate with resources it references in its values rather than the resource specified in resource_id.

@sreya sreya force-pushed the jon/resourcemetadata branch from 439f812 to 558c1eb Compare June 10, 2025 03:41
@github-actions github-actions bot added the stale This issue is like stale bread. label Jun 20, 2025
@github-actions github-actions bot closed this Jun 23, 2025
@sreya sreya reopened this Jun 23, 2025
@github-actions github-actions bot removed the stale This issue is like stale bread. label Jun 24, 2025
@github-actions github-actions bot added the stale This issue is like stale bread. label Jul 1, 2025
@github-actions github-actions bot closed this Jul 5, 2025
@ThomasK33 ThomasK33 reopened this Aug 8, 2025
@ThomasK33 ThomasK33 assigned ThomasK33 and unassigned sreya Aug 8, 2025
@ThomasK33 ThomasK33 force-pushed the jon/resourcemetadata branch from ae3960b to 7e0c0a2 Compare August 8, 2025 10:20
@ThomasK33 ThomasK33 marked this pull request as ready for review August 8, 2025 10:41
@github-actions github-actions bot removed the stale This issue is like stale bread. label Aug 9, 2025
Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are at a point where it would make sense to start breaking up ConvertState. This metadata association piece seems like a good candidate to start with and have individual unit tests around.

Comment on lines +706 to +710
case 0:
// If we couldn't find by ID, fall back to graph traversal
logger.Warn(ctx, "coder_metadata resource_id not found, falling back to graph traversal",
slog.F("resource_id", attrs.ResourceID),
slog.F("metadata_address", resource.Address))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this warn log will just end up in the server log and won't be especially visible to template authors.

I can see an argument for erroring out here, but the larger argument against is that it would most likely break existing templates. How do we feel about this?

Comment on lines +714 to +719
default:
// Multiple resources with same ID - this creates ambiguity
logger.Warn(ctx, "multiple resources found with same resource_id, falling back to graph traversal",
slog.F("resource_id", attrs.ResourceID),
slog.F("metadata_address", resource.Address),
slog.F("matching_labels", foundLabels))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I can see an argument for erroring here to surface the misconfiguration but worry about the potential for breaking existing templates.

continue
}
}
if attachedResource == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what situation would we encounter this? It's not covered in existing or new tests.

resourceLabel := convertAddressToLabel(resource.Address)

var attachedNode *gographviz.Node
for _, node := range graph.Nodes.Lookup {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this stage, it would make sense to extract the "metadata association" piece out and test it separately.

resource "null_resource" "example" {}

resource "coder_metadata" "example" {
resource_id = "non-existent-id"
Copy link
Member

@ethanndickson ethanndickson Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have another test to check that it falls back to graph traversal when the resource_id is set to coder_agent.whatever.id. I've seen that pattern in many customer templates in the past.

@spikecurtis
Copy link
Contributor

Is there an issue that describes the problem this is intending to solve?

It worries me that we're using resource IDs to identify the resource the metadata refers to when resource IDs are not unique. I think it would be better to deprecate the resource ID and instead use something unique like (module, resource_type, name).

@sreya
Copy link
Collaborator Author

sreya commented Aug 13, 2025

Yeah the problem is the metadata gets assigned to the wrong terraform resource. So on the workspace page, metadata you intend to be displayed alongside the agent is instead hidden on a random resource that you have to open the side bar to see.

This one is working as intended but to give an idea of what i'm describing

image

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.

5 participants