processUpdatedContainersLocked updates the devcontainer state based on the latest list of containers. This method assumes that api.mu is held.
(ctx context.Context, updated codersdk.WorkspaceAgentListContainersResponse)
| 903 | // on the latest list of containers. This method assumes that api.mu is |
| 904 | // held. |
| 905 | func (api *API) processUpdatedContainersLocked(ctx context.Context, updated codersdk.WorkspaceAgentListContainersResponse) { |
| 906 | dcFields := func(dc codersdk.WorkspaceAgentDevcontainer) []slog.Field { |
| 907 | f := []slog.Field{ |
| 908 | slog.F("devcontainer_id", dc.ID), |
| 909 | slog.F("devcontainer_name", dc.Name), |
| 910 | slog.F("workspace_folder", dc.WorkspaceFolder), |
| 911 | slog.F("config_path", dc.ConfigPath), |
| 912 | } |
| 913 | if dc.Container != nil { |
| 914 | f = append(f, slog.F("container_id", dc.Container.ID)) |
| 915 | f = append(f, slog.F("container_name", dc.Container.FriendlyName)) |
| 916 | } |
| 917 | return f |
| 918 | } |
| 919 | |
| 920 | // Reset the container links in known devcontainers to detect if |
| 921 | // they still exist. |
| 922 | for _, dc := range api.knownDevcontainers { |
| 923 | dc.Container = nil |
| 924 | api.knownDevcontainers[dc.WorkspaceFolder] = dc |
| 925 | } |
| 926 | |
| 927 | // Check if the container is running and update the known devcontainers. |
| 928 | for i := range updated.Containers { |
| 929 | container := &updated.Containers[i] // Grab a reference to the container to allow mutating it. |
| 930 | |
| 931 | workspaceFolder := container.Labels[DevcontainerLocalFolderLabel] |
| 932 | configFile := container.Labels[DevcontainerConfigFileLabel] |
| 933 | |
| 934 | if workspaceFolder == "" { |
| 935 | continue |
| 936 | } |
| 937 | |
| 938 | logger := api.logger.With( |
| 939 | slog.F("container_id", updated.Containers[i].ID), |
| 940 | slog.F("container_name", updated.Containers[i].FriendlyName), |
| 941 | slog.F("workspace_folder", workspaceFolder), |
| 942 | slog.F("config_file", configFile), |
| 943 | ) |
| 944 | |
| 945 | // If we haven't set any include filters, we should explicitly ignore test devcontainers. |
| 946 | if len(api.containerLabelIncludeFilter) == 0 && container.Labels[DevcontainerIsTestRunLabel] == "true" { |
| 947 | continue |
| 948 | } |
| 949 | |
| 950 | // Filter out devcontainer tests, unless explicitly set in include filters. |
| 951 | if len(api.containerLabelIncludeFilter) > 0 { |
| 952 | includeContainer := true |
| 953 | for label, value := range api.containerLabelIncludeFilter { |
| 954 | v, found := container.Labels[label] |
| 955 | |
| 956 | includeContainer = includeContainer && (found && v == value) |
| 957 | } |
| 958 | // Verbose debug logging is fine here since typically filters |
| 959 | // are only used in development or testing environments. |
| 960 | if !includeContainer { |
| 961 | logger.Debug(ctx, "container does not match include filter, ignoring devcontainer", slog.F("container_labels", container.Labels), slog.F("include_filter", api.containerLabelIncludeFilter)) |
| 962 | continue |
no test coverage detected