WithDevcontainers sets the known devcontainers for the API. This allows the API to be aware of devcontainers defined in the workspace agent manifest.
(devcontainers []codersdk.WorkspaceAgentDevcontainer, scripts []codersdk.WorkspaceAgentScript)
| 219 | // allows the API to be aware of devcontainers defined in the workspace |
| 220 | // agent manifest. |
| 221 | func WithDevcontainers(devcontainers []codersdk.WorkspaceAgentDevcontainer, scripts []codersdk.WorkspaceAgentScript) Option { |
| 222 | return func(api *API) { |
| 223 | if len(devcontainers) == 0 { |
| 224 | return |
| 225 | } |
| 226 | api.knownDevcontainers = make(map[string]codersdk.WorkspaceAgentDevcontainer, len(devcontainers)) |
| 227 | api.devcontainerNames = make(map[string]bool, len(devcontainers)) |
| 228 | api.devcontainerLogSourceIDs = make(map[string]uuid.UUID) |
| 229 | for _, dc := range devcontainers { |
| 230 | if dc.Status == "" { |
| 231 | dc.Status = codersdk.WorkspaceAgentDevcontainerStatusStarting |
| 232 | } |
| 233 | logger := api.logger.With( |
| 234 | slog.F("devcontainer_id", dc.ID), |
| 235 | slog.F("devcontainer_name", dc.Name), |
| 236 | slog.F("workspace_folder", dc.WorkspaceFolder), |
| 237 | slog.F("config_path", dc.ConfigPath), |
| 238 | ) |
| 239 | |
| 240 | // Devcontainers have a name originating from Terraform, but |
| 241 | // we need to ensure that the name is unique. We will use |
| 242 | // the workspace folder name to generate a unique agent name, |
| 243 | // and if that fails, we will fall back to the devcontainers |
| 244 | // original name. |
| 245 | name, usingWorkspaceFolder := api.makeAgentName(dc.WorkspaceFolder, dc.Name) |
| 246 | if name != dc.Name { |
| 247 | logger = logger.With(slog.F("devcontainer_name", name)) |
| 248 | logger.Debug(api.ctx, "updating devcontainer name", slog.F("devcontainer_old_name", dc.Name)) |
| 249 | dc.Name = name |
| 250 | api.usingWorkspaceFolderName[dc.WorkspaceFolder] = usingWorkspaceFolder |
| 251 | } |
| 252 | |
| 253 | api.knownDevcontainers[dc.WorkspaceFolder] = dc |
| 254 | api.devcontainerNames[dc.Name] = true |
| 255 | for _, script := range scripts { |
| 256 | // The devcontainer scripts match the devcontainer ID for |
| 257 | // identification. |
| 258 | if script.ID == dc.ID { |
| 259 | api.devcontainerLogSourceIDs[dc.WorkspaceFolder] = script.LogSourceID |
| 260 | break |
| 261 | } |
| 262 | } |
| 263 | if api.devcontainerLogSourceIDs[dc.WorkspaceFolder] == uuid.Nil { |
| 264 | logger.Error(api.ctx, "devcontainer log source ID not found for devcontainer") |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | // WithWatcher sets the file watcher implementation to use. By default a |
| 271 | // noop watcher is used. This can be used in tests to modify the watcher |