Skip to content

Commit e98caae

Browse files
committed
Merge branch 'main' into lilac/go-122-range-fix
2 parents c5ea297 + 556b095 commit e98caae

File tree

128 files changed

+5841
-1614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+5841
-1614
lines changed

agent/agent.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,7 @@ func (a *agent) updateCommandEnv(current []string) (updated []string, err error)
12961296
"CODER": "true",
12971297
"CODER_WORKSPACE_NAME": manifest.WorkspaceName,
12981298
"CODER_WORKSPACE_AGENT_NAME": manifest.AgentName,
1299+
"CODER_WORKSPACE_OWNER_NAME": manifest.OwnerName,
12991300

13001301
// Specific Coder subcommands require the agent token exposed!
13011302
"CODER_AGENT_TOKEN": *a.sessionToken.Load(),

agent/agent_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ func TestAgent_EnvironmentVariableExpansion(t *testing.T) {
12021202
func TestAgent_CoderEnvVars(t *testing.T) {
12031203
t.Parallel()
12041204

1205-
for _, key := range []string{"CODER", "CODER_WORKSPACE_NAME", "CODER_WORKSPACE_AGENT_NAME"} {
1205+
for _, key := range []string{"CODER", "CODER_WORKSPACE_NAME", "CODER_WORKSPACE_OWNER_NAME", "CODER_WORKSPACE_AGENT_NAME"} {
12061206
t.Run(key, func(t *testing.T) {
12071207
t.Parallel()
12081208

@@ -3067,6 +3067,9 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
30673067
if metadata.WorkspaceName == "" {
30683068
metadata.WorkspaceName = "test-workspace"
30693069
}
3070+
if metadata.OwnerName == "" {
3071+
metadata.OwnerName = "test-user"
3072+
}
30703073
if metadata.WorkspaceID == uuid.Nil {
30713074
metadata.WorkspaceID = uuid.New()
30723075
}

agent/agentcontainers/api.go

Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package agentcontainers
22

33
import (
4-
"bytes"
54
"context"
65
"errors"
76
"fmt"
8-
"io"
97
"net/http"
108
"os"
119
"path"
@@ -28,6 +26,7 @@ import (
2826
"github.com/coder/coder/v2/coderd/httpapi"
2927
"github.com/coder/coder/v2/codersdk"
3028
"github.com/coder/coder/v2/codersdk/agentsdk"
29+
"github.com/coder/coder/v2/provisioner"
3130
"github.com/coder/quartz"
3231
)
3332

@@ -1113,27 +1112,6 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11131112
if proc.agent.ID == uuid.Nil || maybeRecreateSubAgent {
11141113
subAgentConfig.Architecture = arch
11151114

1116-
// Detect workspace folder by executing `pwd` in the container.
1117-
// NOTE(mafredri): This is a quick and dirty way to detect the
1118-
// workspace folder inside the container. In the future we will
1119-
// rely more on `devcontainer read-configuration`.
1120-
var pwdBuf bytes.Buffer
1121-
err = api.dccli.Exec(ctx, dc.WorkspaceFolder, dc.ConfigPath, "pwd", []string{},
1122-
WithExecOutput(&pwdBuf, io.Discard),
1123-
WithExecContainerID(container.ID),
1124-
)
1125-
if err != nil {
1126-
return xerrors.Errorf("check workspace folder in container: %w", err)
1127-
}
1128-
directory := strings.TrimSpace(pwdBuf.String())
1129-
if directory == "" {
1130-
logger.Warn(ctx, "detected workspace folder is empty, using default workspace folder",
1131-
slog.F("default_workspace_folder", DevcontainerDefaultContainerWorkspaceFolder),
1132-
)
1133-
directory = DevcontainerDefaultContainerWorkspaceFolder
1134-
}
1135-
subAgentConfig.Directory = directory
1136-
11371115
displayAppsMap := map[codersdk.DisplayApp]bool{
11381116
// NOTE(DanielleMaywood):
11391117
// We use the same defaults here as set in terraform-provider-coder.
@@ -1145,18 +1123,55 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11451123
codersdk.DisplayAppPortForward: true,
11461124
}
11471125

1148-
var appsWithPossibleDuplicates []SubAgentApp
1126+
var (
1127+
appsWithPossibleDuplicates []SubAgentApp
1128+
workspaceFolder = DevcontainerDefaultContainerWorkspaceFolder
1129+
)
1130+
1131+
if err := func() error {
1132+
var (
1133+
config DevcontainerConfig
1134+
configOutdated bool
1135+
)
1136+
1137+
readConfig := func() (DevcontainerConfig, error) {
1138+
return api.dccli.ReadConfig(ctx, dc.WorkspaceFolder, dc.ConfigPath, []string{
1139+
fmt.Sprintf("CODER_WORKSPACE_AGENT_NAME=%s", subAgentConfig.Name),
1140+
fmt.Sprintf("CODER_WORKSPACE_OWNER_NAME=%s", api.ownerName),
1141+
fmt.Sprintf("CODER_WORKSPACE_NAME=%s", api.workspaceName),
1142+
fmt.Sprintf("CODER_URL=%s", api.subAgentURL),
1143+
})
1144+
}
1145+
1146+
if config, err = readConfig(); err != nil {
1147+
return err
1148+
}
1149+
1150+
workspaceFolder = config.Workspace.WorkspaceFolder
1151+
1152+
// NOTE(DanielleMaywood):
1153+
// We only want to take an agent name specified in the root customization layer.
1154+
// This restricts the ability for a feature to specify the agent name. We may revisit
1155+
// this in the future, but for now we want to restrict this behavior.
1156+
if name := config.Configuration.Customizations.Coder.Name; name != "" {
1157+
// We only want to pick this name if it is a valid name.
1158+
if provisioner.AgentNameRegex.Match([]byte(name)) {
1159+
subAgentConfig.Name = name
1160+
configOutdated = true
1161+
} else {
1162+
logger.Warn(ctx, "invalid name in devcontainer customization, ignoring",
1163+
slog.F("name", name),
1164+
slog.F("regex", provisioner.AgentNameRegex.String()),
1165+
)
1166+
}
1167+
}
1168+
1169+
if configOutdated {
1170+
if config, err = readConfig(); err != nil {
1171+
return err
1172+
}
1173+
}
11491174

1150-
if config, err := api.dccli.ReadConfig(ctx, dc.WorkspaceFolder, dc.ConfigPath,
1151-
[]string{
1152-
fmt.Sprintf("CODER_WORKSPACE_AGENT_NAME=%s", dc.Name),
1153-
fmt.Sprintf("CODER_WORKSPACE_OWNER_NAME=%s", api.ownerName),
1154-
fmt.Sprintf("CODER_WORKSPACE_NAME=%s", api.workspaceName),
1155-
fmt.Sprintf("CODER_URL=%s", api.subAgentURL),
1156-
},
1157-
); err != nil {
1158-
api.logger.Error(ctx, "unable to read devcontainer config", slog.Error(err))
1159-
} else {
11601175
coderCustomization := config.MergedConfiguration.Customizations.Coder
11611176

11621177
for _, customization := range coderCustomization {
@@ -1173,6 +1188,10 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
11731188

11741189
appsWithPossibleDuplicates = append(appsWithPossibleDuplicates, customization.Apps...)
11751190
}
1191+
1192+
return nil
1193+
}(); err != nil {
1194+
api.logger.Error(ctx, "unable to read devcontainer config", slog.Error(err))
11761195
}
11771196

11781197
displayApps := make([]codersdk.DisplayApp, 0, len(displayAppsMap))
@@ -1204,6 +1223,7 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c
12041223

12051224
subAgentConfig.DisplayApps = displayApps
12061225
subAgentConfig.Apps = apps
1226+
subAgentConfig.Directory = workspaceFolder
12071227
}
12081228

12091229
deleteSubAgent := proc.agent.ID != uuid.Nil && maybeRecreateSubAgent && !proc.agent.EqualConfig(subAgentConfig)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy