Skip to content

Commit 765c2cf

Browse files
committed
backend
1 parent 8e29ee5 commit 765c2cf

File tree

9 files changed

+235
-102
lines changed

9 files changed

+235
-102
lines changed

agent/agentcontainers/api.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,6 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
486486
// Check if the container is running and update the known devcontainers.
487487
for i := range updated.Containers {
488488
container := &updated.Containers[i] // Grab a reference to the container to allow mutating it.
489-
container.DevcontainerStatus = "" // Reset the status for the container (updated later).
490-
container.DevcontainerDirty = false // Reset dirty state for the container (updated later).
491489

492490
workspaceFolder := container.Labels[DevcontainerLocalFolderLabel]
493491
configFile := container.Labels[DevcontainerConfigFileLabel]
@@ -568,8 +566,6 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
568566
// TODO(mafredri): Parse the container label (i.e. devcontainer.json) for customization.
569567
dc.Name = safeFriendlyName(dc.Container.FriendlyName)
570568
}
571-
dc.Container.DevcontainerStatus = dc.Status
572-
dc.Container.DevcontainerDirty = dc.Dirty
573569
}
574570

575571
switch {
@@ -584,13 +580,11 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code
584580
if dc.Container.Running {
585581
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusRunning
586582
}
587-
dc.Container.DevcontainerStatus = dc.Status
588583

589584
dc.Dirty = false
590585
if lastModified, hasModTime := api.configFileModifiedTimes[dc.ConfigPath]; hasModTime && dc.Container.CreatedAt.Before(lastModified) {
591586
dc.Dirty = true
592587
}
593-
dc.Container.DevcontainerDirty = dc.Dirty
594588

595589
if _, injected := api.injectedSubAgentProcs[dc.Container.ID]; !injected && dc.Status == codersdk.WorkspaceAgentDevcontainerStatusRunning {
596590
err := api.injectSubAgentIntoContainerLocked(ctx, dc)
@@ -661,9 +655,19 @@ func (api *API) getContainers() (codersdk.WorkspaceAgentListContainersResponse,
661655
if api.containersErr != nil {
662656
return codersdk.WorkspaceAgentListContainersResponse{}, api.containersErr
663657
}
658+
659+
devcontainers := make([]codersdk.WorkspaceAgentDevcontainer, 0, len(api.knownDevcontainers))
660+
for _, dc := range api.knownDevcontainers {
661+
devcontainers = append(devcontainers, dc)
662+
}
663+
slices.SortFunc(devcontainers, func(a, b codersdk.WorkspaceAgentDevcontainer) int {
664+
return strings.Compare(a.ID.String(), b.ID.String())
665+
})
666+
664667
return codersdk.WorkspaceAgentListContainersResponse{
665-
Containers: slices.Clone(api.containers.Containers),
666-
Warnings: slices.Clone(api.containers.Warnings),
668+
Devcontainers: devcontainers,
669+
Containers: slices.Clone(api.containers.Containers),
670+
Warnings: slices.Clone(api.containers.Warnings),
667671
}, nil
668672
}
669673

@@ -740,9 +744,6 @@ func (api *API) handleDevcontainerRecreate(w http.ResponseWriter, r *http.Reques
740744
// Update the status so that we don't try to recreate the
741745
// devcontainer multiple times in parallel.
742746
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStarting
743-
if dc.Container != nil {
744-
dc.Container.DevcontainerStatus = dc.Status
745-
}
746747
api.knownDevcontainers[dc.WorkspaceFolder] = dc
747748
api.asyncWg.Add(1)
748749
go api.recreateDevcontainer(dc, configPath)
@@ -815,9 +816,6 @@ func (api *API) recreateDevcontainer(dc codersdk.WorkspaceAgentDevcontainer, con
815816
api.mu.Lock()
816817
dc = api.knownDevcontainers[dc.WorkspaceFolder]
817818
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusError
818-
if dc.Container != nil {
819-
dc.Container.DevcontainerStatus = dc.Status
820-
}
821819
api.knownDevcontainers[dc.WorkspaceFolder] = dc
822820
api.recreateErrorTimes[dc.WorkspaceFolder] = api.clock.Now("agentcontainers", "recreate", "errorTimes")
823821
api.mu.Unlock()
@@ -838,7 +836,6 @@ func (api *API) recreateDevcontainer(dc codersdk.WorkspaceAgentDevcontainer, con
838836
if dc.Container.Running {
839837
dc.Status = codersdk.WorkspaceAgentDevcontainerStatusRunning
840838
}
841-
dc.Container.DevcontainerStatus = dc.Status
842839
}
843840
dc.Dirty = false
844841
api.recreateSuccessTimes[dc.WorkspaceFolder] = api.clock.Now("agentcontainers", "recreate", "successTimes")
@@ -914,10 +911,6 @@ func (api *API) markDevcontainerDirty(configPath string, modifiedAt time.Time) {
914911
logger.Info(api.ctx, "marking devcontainer as dirty")
915912
dc.Dirty = true
916913
}
917-
if dc.Container != nil && !dc.Container.DevcontainerDirty {
918-
logger.Info(api.ctx, "marking devcontainer container as dirty")
919-
dc.Container.DevcontainerDirty = true
920-
}
921914

922915
api.knownDevcontainers[dc.WorkspaceFolder] = dc
923916
}

agent/agentcontainers/api_test.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ func TestAPI(t *testing.T) {
609609
require.Len(t, resp.Devcontainers, 1, "expected one devcontainer in response")
610610
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusStarting, resp.Devcontainers[0].Status, "devcontainer is not starting")
611611
require.NotNil(t, resp.Devcontainers[0].Container, "devcontainer should have container reference")
612-
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusStarting, resp.Devcontainers[0].Container.DevcontainerStatus, "container dc status is not starting")
613612

614613
// Allow the devcontainer CLI to continue the up process.
615614
close(tt.devcontainerCLI.upErrC)
@@ -637,7 +636,6 @@ func TestAPI(t *testing.T) {
637636
require.Len(t, resp.Devcontainers, 1, "expected one devcontainer in response after error")
638637
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusError, resp.Devcontainers[0].Status, "devcontainer is not in an error state after up failure")
639638
require.NotNil(t, resp.Devcontainers[0].Container, "devcontainer should have container reference after up failure")
640-
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusError, resp.Devcontainers[0].Container.DevcontainerStatus, "container dc status is not error after up failure")
641639
return
642640
}
643641

@@ -662,7 +660,6 @@ func TestAPI(t *testing.T) {
662660
require.Len(t, resp.Devcontainers, 1, "expected one devcontainer in response after recreation")
663661
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, resp.Devcontainers[0].Status, "devcontainer is not running after recreation")
664662
require.NotNil(t, resp.Devcontainers[0].Container, "devcontainer should have container reference after recreation")
665-
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, resp.Devcontainers[0].Container.DevcontainerStatus, "container dc status is not running after recreation")
666663
})
667664
}
668665
})
@@ -757,7 +754,6 @@ func TestAPI(t *testing.T) {
757754
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, dc.Status)
758755
require.NotNil(t, dc.Container)
759756
assert.Equal(t, "runtime-container-1", dc.Container.ID)
760-
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, dc.Container.DevcontainerStatus)
761757
},
762758
},
763759
{
@@ -802,10 +798,8 @@ func TestAPI(t *testing.T) {
802798

803799
require.NotNil(t, known1.Container)
804800
assert.Equal(t, "known-container-1", known1.Container.ID)
805-
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, known1.Container.DevcontainerStatus)
806801
require.NotNil(t, runtime1.Container)
807802
assert.Equal(t, "runtime-container-1", runtime1.Container.ID)
808-
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, runtime1.Container.DevcontainerStatus)
809803
},
810804
},
811805
{
@@ -845,11 +839,9 @@ func TestAPI(t *testing.T) {
845839

846840
require.NotNil(t, running.Container, "running container should have container reference")
847841
assert.Equal(t, "running-container", running.Container.ID)
848-
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, running.Container.DevcontainerStatus)
849842

850843
require.NotNil(t, nonRunning.Container, "non-running container should have container reference")
851844
assert.Equal(t, "non-running-container", nonRunning.Container.ID)
852-
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusStopped, nonRunning.Container.DevcontainerStatus)
853845
},
854846
},
855847
{
@@ -885,7 +877,6 @@ func TestAPI(t *testing.T) {
885877
assert.NotEmpty(t, dc2.ConfigPath)
886878
require.NotNil(t, dc2.Container)
887879
assert.Equal(t, "known-container-2", dc2.Container.ID)
888-
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, dc2.Container.DevcontainerStatus)
889880
},
890881
},
891882
{
@@ -1185,8 +1176,6 @@ func TestAPI(t *testing.T) {
11851176
"devcontainer should not be marked as dirty initially")
11861177
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, response.Devcontainers[0].Status, "devcontainer should be running initially")
11871178
require.NotNil(t, response.Devcontainers[0].Container, "container should not be nil")
1188-
assert.False(t, response.Devcontainers[0].Container.DevcontainerDirty,
1189-
"container should not be marked as dirty initially")
11901179

11911180
// Verify the watcher is watching the config file.
11921181
assert.Contains(t, fWatcher.addedPaths, configPath,
@@ -1220,8 +1209,6 @@ func TestAPI(t *testing.T) {
12201209
"container should be marked as dirty after config file was modified")
12211210
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, response.Devcontainers[0].Status, "devcontainer should be running after config file was modified")
12221211
require.NotNil(t, response.Devcontainers[0].Container, "container should not be nil")
1223-
assert.True(t, response.Devcontainers[0].Container.DevcontainerDirty,
1224-
"container should be marked as dirty after config file was modified")
12251212

12261213
container.ID = "new-container-id" // Simulate a new container ID after recreation.
12271214
container.FriendlyName = "new-container-name"
@@ -1246,8 +1233,6 @@ func TestAPI(t *testing.T) {
12461233
"dirty flag should be cleared on the devcontainer after container recreation")
12471234
assert.Equal(t, codersdk.WorkspaceAgentDevcontainerStatusRunning, response.Devcontainers[0].Status, "devcontainer should be running after recreation")
12481235
require.NotNil(t, response.Devcontainers[0].Container, "container should not be nil")
1249-
assert.False(t, response.Devcontainers[0].Container.DevcontainerDirty,
1250-
"dirty flag should be cleared on the container after container recreation")
12511236
})
12521237

12531238
t.Run("SubAgentLifecycle", func(t *testing.T) {

coderd/apidoc/docs.go

Lines changed: 39 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 39 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/workspaceagents_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,15 +1403,13 @@ func TestWorkspaceAgentRecreateDevcontainer(t *testing.T) {
14031403
agentcontainers.DevcontainerConfigFileLabel: configFile,
14041404
}
14051405
devContainer = codersdk.WorkspaceAgentContainer{
1406-
ID: uuid.NewString(),
1407-
CreatedAt: dbtime.Now(),
1408-
FriendlyName: testutil.GetRandomName(t),
1409-
Image: "busybox:latest",
1410-
Labels: dcLabels,
1411-
Running: true,
1412-
Status: "running",
1413-
DevcontainerDirty: true,
1414-
DevcontainerStatus: codersdk.WorkspaceAgentDevcontainerStatusRunning,
1406+
ID: uuid.NewString(),
1407+
CreatedAt: dbtime.Now(),
1408+
FriendlyName: testutil.GetRandomName(t),
1409+
Image: "busybox:latest",
1410+
Labels: dcLabels,
1411+
Running: true,
1412+
Status: "running",
14151413
}
14161414
plainContainer = codersdk.WorkspaceAgentContainer{
14171415
ID: uuid.NewString(),

codersdk/workspaceagents.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,6 @@ type WorkspaceAgentContainer struct {
450450
// Volumes is a map of "things" mounted into the container. Again, this
451451
// is somewhat implementation-dependent.
452452
Volumes map[string]string `json:"volumes"`
453-
// DevcontainerStatus is the status of the devcontainer, if this
454-
// container is a devcontainer. This is used to determine if the
455-
// devcontainer is running, stopped, starting, or in an error state.
456-
DevcontainerStatus WorkspaceAgentDevcontainerStatus `json:"devcontainer_status,omitempty"`
457-
// DevcontainerDirty is true if the devcontainer configuration has changed
458-
// since the container was created. This is used to determine if the
459-
// container needs to be rebuilt.
460-
DevcontainerDirty bool `json:"devcontainer_dirty"`
461453
}
462454

463455
func (c *WorkspaceAgentContainer) Match(idOrName string) bool {
@@ -486,6 +478,8 @@ type WorkspaceAgentContainerPort struct {
486478
// WorkspaceAgentListContainersResponse is the response to the list containers
487479
// request.
488480
type WorkspaceAgentListContainersResponse struct {
481+
// Devcontainers is a list of devcontainers visible to the workspace agent.
482+
Devcontainers []WorkspaceAgentDevcontainer `json:"devcontainers"`
489483
// Containers is a list of containers visible to the workspace agent.
490484
Containers []WorkspaceAgentContainer `json:"containers"`
491485
// Warnings is a list of warnings that may have occurred during the

0 commit comments

Comments
 (0)