mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: Use Terraform address to index resource + agent association (#1727)
Closes #1705. There was an issue in the implementation brought by #1577 by not trimming the array value when resources use counts. This should fix it, and adds a test to be sure!
This commit is contained in:
@@ -390,7 +390,7 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
|
||||
}
|
||||
}
|
||||
|
||||
agents[resource.Address] = agent
|
||||
agents[convertAddressToLabel(resource.Address)] = agent
|
||||
}
|
||||
|
||||
for _, resource := range tfResources {
|
||||
@@ -400,11 +400,10 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
|
||||
if resource.Type == "coder_agent" || resource.Type == "coder_agent_instance" {
|
||||
continue
|
||||
}
|
||||
resourceKey := strings.Join([]string{resource.Type, resource.Name}, ".")
|
||||
resources = append(resources, &proto.Resource{
|
||||
Name: resource.Name,
|
||||
Type: resource.Type,
|
||||
Agents: findAgents(resourceDependencies, agents, resourceKey),
|
||||
Agents: findAgents(resourceDependencies, agents, convertAddressToLabel(resource.Address)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -485,8 +484,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
|
||||
default:
|
||||
agent.Auth = &proto.Agent_InstanceId{}
|
||||
}
|
||||
resourceKey := strings.Join([]string{resource.Type, resource.Name}, ".")
|
||||
agents[resourceKey] = agent
|
||||
agents[convertAddressToLabel(resource.Address)] = agent
|
||||
}
|
||||
|
||||
// Manually associate agents with instance IDs.
|
||||
@@ -529,8 +527,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
|
||||
if resource.Type == "coder_agent" || resource.Type == "coder_agent_instance" {
|
||||
continue
|
||||
}
|
||||
resourceKey := strings.Join([]string{resource.Type, resource.Name}, ".")
|
||||
resourceAgents := findAgents(resourceDependencies, agents, resourceKey)
|
||||
resourceAgents := findAgents(resourceDependencies, agents, convertAddressToLabel(resource.Address))
|
||||
for _, agent := range resourceAgents {
|
||||
// Didn't use instance identity.
|
||||
if agent.GetToken() != "" {
|
||||
@@ -696,8 +693,8 @@ func findDependenciesWithLabels(graph *gographviz.Graph, nodeName string) []stri
|
||||
// findAgents recursively searches through resource dependencies
|
||||
// to find associated agents. Nested is required for indirect
|
||||
// dependency matching.
|
||||
func findAgents(resourceDependencies map[string][]string, agents map[string]*proto.Agent, resourceKey string) []*proto.Agent {
|
||||
resourceNode, exists := resourceDependencies[resourceKey]
|
||||
func findAgents(resourceDependencies map[string][]string, agents map[string]*proto.Agent, resourceLabel string) []*proto.Agent {
|
||||
resourceNode, exists := resourceDependencies[resourceLabel]
|
||||
if !exists {
|
||||
return []*proto.Agent{}
|
||||
}
|
||||
@@ -714,3 +711,9 @@ func findAgents(resourceDependencies map[string][]string, agents map[string]*pro
|
||||
}
|
||||
return resourceAgents
|
||||
}
|
||||
|
||||
// convertAddressToLabel returns the Terraform address without the count
|
||||
// specifier. eg. "module.ec2_dev.ec2_instance.dev[0]" becomes "module.ec2_dev.ec2_instance.dev"
|
||||
func convertAddressToLabel(address string) string {
|
||||
return strings.Split(address, "[")[0]
|
||||
}
|
||||
|
||||
@@ -206,6 +206,7 @@ provider "coder" {
|
||||
Name: "dryrun-resource-associated-with-agent",
|
||||
Files: map[string]string{
|
||||
"main.tf": provider + `
|
||||
data "coder_workspace" "me" {}
|
||||
resource "coder_agent" "A" {
|
||||
count = 1
|
||||
os = "linux"
|
||||
@@ -216,7 +217,10 @@ provider "coder" {
|
||||
startup_script = "code-server"
|
||||
}
|
||||
resource "null_resource" "A" {
|
||||
count = length(coder_agent.A)
|
||||
depends_on = [
|
||||
coder_agent.A[0]
|
||||
]
|
||||
count = data.coder_workspace.me.start_count
|
||||
}`,
|
||||
},
|
||||
Request: &proto.Provision_Request{
|
||||
|
||||
Reference in New Issue
Block a user