- Notifications
You must be signed in to change notification settings - Fork925
refactor(agent/agentcontainers): implement API service#17340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
mafredri merged 2 commits intomainfrommafredri/feat-agent-recreate-devcontainer-refactorApr 11, 2025
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
205 changes: 205 additions & 0 deletionsagent/agentcontainers/api.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
package agentcontainers | ||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"slices" | ||
"time" | ||
"github.com/go-chi/chi/v5" | ||
"golang.org/x/xerrors" | ||
"cdr.dev/slog" | ||
"github.com/coder/coder/v2/agent/agentexec" | ||
"github.com/coder/coder/v2/coderd/httpapi" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/quartz" | ||
) | ||
const ( | ||
defaultGetContainersCacheDuration = 10 * time.Second | ||
dockerCreatedAtTimeFormat = "2006-01-02 15:04:05 -0700 MST" | ||
getContainersTimeout = 5 * time.Second | ||
) | ||
// API is responsible for container-related operations in the agent. | ||
// It provides methods to list and manage containers. | ||
type API struct { | ||
cacheDuration time.Duration | ||
cl Lister | ||
dccli DevcontainerCLI | ||
clock quartz.Clock | ||
// lockCh protects the below fields. We use a channel instead of a mutex so we | ||
// can handle cancellation properly. | ||
lockCh chan struct{} | ||
containers codersdk.WorkspaceAgentListContainersResponse | ||
mtime time.Time | ||
} | ||
// Option is a functional option for API. | ||
type Option func(*API) | ||
// WithLister sets the agentcontainers.Lister implementation to use. | ||
// The default implementation uses the Docker CLI to list containers. | ||
func WithLister(cl Lister) Option { | ||
return func(api *API) { | ||
api.cl = cl | ||
} | ||
} | ||
func WithDevcontainerCLI(dccli DevcontainerCLI) Option { | ||
return func(api *API) { | ||
api.dccli = dccli | ||
} | ||
} | ||
// NewAPI returns a new API with the given options applied. | ||
func NewAPI(logger slog.Logger, options ...Option) *API { | ||
api := &API{ | ||
clock: quartz.NewReal(), | ||
cacheDuration: defaultGetContainersCacheDuration, | ||
lockCh: make(chan struct{}, 1), | ||
} | ||
for _, opt := range options { | ||
opt(api) | ||
} | ||
if api.cl == nil { | ||
api.cl = &DockerCLILister{} | ||
} | ||
if api.dccli == nil { | ||
api.dccli = NewDevcontainerCLI(logger, agentexec.DefaultExecer) | ||
} | ||
return api | ||
} | ||
// Routes returns the HTTP handler for container-related routes. | ||
func (api *API) Routes() http.Handler { | ||
r := chi.NewRouter() | ||
r.Get("/", api.handleList) | ||
r.Post("/{id}/recreate", api.handleRecreate) | ||
return r | ||
} | ||
// handleList handles the HTTP request to list containers. | ||
func (api *API) handleList(rw http.ResponseWriter, r *http.Request) { | ||
select { | ||
case <-r.Context().Done(): | ||
// Client went away. | ||
return | ||
default: | ||
ct, err := api.getContainers(r.Context()) | ||
if err != nil { | ||
if errors.Is(err, context.Canceled) { | ||
httpapi.Write(r.Context(), rw, http.StatusRequestTimeout, codersdk.Response{ | ||
Message: "Could not get containers.", | ||
Detail: "Took too long to list containers.", | ||
}) | ||
return | ||
} | ||
httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Could not get containers.", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
httpapi.Write(r.Context(), rw, http.StatusOK, ct) | ||
} | ||
} | ||
func copyListContainersResponse(resp codersdk.WorkspaceAgentListContainersResponse) codersdk.WorkspaceAgentListContainersResponse { | ||
return codersdk.WorkspaceAgentListContainersResponse{ | ||
Containers: slices.Clone(resp.Containers), | ||
Warnings: slices.Clone(resp.Warnings), | ||
} | ||
} | ||
func (api *API) getContainers(ctx context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) { | ||
select { | ||
case <-ctx.Done(): | ||
return codersdk.WorkspaceAgentListContainersResponse{}, ctx.Err() | ||
default: | ||
api.lockCh <- struct{}{} | ||
} | ||
defer func() { | ||
<-api.lockCh | ||
}() | ||
now := api.clock.Now() | ||
if now.Sub(api.mtime) < api.cacheDuration { | ||
return copyListContainersResponse(api.containers), nil | ||
} | ||
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, getContainersTimeout) | ||
defer timeoutCancel() | ||
updated, err := api.cl.List(timeoutCtx) | ||
if err != nil { | ||
return codersdk.WorkspaceAgentListContainersResponse{}, xerrors.Errorf("get containers: %w", err) | ||
} | ||
api.containers = updated | ||
api.mtime = now | ||
return copyListContainersResponse(api.containers), nil | ||
} | ||
// handleRecreate handles the HTTP request to recreate a container. | ||
func (api *API) handleRecreate(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
id := chi.URLParam(r, "id") | ||
if id == "" { | ||
httpapi.Write(ctx, w, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Missing container ID or name", | ||
Detail: "Container ID or name is required to recreate a devcontainer.", | ||
}) | ||
return | ||
} | ||
containers, err := api.cl.List(ctx) | ||
if err != nil { | ||
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Could not list containers", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
containerIdx := slices.IndexFunc(containers.Containers, func(c codersdk.WorkspaceAgentContainer) bool { | ||
return c.Match(id) | ||
}) | ||
if containerIdx == -1 { | ||
httpapi.Write(ctx, w, http.StatusNotFound, codersdk.Response{ | ||
Message: "Container not found", | ||
Detail: "Container ID or name not found in the list of containers.", | ||
}) | ||
return | ||
} | ||
container := containers.Containers[containerIdx] | ||
workspaceFolder := container.Labels[DevcontainerLocalFolderLabel] | ||
configPath := container.Labels[DevcontainerConfigFileLabel] | ||
// Workspace folder is required to recreate a container, we don't verify | ||
// the config path here because it's optional. | ||
if workspaceFolder == "" { | ||
httpapi.Write(ctx, w, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Missing workspace folder label", | ||
Detail: "The workspace folder label is required to recreate a devcontainer.", | ||
}) | ||
return | ||
} | ||
_, err = api.dccli.Up(ctx, workspaceFolder, configPath, WithRemoveExistingContainer()) | ||
if err != nil { | ||
httpapi.Write(ctx, w, http.StatusInternalServerError, codersdk.Response{ | ||
Message: "Could not recreate devcontainer", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
w.WriteHeader(http.StatusNoContent) | ||
} |
161 changes: 161 additions & 0 deletionsagent/agentcontainers/api_internal_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package agentcontainers | ||
import ( | ||
"math/rand" | ||
"strings" | ||
"testing" | ||
"time" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/slogtest" | ||
"github.com/coder/coder/v2/agent/agentcontainers/acmock" | ||
"github.com/coder/coder/v2/codersdk" | ||
"github.com/coder/coder/v2/testutil" | ||
"github.com/coder/quartz" | ||
) | ||
func TestAPI(t *testing.T) { | ||
t.Parallel() | ||
// List tests the API.getContainers method using a mock | ||
// implementation. It specifically tests caching behavior. | ||
t.Run("List", func(t *testing.T) { | ||
t.Parallel() | ||
fakeCt := fakeContainer(t) | ||
fakeCt2 := fakeContainer(t) | ||
makeResponse := func(cts ...codersdk.WorkspaceAgentContainer) codersdk.WorkspaceAgentListContainersResponse { | ||
return codersdk.WorkspaceAgentListContainersResponse{Containers: cts} | ||
} | ||
// Each test case is called multiple times to ensure idempotency | ||
for _, tc := range []struct { | ||
name string | ||
// data to be stored in the handler | ||
cacheData codersdk.WorkspaceAgentListContainersResponse | ||
// duration of cache | ||
cacheDur time.Duration | ||
// relative age of the cached data | ||
cacheAge time.Duration | ||
// function to set up expectations for the mock | ||
setupMock func(*acmock.MockLister) | ||
// expected result | ||
expected codersdk.WorkspaceAgentListContainersResponse | ||
// expected error | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "no cache", | ||
setupMock: func(mcl *acmock.MockLister) { | ||
mcl.EXPECT().List(gomock.Any()).Return(makeResponse(fakeCt), nil).AnyTimes() | ||
}, | ||
expected: makeResponse(fakeCt), | ||
}, | ||
{ | ||
name: "no data", | ||
cacheData: makeResponse(), | ||
cacheAge: 2 * time.Second, | ||
cacheDur: time.Second, | ||
setupMock: func(mcl *acmock.MockLister) { | ||
mcl.EXPECT().List(gomock.Any()).Return(makeResponse(fakeCt), nil).AnyTimes() | ||
}, | ||
expected: makeResponse(fakeCt), | ||
}, | ||
{ | ||
name: "cached data", | ||
cacheAge: time.Second, | ||
cacheData: makeResponse(fakeCt), | ||
cacheDur: 2 * time.Second, | ||
expected: makeResponse(fakeCt), | ||
}, | ||
{ | ||
name: "lister error", | ||
setupMock: func(mcl *acmock.MockLister) { | ||
mcl.EXPECT().List(gomock.Any()).Return(makeResponse(), assert.AnError).AnyTimes() | ||
}, | ||
expectedErr: assert.AnError.Error(), | ||
}, | ||
{ | ||
name: "stale cache", | ||
cacheAge: 2 * time.Second, | ||
cacheData: makeResponse(fakeCt), | ||
cacheDur: time.Second, | ||
setupMock: func(mcl *acmock.MockLister) { | ||
mcl.EXPECT().List(gomock.Any()).Return(makeResponse(fakeCt2), nil).AnyTimes() | ||
}, | ||
expected: makeResponse(fakeCt2), | ||
}, | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
var ( | ||
ctx = testutil.Context(t, testutil.WaitShort) | ||
clk = quartz.NewMock(t) | ||
ctrl = gomock.NewController(t) | ||
mockLister = acmock.NewMockLister(ctrl) | ||
now = time.Now().UTC() | ||
logger = slogtest.Make(t, nil).Leveled(slog.LevelDebug) | ||
api = NewAPI(logger, WithLister(mockLister)) | ||
) | ||
api.cacheDuration = tc.cacheDur | ||
api.clock = clk | ||
api.containers = tc.cacheData | ||
if tc.cacheAge != 0 { | ||
api.mtime = now.Add(-tc.cacheAge) | ||
} | ||
if tc.setupMock != nil { | ||
tc.setupMock(mockLister) | ||
} | ||
clk.Set(now).MustWait(ctx) | ||
// Repeat the test to ensure idempotency | ||
for i := 0; i < 2; i++ { | ||
actual, err := api.getContainers(ctx) | ||
if tc.expectedErr != "" { | ||
require.Empty(t, actual, "expected no data (attempt %d)", i) | ||
require.ErrorContains(t, err, tc.expectedErr, "expected error (attempt %d)", i) | ||
} else { | ||
require.NoError(t, err, "expected no error (attempt %d)", i) | ||
require.Equal(t, tc.expected, actual, "expected containers to be equal (attempt %d)", i) | ||
} | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
func fakeContainer(t *testing.T, mut ...func(*codersdk.WorkspaceAgentContainer)) codersdk.WorkspaceAgentContainer { | ||
t.Helper() | ||
ct := codersdk.WorkspaceAgentContainer{ | ||
CreatedAt: time.Now().UTC(), | ||
ID: uuid.New().String(), | ||
FriendlyName: testutil.GetRandomName(t), | ||
Image: testutil.GetRandomName(t) + ":" + strings.Split(uuid.New().String(), "-")[0], | ||
Labels: map[string]string{ | ||
testutil.GetRandomName(t): testutil.GetRandomName(t), | ||
}, | ||
Running: true, | ||
Ports: []codersdk.WorkspaceAgentContainerPort{ | ||
{ | ||
Network: "tcp", | ||
Port: testutil.RandomPortNoListen(t), | ||
HostPort: testutil.RandomPortNoListen(t), | ||
//nolint:gosec // this is a test | ||
HostIP: []string{"127.0.0.1", "[::1]", "localhost", "0.0.0.0", "[::]", testutil.GetRandomName(t)}[rand.Intn(6)], | ||
}, | ||
}, | ||
Status: testutil.MustRandString(t, 10), | ||
Volumes: map[string]string{testutil.GetRandomName(t): testutil.GetRandomName(t)}, | ||
} | ||
for _, m := range mut { | ||
m(&ct) | ||
} | ||
return ct | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.