-
Notifications
You must be signed in to change notification settings - Fork 21
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
coder_metadata
gets incorrectly assigned to resources
#306
Comments
coder/coder#6517 could be related. |
#133 also looks related. |
unassigning myself for now in case @Emyrk can pick it up, but I might circle back if he doesn't |
One thing to keep in mind when solving this is that a Terraform resource ID isn't a protected attribute or anything special. There's nothing stopping popular providers from using the same |
I was unable to get to this, but pretty sure the code is located here in coderd: https://github.com/coder/coder/blob/389768b48f8d0ab9cbd28bc66f7bae308011062d/provisioner/terraform/resources.go#L563-L578 And should be fixed there. |
I've been looking into this issue and I have a pretty good idea of what's going on and what's going wrong. As I see it, we the following problems:
For 1 and 2, we can utilize tfplan and tfstate JSON outputs, however, for 3 I have yet to figure out a 100% solution. Proposal: For Does this seem like a sufficient solution for the problem? See below for more details on what data is available to us. Thankfully, the tfplan actually contains everything we need (see {
"configuration": {
"root_module": {
"resources": [
{
"address": "coder_metadata.about_info",
"mode": "managed",
"type": "coder_metadata",
"name": "about_info",
"provider_config_key": "coder",
"expressions": {
"item": [
{
"key": {
"constant_value": "other-reference"
},
"value": {
"references": [
"null_resource.other.triggers.about_name",
"null_resource.other.triggers",
"null_resource.other"
]
}
}
],
"resource_id": {
"references": [
"null_resource.about.id",
"null_resource.about"
]
}
},
"schema_version": 0
}
]
}
}
} As can be ween in this JSON output, the exact resource and field is being referenced in the configuration. We've yet to utilize this part of the tfplan, which seems like a shame, it could probably also be used to make a better resource<->agent association. However, if we assume that the uniqueness of ID is unreliable, the tfstate actually ends up being more lossy for our use-case as it references the potentially non-unique ID instead of the unique resource name. (JSON has been trimmed.) {
"values": {
"root_module": {
"resources": [
{
"address": "coder_metadata.about_info",
"mode": "managed",
"type": "coder_metadata",
"name": "about_info",
"provider_name": "registry.terraform.io/coder/coder",
"schema_version": 0,
"values": {
"daily_cost": null,
"hide": null,
"icon": null,
"id": "f0c9ff7b-48fa-425a-ac82-8bc4759802db",
"item": [
{
"is_null": false,
"key": "other-reference",
"sensitive": false,
"value": "Null About"
}
],
"resource_id": "8302293550006744338"
},
"sensitive_values": {
"item": [
{}
]
},
"depends_on": [
"coder_agent.main",
"null_resource.about",
"null_resource.other"
]
},
{
"address": "null_resource.about",
"mode": "managed",
"type": "null_resource",
"name": "about",
"provider_name": "registry.terraform.io/hashicorp/null",
"schema_version": 0,
"values": {
"id": "8302293550006744338",
"triggers": {
"name": "Null About"
}
},
"sensitive_values": {
"triggers": {}
},
"depends_on": [
"coder_agent.main"
]
}
]
}
}
} AFAICT, the best we can do here is rely on resource ID (without passing on information from plan). My guess is that we could further reduce the number of possible resources by checking that the resource with said ID exists in |
I just had a good chat with @mafredri In summary, we believe using To recap in my own words:
In the long term (2mo+), I proposed to leverage the terraform evaluation engine that is under works to determine the relationships instead. We can do this by looking at the static hcl expression for the @mafredri given my proposal above, do you think you have a cheap fix that can solve some of the more obvious errors? |
@Emyrk thanks for the great chat and writing the summary! While I'm a fan of consolidating our Terraform logic in one place, and would like to see us use the static analysis your working on @Emyrk, we can fairly simply fix this issue by utilizing the configuration/resource ID of tf plan/apply and probably cover 99.9% of cases. I'd suggest we fall-back to the current graph-based matching in the case of duplicate resource IDs, and pick the closest match of those IDs. @bpmct I'm guessing a 2mo+ for a fix is not ideal here, but I'll defer to you whether or not we should fix this now or wait. |
Let's just do this.
It wouldn't take 2 months to implement something special, right? It would be 2 months until we have static analysis (for other purposes), then we can repurpose to do this assignment slightly better? I'm in favor of the quick fix now and then evaluating using better methods once we have them. |
Currently, the Terraform provisioner relies on the output of
terraform graph
to determine what resourcecoder_metadata
instances should be applied to when building the template or workspace.Unfortunately, it's very reasonable for a user to write a template where the output of
terraform graph
does not meet what's required to correctly map the metadata to the resource, such as when using the contents of another resource to populate a field. This results in the UI showing metadata intended for one resource on another.Here's a minimal example of this using
null_resource
s:The graph output of this template is then:
![image](https://private-user-images.githubusercontent.com/39577870/358247303-7d640ab4-b5f1-4430-9098-4ab3fb84b086.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkyODM3NjIsIm5iZiI6MTczOTI4MzQ2MiwicGF0aCI6Ii8zOTU3Nzg3MC8zNTgyNDczMDMtN2Q2NDBhYjQtYjVmMS00NDMwLTkwOTgtNGFiM2ZiODRiMDg2LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjExVDE0MTc0MlomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTA3MDhhMDRjMjZmYWI5YzkxOWQ1NjEwMDM3M2NiM2JhYmYyYTliNjdjMDEyMzNkN2QxN2ZlZjBhM2M3YjEzMzMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.ZPQIiU6RqP_lj6wMHYts_ULMWxxipubEN2c7aTlS6u0)
Since
null_resource.other
is the closest resource tocoder_metadata.about_info
, the metadata gets assigned to it, instead ofnull_resource.about
, as was indicated in the template.The same graph and graph traversal algorithm is used to determine which resource the agent exists within, so it's possible (but not proven) that this same issue effects assigning the agent to a resource.
Regardless, this should be a purely cosmetic bug, and shouldn't have any bearing on anything outside of how workspace metadata is shown in the UI.
The text was updated successfully, but these errors were encountered: