|
| 1 | +package agentcontainers |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + "slices" |
| 8 | + "time" |
| 9 | + |
| 10 | + "golang.org/x/xerrors" |
| 11 | + |
| 12 | + "github.com/coder/coder/v2/coderd/httpapi" |
| 13 | + "github.com/coder/coder/v2/codersdk" |
| 14 | + "github.com/coder/quartz" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + defaultGetContainersCacheDuration = 10 * time.Second |
| 19 | + dockerCreatedAtTimeFormat = "2006-01-02 15:04:05 -0700 MST" |
| 20 | + getContainersTimeout = 5 * time.Second |
| 21 | +) |
| 22 | + |
| 23 | +type devcontainersHandler struct { |
| 24 | + cacheDuration time.Duration |
| 25 | + cl Lister |
| 26 | + clock quartz.Clock |
| 27 | + |
| 28 | + // lockCh protects the below fields. We use a channel instead of a mutex so we |
| 29 | + // can handle cancellation properly. |
| 30 | + lockCh chan struct{} |
| 31 | + containers *codersdk.WorkspaceAgentListContainersResponse |
| 32 | + mtime time.Time |
| 33 | +} |
| 34 | + |
| 35 | +// Option is a functional option for devcontainersHandler. |
| 36 | +type Option func(*devcontainersHandler) |
| 37 | + |
| 38 | +// WithLister sets the agentcontainers.Lister implementation to use. |
| 39 | +// The default implementation uses the Docker CLI to list containers. |
| 40 | +func WithLister(cl Lister) Option { |
| 41 | + return func(ch *devcontainersHandler) { |
| 42 | + ch.cl = cl |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// New returns a new devcontainersHandler with the given options applied. |
| 47 | +func New(options ...Option) http.Handler { |
| 48 | + ch := &devcontainersHandler{ |
| 49 | + lockCh: make(chan struct{}, 1), |
| 50 | + } |
| 51 | + for _, opt := range options { |
| 52 | + opt(ch) |
| 53 | + } |
| 54 | + return ch |
| 55 | +} |
| 56 | + |
| 57 | +func (ch *devcontainersHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 58 | + select { |
| 59 | + case <-r.Context().Done(): |
| 60 | + // Client went away. |
| 61 | + return |
| 62 | + default: |
| 63 | + ct, err := ch.getContainers(r.Context()) |
| 64 | + if err != nil { |
| 65 | + if errors.Is(err, context.Canceled) { |
| 66 | + httpapi.Write(r.Context(), rw, http.StatusRequestTimeout, codersdk.Response{ |
| 67 | + Message: "Could not get containers.", |
| 68 | + Detail: "Took too long to list containers.", |
| 69 | + }) |
| 70 | + return |
| 71 | + } |
| 72 | + httpapi.Write(r.Context(), rw, http.StatusInternalServerError, codersdk.Response{ |
| 73 | + Message: "Could not get containers.", |
| 74 | + Detail: err.Error(), |
| 75 | + }) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + httpapi.Write(r.Context(), rw, http.StatusOK, ct) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func (ch *devcontainersHandler) getContainers(ctx context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) { |
| 84 | + select { |
| 85 | + case <-ctx.Done(): |
| 86 | + return codersdk.WorkspaceAgentListContainersResponse{}, ctx.Err() |
| 87 | + default: |
| 88 | + ch.lockCh <- struct{}{} |
| 89 | + } |
| 90 | + defer func() { |
| 91 | + <-ch.lockCh |
| 92 | + }() |
| 93 | + |
| 94 | + // make zero-value usable |
| 95 | + if ch.cacheDuration == 0 { |
| 96 | + ch.cacheDuration = defaultGetContainersCacheDuration |
| 97 | + } |
| 98 | + if ch.cl == nil { |
| 99 | + ch.cl = &DockerCLILister{} |
| 100 | + } |
| 101 | + if ch.containers == nil { |
| 102 | + ch.containers = &codersdk.WorkspaceAgentListContainersResponse{} |
| 103 | + } |
| 104 | + if ch.clock == nil { |
| 105 | + ch.clock = quartz.NewReal() |
| 106 | + } |
| 107 | + |
| 108 | + now := ch.clock.Now() |
| 109 | + if now.Sub(ch.mtime) < ch.cacheDuration { |
| 110 | + // Return a copy of the cached data to avoid accidental modification by the caller. |
| 111 | + cpy := codersdk.WorkspaceAgentListContainersResponse{ |
| 112 | + Containers: slices.Clone(ch.containers.Containers), |
| 113 | + Warnings: slices.Clone(ch.containers.Warnings), |
| 114 | + } |
| 115 | + return cpy, nil |
| 116 | + } |
| 117 | + |
| 118 | + timeoutCtx, timeoutCancel := context.WithTimeout(ctx, getContainersTimeout) |
| 119 | + defer timeoutCancel() |
| 120 | + updated, err := ch.cl.List(timeoutCtx) |
| 121 | + if err != nil { |
| 122 | + return codersdk.WorkspaceAgentListContainersResponse{}, xerrors.Errorf("get containers: %w", err) |
| 123 | + } |
| 124 | + ch.containers = &updated |
| 125 | + ch.mtime = now |
| 126 | + |
| 127 | + // Return a copy of the cached data to avoid accidental modification by the |
| 128 | + // caller. |
| 129 | + cpy := codersdk.WorkspaceAgentListContainersResponse{ |
| 130 | + Containers: slices.Clone(ch.containers.Containers), |
| 131 | + Warnings: slices.Clone(ch.containers.Warnings), |
| 132 | + } |
| 133 | + return cpy, nil |
| 134 | +} |
| 135 | + |
| 136 | +// Lister is an interface for listing containers visible to the |
| 137 | +// workspace agent. |
| 138 | +type Lister interface { |
| 139 | + // List returns a list of containers visible to the workspace agent. |
| 140 | + // This should include running and stopped containers. |
| 141 | + List(ctx context.Context) (codersdk.WorkspaceAgentListContainersResponse, error) |
| 142 | +} |
0 commit comments