Skip to content

Commit 3e5d292

Browse files
authored
feat: add support for coder_env (coder#11102)
Fixes coder#10166
1 parent 4612c28 commit 3e5d292

File tree

7 files changed

+550
-382
lines changed

7 files changed

+550
-382
lines changed

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,13 +1388,27 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
13881388
Valid: true,
13891389
}
13901390
}
1391-
var env pqtype.NullRawMessage
1392-
if prAgent.Env != nil {
1393-
data, err := json.Marshal(prAgent.Env)
1391+
1392+
env := make(map[string]string)
1393+
// For now, we only support adding extra envs, not overriding
1394+
// existing ones or performing other manipulations. In future
1395+
// we may write these to a separate table so we can perform
1396+
// conditional logic on the agent.
1397+
for _, e := range prAgent.ExtraEnvs {
1398+
env[e.Name] = e.Value
1399+
}
1400+
// Allow the agent defined envs to override extra envs.
1401+
for k, v := range prAgent.Env {
1402+
env[k] = v
1403+
}
1404+
1405+
var envJSON pqtype.NullRawMessage
1406+
if len(env) > 0 {
1407+
data, err := json.Marshal(env)
13941408
if err != nil {
13951409
return xerrors.Errorf("marshal env: %w", err)
13961410
}
1397-
env = pqtype.NullRawMessage{
1411+
envJSON = pqtype.NullRawMessage{
13981412
RawMessage: data,
13991413
Valid: true,
14001414
}
@@ -1417,7 +1431,7 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
14171431
AuthToken: authToken,
14181432
AuthInstanceID: instanceID,
14191433
Architecture: prAgent.Architecture,
1420-
EnvironmentVariables: env,
1434+
EnvironmentVariables: envJSON,
14211435
Directory: prAgent.Directory,
14221436
OperatingSystem: prAgent.OperatingSystem,
14231437
ConnectionTimeoutSeconds: prAgent.GetConnectionTimeoutSeconds(),

coderd/provisionerdserver/provisionerdserver_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,16 @@ func TestInsertWorkspaceResource(t *testing.T) {
15851585
Apps: []*sdkproto.App{{
15861586
Slug: "a",
15871587
}},
1588+
ExtraEnvs: []*sdkproto.Env{
1589+
{
1590+
Name: "something", // Duplicate, already set by Env.
1591+
Value: "I should be discarded!",
1592+
},
1593+
{
1594+
Name: "else",
1595+
Value: "I laugh in the face of danger.",
1596+
},
1597+
},
15881598
Scripts: []*sdkproto.Script{{
15891599
DisplayName: "Startup",
15901600
Icon: "/test.png",
@@ -1609,6 +1619,7 @@ func TestInsertWorkspaceResource(t *testing.T) {
16091619
require.Equal(t, "linux", agent.OperatingSystem)
16101620
want, err := json.Marshal(map[string]string{
16111621
"something": "test",
1622+
"else": "I laugh in the face of danger.",
16121623
})
16131624
require.NoError(t, err)
16141625
got, err := agent.EnvironmentVariables.RawMessage.MarshalJSON()

provisioner/terraform/resources.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ type agentAppAttributes struct {
7777
Healthcheck []appHealthcheckAttributes `mapstructure:"healthcheck"`
7878
}
7979

80+
type agentEnvAttributes struct {
81+
AgentID string `mapstructure:"agent_id"`
82+
Name string `mapstructure:"name"`
83+
Value string `mapstructure:"value"`
84+
}
85+
8086
type agentScriptAttributes struct {
8187
AgentID string `mapstructure:"agent_id"`
8288
DisplayName string `mapstructure:"display_name"`
@@ -435,6 +441,32 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
435441
}
436442
}
437443

444+
// Associate envs with agents.
445+
for _, resources := range tfResourcesByLabel {
446+
for _, resource := range resources {
447+
if resource.Type != "coder_env" {
448+
continue
449+
}
450+
var attrs agentEnvAttributes
451+
err = mapstructure.Decode(resource.AttributeValues, &attrs)
452+
if err != nil {
453+
return nil, xerrors.Errorf("decode env attributes: %w", err)
454+
}
455+
for _, agents := range resourceAgents {
456+
for _, agent := range agents {
457+
// Find agents with the matching ID and associate them!
458+
if agent.Id != attrs.AgentID {
459+
continue
460+
}
461+
agent.ExtraEnvs = append(agent.ExtraEnvs, &proto.Env{
462+
Name: attrs.Name,
463+
Value: attrs.Value,
464+
})
465+
}
466+
}
467+
}
468+
}
469+
438470
// Associate scripts with agents.
439471
for _, resources := range tfResourcesByLabel {
440472
for _, resource := range resources {
@@ -444,7 +476,7 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
444476
var attrs agentScriptAttributes
445477
err = mapstructure.Decode(resource.AttributeValues, &attrs)
446478
if err != nil {
447-
return nil, xerrors.Errorf("decode app attributes: %w", err)
479+
return nil, xerrors.Errorf("decode script attributes: %w", err)
448480
}
449481
for _, agents := range resourceAgents {
450482
for _, agent := range agents {

0 commit comments

Comments
 (0)