Skip to content

Commit 74329f4

Browse files
authored
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!
1 parent 95d7e39 commit 74329f4

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

provisioner/terraform/provision.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
390390
}
391391
}
392392

393-
agents[resource.Address] = agent
393+
agents[convertAddressToLabel(resource.Address)] = agent
394394
}
395395

396396
for _, resource := range tfResources {
@@ -400,11 +400,10 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
400400
if resource.Type == "coder_agent" || resource.Type == "coder_agent_instance" {
401401
continue
402402
}
403-
resourceKey := strings.Join([]string{resource.Type, resource.Name}, ".")
404403
resources = append(resources, &proto.Resource{
405404
Name: resource.Name,
406405
Type: resource.Type,
407-
Agents: findAgents(resourceDependencies, agents, resourceKey),
406+
Agents: findAgents(resourceDependencies, agents, convertAddressToLabel(resource.Address)),
408407
})
409408
}
410409

@@ -485,8 +484,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
485484
default:
486485
agent.Auth = &proto.Agent_InstanceId{}
487486
}
488-
resourceKey := strings.Join([]string{resource.Type, resource.Name}, ".")
489-
agents[resourceKey] = agent
487+
agents[convertAddressToLabel(resource.Address)] = agent
490488
}
491489

492490
// Manually associate agents with instance IDs.
@@ -529,8 +527,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
529527
if resource.Type == "coder_agent" || resource.Type == "coder_agent_instance" {
530528
continue
531529
}
532-
resourceKey := strings.Join([]string{resource.Type, resource.Name}, ".")
533-
resourceAgents := findAgents(resourceDependencies, agents, resourceKey)
530+
resourceAgents := findAgents(resourceDependencies, agents, convertAddressToLabel(resource.Address))
534531
for _, agent := range resourceAgents {
535532
// Didn't use instance identity.
536533
if agent.GetToken() != "" {
@@ -696,8 +693,8 @@ func findDependenciesWithLabels(graph *gographviz.Graph, nodeName string) []stri
696693
// findAgents recursively searches through resource dependencies
697694
// to find associated agents. Nested is required for indirect
698695
// dependency matching.
699-
func findAgents(resourceDependencies map[string][]string, agents map[string]*proto.Agent, resourceKey string) []*proto.Agent {
700-
resourceNode, exists := resourceDependencies[resourceKey]
696+
func findAgents(resourceDependencies map[string][]string, agents map[string]*proto.Agent, resourceLabel string) []*proto.Agent {
697+
resourceNode, exists := resourceDependencies[resourceLabel]
701698
if !exists {
702699
return []*proto.Agent{}
703700
}
@@ -714,3 +711,9 @@ func findAgents(resourceDependencies map[string][]string, agents map[string]*pro
714711
}
715712
return resourceAgents
716713
}
714+
715+
// convertAddressToLabel returns the Terraform address without the count
716+
// specifier. eg. "module.ec2_dev.ec2_instance.dev[0]" becomes "module.ec2_dev.ec2_instance.dev"
717+
func convertAddressToLabel(address string) string {
718+
return strings.Split(address, "[")[0]
719+
}

provisioner/terraform/provision_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ provider "coder" {
206206
Name: "dryrun-resource-associated-with-agent",
207207
Files: map[string]string{
208208
"main.tf": provider + `
209+
data "coder_workspace" "me" {}
209210
resource "coder_agent" "A" {
210211
count = 1
211212
os = "linux"
@@ -216,7 +217,10 @@ provider "coder" {
216217
startup_script = "code-server"
217218
}
218219
resource "null_resource" "A" {
219-
count = length(coder_agent.A)
220+
depends_on = [
221+
coder_agent.A[0]
222+
]
223+
count = data.coder_workspace.me.start_count
220224
}`,
221225
},
222226
Request: &proto.Provision_Request{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy