Skip to content

Commit 3e2e2ac

Browse files
authored
fix: enforce unique agent names per workspace (#5497)
1 parent 461c0d0 commit 3e2e2ac

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,8 +854,16 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
854854
}
855855
snapshot.WorkspaceResources = append(snapshot.WorkspaceResources, telemetry.ConvertWorkspaceResource(resource))
856856

857-
appSlugs := make(map[string]struct{})
857+
var (
858+
agentNames = make(map[string]struct{})
859+
appSlugs = make(map[string]struct{})
860+
)
858861
for _, prAgent := range protoResource.Agents {
862+
if _, ok := agentNames[prAgent.Name]; ok {
863+
return xerrors.Errorf("duplicate agent name %q", prAgent.Name)
864+
}
865+
agentNames[prAgent.Name] = struct{}{}
866+
859867
var instanceID sql.NullString
860868
if prAgent.GetInstanceId() != "" {
861869
instanceID = sql.NullString{

provisioner/terraform/resources.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res
101101
findTerraformResources(module)
102102

103103
// Find all agents!
104+
agentNames := map[string]struct{}{}
104105
for _, tfResource := range tfResourceByLabel {
105106
if tfResource.Type != "coder_agent" {
106107
continue
@@ -110,6 +111,12 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res
110111
if err != nil {
111112
return nil, xerrors.Errorf("decode agent attributes: %w", err)
112113
}
114+
115+
if _, ok := agentNames[tfResource.Name]; ok {
116+
return nil, xerrors.Errorf("duplicate agent name: %s", tfResource.Name)
117+
}
118+
agentNames[tfResource.Name] = struct{}{}
119+
113120
agent := &proto.Agent{
114121
Name: tfResource.Name,
115122
Id: attrs.ID,

0 commit comments

Comments
 (0)