Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(provisioner): handle multiple agents, apps, scripts and envs#13741

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
mtojek merged 11 commits intomainfrom12949-apps
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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
32 changes: 29 additions & 3 deletionsprovisioner/terraform/resources.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -427,9 +427,11 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
for _, agents := range resourceAgents {
for _, agent := range agents {
// Find agents with the matching ID and associate them!
if agent.Id != attrs.AgentID {

if !dependsOnAgent(graph, agent, attrs.AgentID, resource) {
continue
}

agent.Apps = append(agent.Apps, &proto.App{
Slug: attrs.Slug,
DisplayName: attrs.DisplayName,
Expand DownExpand Up@@ -461,7 +463,7 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
for _, agents := range resourceAgents {
for _, agent := range agents {
// Find agents with the matching ID and associate them!
ifagent.Id !=attrs.AgentID {
if!dependsOnAgent(graph, agent,attrs.AgentID, resource) {
continue
}
agent.ExtraEnvs = append(agent.ExtraEnvs, &proto.Env{
Expand All@@ -487,7 +489,7 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
for _, agents := range resourceAgents {
for _, agent := range agents {
// Find agents with the matching ID and associate them!
ifagent.Id !=attrs.AgentID {
if!dependsOnAgent(graph, agent,attrs.AgentID, resource) {
continue
}
agent.Scripts = append(agent.Scripts, &proto.Script{
Expand DownExpand Up@@ -748,6 +750,30 @@ func convertAddressToLabel(address string) string {
return cut
}

func dependsOnAgent(graph *gographviz.Graph, agent *proto.Agent, resourceAgentID string, resource *tfjson.StateResource) bool {
// Plan: we need to find if there is edge between the agent and the resource.
if agent.Id == "" && resourceAgentID == "" {
resourceNodeSuffix := fmt.Sprintf(`] %s.%s (expand)"`, resource.Type, resource.Name)
agentNodeSuffix := fmt.Sprintf(`] coder_agent.%s (expand)"`, agent.Name)

// Traverse the graph to check if the coder_<resource_type> depends on coder_agent.
for _, dst := range graph.Edges.SrcToDsts {
for _, edges := range dst {
for _, edge := range edges {
if strings.HasSuffix(edge.Src, resourceNodeSuffix) &&
strings.HasSuffix(edge.Dst, agentNodeSuffix) {
return true
}
}
}
}
return false
}

// Provision: agent ID and child resource ID are present
return agent.Id == resourceAgentID
}

type graphResource struct {
Label string
Depth uint
Expand Down
163 changes: 162 additions & 1 deletionprovisioner/terraform/resources_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -219,6 +219,150 @@ func TestConvertResources(t *testing.T) {
}},
}},
},
"multiple-agents-multiple-apps": {
resources: []*proto.Resource{{
Name: "dev1",
Type: "null_resource",
Agents: []*proto.Agent{{
Name: "dev1",
OperatingSystem: "linux",
Architecture: "amd64",
Apps: []*proto.App{
{
Slug: "app1",
DisplayName: "app1",
// Subdomain defaults to false if unspecified.
Subdomain: false,
},
{
Slug: "app2",
DisplayName: "app2",
Subdomain: true,
Healthcheck: &proto.Healthcheck{
Url: "http://localhost:13337/healthz",
Interval: 5,
Threshold: 6,
},
},
},
Auth: &proto.Agent_Token{},
ConnectionTimeoutSeconds: 120,
DisplayApps: &displayApps,
}},
}, {
Name: "dev2",
Type: "null_resource",
Agents: []*proto.Agent{{
Name: "dev2",
OperatingSystem: "linux",
Architecture: "amd64",
Apps: []*proto.App{
{
Slug: "app3",
DisplayName: "app3",
Subdomain: false,
},
},
Auth: &proto.Agent_Token{},
ConnectionTimeoutSeconds: 120,
DisplayApps: &displayApps,
}},
}},
},
"multiple-agents-multiple-envs": {
resources: []*proto.Resource{{
Name: "dev1",
Type: "null_resource",
Agents: []*proto.Agent{{
Name: "dev1",
OperatingSystem: "linux",
Architecture: "amd64",
ExtraEnvs: []*proto.Env{
{
Name: "ENV_1",
Value: "Env 1",
},
{
Name: "ENV_2",
Value: "Env 2",
},
},
Auth: &proto.Agent_Token{},
ConnectionTimeoutSeconds: 120,
DisplayApps: &displayApps,
}},
}, {
Name: "dev2",
Type: "null_resource",
Agents: []*proto.Agent{{
Name: "dev2",
OperatingSystem: "linux",
Architecture: "amd64",
ExtraEnvs: []*proto.Env{
{
Name: "ENV_3",
Value: "Env 3",
},
},
Auth: &proto.Agent_Token{},
ConnectionTimeoutSeconds: 120,
DisplayApps: &displayApps,
}},
}, {
Name: "env1",
Type: "coder_env",
}, {
Name: "env2",
Type: "coder_env",
}, {
Name: "env3",
Type: "coder_env",
}},
},
"multiple-agents-multiple-scripts": {
resources: []*proto.Resource{{
Name: "dev1",
Type: "null_resource",
Agents: []*proto.Agent{{
Name: "dev1",
OperatingSystem: "linux",
Architecture: "amd64",
Scripts: []*proto.Script{
{
DisplayName: "Foobar Script 1",
Script: "echo foobar 1",
RunOnStart: true,
},
{
DisplayName: "Foobar Script 2",
Script: "echo foobar 2",
RunOnStart: true,
},
},
Auth: &proto.Agent_Token{},
ConnectionTimeoutSeconds: 120,
DisplayApps: &displayApps,
}},
}, {
Name: "dev2",
Type: "null_resource",
Agents: []*proto.Agent{{
Name: "dev2",
OperatingSystem: "linux",
Architecture: "amd64",
Scripts: []*proto.Script{
{
DisplayName: "Foobar Script 3",
Script: "echo foobar 3",
RunOnStart: true,
},
},
Auth: &proto.Agent_Token{},
ConnectionTimeoutSeconds: 120,
DisplayApps: &displayApps,
}},
}},
},
// Tests fetching metadata about workspace resources.
"resource-metadata": {
resources: []*proto.Resource{{
Expand DownExpand Up@@ -565,6 +709,18 @@ func TestConvertResources(t *testing.T) {
sortResources(state.Resources)
sortExternalAuthProviders(state.ExternalAuthProviders)

for _, resource := range state.Resources {
for _, agent := range resource.Agents {
agent.Id = ""
if agent.GetToken() != "" {
agent.Auth = &proto.Agent_Token{}
}
if agent.GetInstanceId() != "" {
agent.Auth = &proto.Agent_InstanceId{}
}
}
}

expectedNoMetadata := make([]*proto.Resource, 0)
for _, resource := range expected.resources {
resourceCopy, _ := protobuf.Clone(resource).(*proto.Resource)
Expand DownExpand Up@@ -642,7 +798,6 @@ func TestConvertResources(t *testing.T) {
var resourcesMap []map[string]interface{}
err = json.Unmarshal(data, &resourcesMap)
require.NoError(t, err)

require.Equal(t, expectedMap, resourcesMap)
require.ElementsMatch(t, expected.externalAuthProviders, state.ExternalAuthProviders)
})
Expand DownExpand Up@@ -911,6 +1066,12 @@ func sortResources(resources []*proto.Resource) {
sort.Slice(agent.Apps, func(i, j int) bool {
return agent.Apps[i].Slug < agent.Apps[j].Slug
})
sort.Slice(agent.ExtraEnvs, func(i, j int) bool {
return agent.ExtraEnvs[i].Name < agent.ExtraEnvs[j].Name
})
sort.Slice(agent.Scripts, func(i, j int) bool {
return agent.Scripts[i].DisplayName < agent.Scripts[j].DisplayName
})
}
sort.Slice(resource.Agents, func(i, j int) bool {
return resource.Agents[i].Name < resource.Agents[j].Name
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.22.0"
}
}
}

resource "coder_agent" "dev1" {
os = "linux"
arch = "amd64"
}

resource "coder_agent" "dev2" {
os = "linux"
arch = "amd64"
}

# app1 is for testing subdomain default.
resource "coder_app" "app1" {
agent_id = coder_agent.dev1.id
slug = "app1"
# subdomain should default to false.
# subdomain = false
}

# app2 tests that subdomaincan be true, and that healthchecks work.
resource "coder_app" "app2" {
agent_id = coder_agent.dev1.id
slug = "app2"
subdomain = true
healthcheck {
url = "http://localhost:13337/healthz"
interval = 5
threshold = 6
}
}

# app3 tests that subdomain can explicitly be false.
resource "coder_app" "app3" {
agent_id = coder_agent.dev2.id
slug = "app3"
subdomain = false
}

resource "null_resource" "dev1" {
depends_on = [
coder_agent.dev1
]
}

resource "null_resource" "dev2" {
depends_on = [
coder_agent.dev2
]
}

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.

Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp