Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Use Terraform address to index resource + agent association #1727

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

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)),
})
}

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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() != "" {
Expand Down Expand Up @@ -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{}
}
Expand All @@ -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]
}
6 changes: 5 additions & 1 deletion provisioner/terraform/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down