- Notifications
You must be signed in to change notification settings - Fork921
feat(agent/agentcontainers): fall back to workspace folder name#18466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
DanielleMaywood wants to merge4 commits intomainChoose a base branch fromdm-devcontainer-folder-name
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
0660cba
feat(agent/agentcontainers): fall back to workspace folder name
DanielleMaywood7d48630
chore: appease linter and add test case
DanielleMaywood72d9e0a
chore: grammar
DanielleMaywood82f8e3f
chore: move subAgentConfig.Directory assignment
DanielleMaywoodFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
62 changes: 35 additions & 27 deletionsagent/agentcontainers/api.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
package agentcontainers | ||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"regexp" | ||
"runtime" | ||
"slices" | ||
"strings" | ||
@@ -585,10 +584,10 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code | ||
if dc.Container != nil { | ||
if !api.devcontainerNames[dc.Name] { | ||
// If the devcontainer name wasn't set via terraform, we | ||
//will attempt to create an agentnamebased on the workspace | ||
//folder's name. If that is not possible, we will fall back | ||
//to using the container's friendly name. | ||
dc.Name =safeAgentName(filepath.Base(dc.WorkspaceFolder),dc.Container.FriendlyName) | ||
} | ||
} | ||
@@ -633,6 +632,34 @@ func (api *API) processUpdatedContainersLocked(ctx context.Context, updated code | ||
api.containersErr = nil | ||
} | ||
var consecutiveHyphenRegex = regexp.MustCompile("-+") | ||
// `safeAgentName` returns a safe agent name derived from a folder name, | ||
// falling back to the container’s friendly name if needed. | ||
func safeAgentName(name string, friendlyName string) string { | ||
// Keep only ASCII letters and digits, replacing everything | ||
// else with a hyphen. | ||
var sb strings.Builder | ||
for _, r := range strings.ToLower(name) { | ||
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') { | ||
_, _ = sb.WriteRune(r) | ||
} else { | ||
_, _ = sb.WriteRune('-') | ||
} | ||
} | ||
// Remove any consecutive hyphens, and then trim any leading | ||
// and trailing hyphens. | ||
name = consecutiveHyphenRegex.ReplaceAllString(sb.String(), "-") | ||
name = strings.Trim(name, "-") | ||
if name == "" { | ||
return safeFriendlyName(friendlyName) | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
return name | ||
} | ||
// safeFriendlyName returns a API safe version of the container's | ||
// friendly name. | ||
// | ||
@@ -1114,27 +1141,6 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c | ||
if proc.agent.ID == uuid.Nil || maybeRecreateSubAgent { | ||
subAgentConfig.Architecture = arch | ||
displayAppsMap := map[codersdk.DisplayApp]bool{ | ||
// NOTE(DanielleMaywood): | ||
// We use the same defaults here as set in terraform-provider-coder. | ||
@@ -1167,6 +1173,8 @@ func (api *API) maybeInjectSubAgentIntoContainerLocked(ctx context.Context, dc c | ||
return err | ||
} | ||
subAgentConfig.Directory = config.Workspace.WorkspaceFolder | ||
// NOTE(DanielleMaywood): | ||
// We only want to take an agent name specified in the root customization layer. | ||
// This restricts the ability for a feature to specify the agent name. We may revisit | ||
195 changes: 195 additions & 0 deletionsagent/agentcontainers/api_internal_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
package agentcontainers | ||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/coder/coder/v2/provisioner" | ||
) | ||
func TestSafeAgentName(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
folderName string | ||
expected string | ||
}{ | ||
// Basic valid names | ||
{ | ||
folderName: "simple", | ||
expected: "simple", | ||
}, | ||
{ | ||
folderName: "with-hyphens", | ||
expected: "with-hyphens", | ||
}, | ||
{ | ||
folderName: "123numbers", | ||
expected: "123numbers", | ||
}, | ||
{ | ||
folderName: "mixed123", | ||
expected: "mixed123", | ||
}, | ||
// Names that need transformation | ||
{ | ||
folderName: "With_Underscores", | ||
expected: "with-underscores", | ||
}, | ||
{ | ||
folderName: "With Spaces", | ||
expected: "with-spaces", | ||
}, | ||
{ | ||
folderName: "UPPERCASE", | ||
expected: "uppercase", | ||
}, | ||
{ | ||
folderName: "Mixed_Case-Name", | ||
expected: "mixed-case-name", | ||
}, | ||
// Names with special characters that get replaced | ||
{ | ||
folderName: "special@#$chars", | ||
expected: "special-chars", | ||
}, | ||
{ | ||
folderName: "dots.and.more", | ||
expected: "dots-and-more", | ||
}, | ||
{ | ||
folderName: "multiple___underscores", | ||
expected: "multiple-underscores", | ||
}, | ||
{ | ||
folderName: "multiple---hyphens", | ||
expected: "multiple-hyphens", | ||
}, | ||
// Edge cases with leading/trailing special chars | ||
{ | ||
folderName: "-leading-hyphen", | ||
expected: "leading-hyphen", | ||
}, | ||
{ | ||
folderName: "trailing-hyphen-", | ||
expected: "trailing-hyphen", | ||
}, | ||
{ | ||
folderName: "_leading_underscore", | ||
expected: "leading-underscore", | ||
}, | ||
{ | ||
folderName: "trailing_underscore_", | ||
expected: "trailing-underscore", | ||
}, | ||
{ | ||
folderName: "---multiple-leading", | ||
expected: "multiple-leading", | ||
}, | ||
{ | ||
folderName: "trailing-multiple---", | ||
expected: "trailing-multiple", | ||
}, | ||
// Complex transformation cases | ||
{ | ||
folderName: "___very---complex@@@name___", | ||
expected: "very-complex-name", | ||
}, | ||
{ | ||
folderName: "my.project-folder_v2", | ||
expected: "my-project-folder-v2", | ||
}, | ||
// Empty and fallback cases - now correctly uses friendlyName fallback | ||
{ | ||
folderName: "", | ||
expected: "friendly-fallback", | ||
}, | ||
{ | ||
folderName: "---", | ||
expected: "friendly-fallback", | ||
}, | ||
{ | ||
folderName: "___", | ||
expected: "friendly-fallback", | ||
}, | ||
{ | ||
folderName: "@#$", | ||
expected: "friendly-fallback", | ||
}, | ||
// Additional edge cases | ||
{ | ||
folderName: "a", | ||
expected: "a", | ||
}, | ||
{ | ||
folderName: "1", | ||
expected: "1", | ||
}, | ||
{ | ||
folderName: "a1b2c3", | ||
expected: "a1b2c3", | ||
}, | ||
{ | ||
folderName: "CamelCase", | ||
expected: "camelcase", | ||
}, | ||
{ | ||
folderName: "snake_case_name", | ||
expected: "snake-case-name", | ||
}, | ||
{ | ||
folderName: "kebab-case-name", | ||
expected: "kebab-case-name", | ||
}, | ||
{ | ||
folderName: "mix3d_C4s3-N4m3", | ||
expected: "mix3d-c4s3-n4m3", | ||
}, | ||
{ | ||
folderName: "123-456-789", | ||
expected: "123-456-789", | ||
}, | ||
{ | ||
folderName: "abc123def456", | ||
expected: "abc123def456", | ||
}, | ||
{ | ||
folderName: " spaces everywhere ", | ||
expected: "spaces-everywhere", | ||
}, | ||
{ | ||
folderName: "unicode-café-naïve", | ||
expected: "unicode-caf-na-ve", | ||
}, | ||
{ | ||
folderName: "path/with/slashes", | ||
expected: "path-with-slashes", | ||
}, | ||
{ | ||
folderName: "file.tar.gz", | ||
expected: "file-tar-gz", | ||
}, | ||
{ | ||
folderName: "version-1.2.3-alpha", | ||
expected: "version-1-2-3-alpha", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.folderName, func(t *testing.T) { | ||
t.Parallel() | ||
name := safeAgentName(tt.folderName, "friendly-fallback") | ||
assert.Equal(t, tt.expected, name) | ||
assert.True(t, provisioner.AgentNameRegex.Match([]byte(name))) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.