Skip to content

Commit 13a3ddd

Browse files
authored
fix(agent/agentcontainers): generate devcontainer metadata from schema (coder#16881)
Adds new dcspec package containing automatically generated devcontainer schema (using glideapps/quicktype).
1 parent ec51765 commit 13a3ddd

11 files changed

+1954
-15
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated files
22
agent/agentcontainers/acmock/acmock.go linguist-generated=true
3+
agent/agentcontainers/dcspec/dcspec_gen.go linguist-generated=true
34
coderd/apidoc/docs.go linguist-generated=true
45
docs/reference/api/*.md linguist-generated=true
56
docs/reference/cli/*.md linguist-generated=true

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ GEN_FILES := \
564564
examples/examples.gen.json \
565565
$(TAILNETTEST_MOCKS) \
566566
coderd/database/pubsub/psmock/psmock.go \
567-
agent/agentcontainers/acmock/acmock.go
568-
567+
agent/agentcontainers/acmock/acmock.go \
568+
agent/agentcontainers/dcspec/dcspec_gen.go
569569

570570
# all gen targets should be added here and to gen/mark-fresh
571571
gen: gen/db $(GEN_FILES)
@@ -600,6 +600,7 @@ gen/mark-fresh:
600600
$(TAILNETTEST_MOCKS) \
601601
coderd/database/pubsub/psmock/psmock.go \
602602
agent/agentcontainers/acmock/acmock.go \
603+
agent/agentcontainers/dcspec/dcspec_gen.go \
603604
"
604605

605606
for file in $$files; do
@@ -634,6 +635,9 @@ coderd/database/pubsub/psmock/psmock.go: coderd/database/pubsub/pubsub.go
634635
agent/agentcontainers/acmock/acmock.go: agent/agentcontainers/containers.go
635636
go generate ./agent/agentcontainers/acmock/
636637

638+
agent/agentcontainers/dcspec/dcspec_gen.go: agent/agentcontainers/dcspec/devContainer.base.schema.json
639+
go generate ./agent/agentcontainers/dcspec/
640+
637641
$(TAILNETTEST_MOCKS): tailnet/coordinator.go tailnet/service.go
638642
go generate ./tailnet/tailnettest/
639643

agent/agentcontainers/containers_dockercli.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import (
1313
"strings"
1414
"time"
1515

16+
"github.com/coder/coder/v2/agent/agentcontainers/dcspec"
1617
"github.com/coder/coder/v2/agent/agentexec"
1718
"github.com/coder/coder/v2/agent/usershell"
19+
"github.com/coder/coder/v2/coderd/util/ptr"
1820
"github.com/coder/coder/v2/codersdk"
1921

2022
"golang.org/x/exp/maps"
@@ -183,7 +185,7 @@ func devcontainerEnv(ctx context.Context, execer agentexec.Execer, container str
183185
return nil, nil
184186
}
185187

186-
meta := make([]DevContainerMeta, 0)
188+
meta := make([]dcspec.DevContainer, 0)
187189
if err := json.Unmarshal([]byte(rawMeta), &meta); err != nil {
188190
return nil, xerrors.Errorf("unmarshal devcontainer.metadata: %w", err)
189191
}
@@ -192,7 +194,13 @@ func devcontainerEnv(ctx context.Context, execer agentexec.Execer, container str
192194
env := make([]string, 0)
193195
for _, m := range meta {
194196
for k, v := range m.RemoteEnv {
195-
env = append(env, fmt.Sprintf("%s=%s", k, v))
197+
if v == nil { // *string per spec
198+
// devcontainer-cli will set this to the string "null" if the value is
199+
// not set. Explicitly setting to an empty string here as this would be
200+
// more expected here.
201+
v = ptr.Ref("")
202+
}
203+
env = append(env, fmt.Sprintf("%s=%s", k, *v))
196204
}
197205
}
198206
slices.Sort(env)
@@ -276,7 +284,7 @@ func (dcl *DockerCLILister) List(ctx context.Context) (codersdk.WorkspaceAgentLi
276284
// log this error, but I'm not sure it's worth it.
277285
ins, dockerInspectStderr, err := runDockerInspect(ctx, dcl.execer, ids...)
278286
if err != nil {
279-
return codersdk.WorkspaceAgentListContainersResponse{}, xerrors.Errorf("run docker inspect: %w", err)
287+
return codersdk.WorkspaceAgentListContainersResponse{}, xerrors.Errorf("run docker inspect: %w: %s", err, dockerInspectStderr)
280288
}
281289

282290
for _, in := range ins {

agent/agentcontainers/containers_internal_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ import (
3434
// It can be run manually as follows:
3535
//
3636
// CODER_TEST_USE_DOCKER=1 go test ./agent/agentcontainers -run TestDockerCLIContainerLister
37+
//
38+
//nolint:paralleltest // This test tends to flake when lots of containers start and stop in parallel.
3739
func TestIntegrationDocker(t *testing.T) {
38-
t.Parallel()
3940
if ctud, ok := os.LookupEnv("CODER_TEST_USE_DOCKER"); !ok || ctud != "1" {
4041
t.Skip("Set CODER_TEST_USE_DOCKER=1 to run this test")
4142
}
@@ -418,8 +419,9 @@ func TestConvertDockerVolume(t *testing.T) {
418419
// It can be run manually as follows:
419420
//
420421
// CODER_TEST_USE_DOCKER=1 go test ./agent/agentcontainers -run TestDockerEnvInfoer
422+
//
423+
//nolint:paralleltest // This test tends to flake when lots of containers start and stop in parallel.
421424
func TestDockerEnvInfoer(t *testing.T) {
422-
t.Parallel()
423425
if ctud, ok := os.LookupEnv("CODER_TEST_USE_DOCKER"); !ok || ctud != "1" {
424426
t.Skip("Set CODER_TEST_USE_DOCKER=1 to run this test")
425427
}
@@ -483,9 +485,8 @@ func TestDockerEnvInfoer(t *testing.T) {
483485
expectedUserShell: "/bin/bash",
484486
},
485487
} {
488+
//nolint:paralleltest // variable recapture no longer required
486489
t.Run(fmt.Sprintf("#%d", idx), func(t *testing.T) {
487-
t.Parallel()
488-
489490
// Start a container with the given image
490491
// and environment variables
491492
image := strings.Split(tt.image, ":")[0]

0 commit comments

Comments
 (0)