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

Skip to content

Commit fd69221

Browse files
committed
update agentcontainers API file watch init
1 parent 2a9a842 commit fd69221

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

agent/agent.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,9 @@ type agent struct {
272272
metrics *agentMetrics
273273
execer agentexec.Execer
274274

275-
containerAPIOptions []agentcontainers.Option
276275
experimentalDevcontainersEnabled bool
276+
containerAPIOptions []agentcontainers.Option
277+
containerAPI *agentcontainers.API
277278
}
278279

279280
func (a *agent) TailnetConn() *tailnet.Conn {
@@ -1167,6 +1168,11 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
11671168
}
11681169
a.metrics.startupScriptSeconds.WithLabelValues(label).Set(dur)
11691170
a.scriptRunner.StartCron()
1171+
if a.containerAPI != nil {
1172+
// Start the containerAPI service after
1173+
// devcontainers have been started.
1174+
a.containerAPI.Start()
1175+
}
11701176
})
11711177
if err != nil {
11721178
return xerrors.Errorf("track conn goroutine: %w", err)

agent/agentcontainers/api.go

+18-9
Original file line numberDiff line numberDiff line change
@@ -146,21 +146,30 @@ func NewAPI(logger slog.Logger, options ...Option) *API {
146146
}
147147
}
148148

149+
go api.loop()
150+
151+
return api
152+
}
153+
154+
// Start watching for devcontainer config file changes and prime the
155+
// cache with the current list of containers.
156+
func (api *API) Start() {
157+
// Prime the cache with the current list of containers.
158+
_, _ = api.cl.List(api.ctx)
159+
149160
// Make sure we watch the devcontainer config files for changes.
150161
for _, devcontainer := range api.knownDevcontainers {
151-
if devcontainer.ConfigPath != "" {
152-
if err := api.watcher.Add(devcontainer.ConfigPath); err != nil {
153-
api.logger.Error(ctx, "watch devcontainer config file failed", slog.Error(err), slog.F("file", devcontainer.ConfigPath))
154-
}
162+
if devcontainer.ConfigPath == "" {
163+
continue
155164
}
156-
}
157-
158-
go api.start()
159165

160-
return api
166+
if err := api.watcher.Add(devcontainer.ConfigPath); err != nil {
167+
api.logger.Error(api.ctx, "watch devcontainer config file failed", slog.Error(err), slog.F("file", devcontainer.ConfigPath))
168+
}
169+
}
161170
}
162171

163-
func (api *API) start() {
172+
func (api *API) loop() {
164173
defer close(api.done)
165174

166175
for {

agent/agentcontainers/api_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"cdr.dev/slog"
1919
"cdr.dev/slog/sloggers/slogtest"
2020
"github.com/coder/coder/v2/agent/agentcontainers"
21+
"github.com/coder/coder/v2/agent/agentcontainers/watcher"
2122
"github.com/coder/coder/v2/codersdk"
2223
"github.com/coder/coder/v2/testutil"
2324
"github.com/coder/quartz"
@@ -253,6 +254,7 @@ func TestAPI(t *testing.T) {
253254
logger,
254255
agentcontainers.WithLister(tt.lister),
255256
agentcontainers.WithDevcontainerCLI(tt.devcontainerCLI),
257+
agentcontainers.WithWatcher(watcher.NewNoop()),
256258
)
257259
defer api.Close()
258260
r.Mount("/", api.Routes())
@@ -558,6 +560,7 @@ func TestAPI(t *testing.T) {
558560
r := chi.NewRouter()
559561
apiOptions := []agentcontainers.Option{
560562
agentcontainers.WithLister(tt.lister),
563+
agentcontainers.WithWatcher(watcher.NewNoop()),
561564
}
562565

563566
if len(tt.knownDevcontainers) > 0 {
@@ -631,6 +634,8 @@ func TestAPI(t *testing.T) {
631634
)
632635
defer api.Close()
633636

637+
api.Start()
638+
634639
r := chi.NewRouter()
635640
r.Mount("/", api.Routes())
636641

agent/api.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
3737
cacheDuration: cacheDuration,
3838
}
3939

40-
var containerAPI *agentcontainers.API
4140
if a.experimentalDevcontainersEnabled {
4241
containerAPIOpts := []agentcontainers.Option{
4342
agentcontainers.WithExecer(a.execer),
@@ -53,9 +52,8 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
5352
// Append after to allow the agent options to override the default options.
5453
containerAPIOpts = append(containerAPIOpts, a.containerAPIOptions...)
5554

56-
containerAPI = agentcontainers.NewAPI(a.logger.Named("containers"), containerAPIOpts...)
57-
58-
r.Mount("/api/v0/containers", containerAPI.Routes())
55+
a.containerAPI = agentcontainers.NewAPI(a.logger.Named("containers"), containerAPIOpts...)
56+
r.Mount("/api/v0/containers", a.containerAPI.Routes())
5957
} else {
6058
r.HandleFunc("/api/v0/containers", func(w http.ResponseWriter, r *http.Request) {
6159
httpapi.Write(r.Context(), w, http.StatusNotFound, codersdk.Response{
@@ -77,8 +75,8 @@ func (a *agent) apiHandler() (http.Handler, func() error) {
7775
r.Get("/debug/prometheus", promHandler.ServeHTTP)
7876

7977
return r, func() error {
80-
if containerAPI != nil {
81-
return containerAPI.Close()
78+
if a.containerAPI != nil {
79+
return a.containerAPI.Close()
8280
}
8381
return nil
8482
}

0 commit comments

Comments
 (0)