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

Skip to content

Commit b63880b

Browse files
committed
feat: add support for coder_env
Fixes #10166
1 parent 4ca4736 commit b63880b

File tree

6 files changed

+549
-382
lines changed

6 files changed

+549
-382
lines changed

coderd/provisionerdserver/provisionerdserver.go

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