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:
Kyle Carberry
2022-05-24 20:00:34 -05:00
committed by GitHub
parent 95d7e39c80
commit 74329f479f
2 changed files with 17 additions and 10 deletions
+12 -9
View File
@@ -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]
}
+5 -1
View File
@@ -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{