- Notifications
You must be signed in to change notification settings - Fork923
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 from1 commit
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
NextNext commit
feat(agent/agentcontainers): fall back to workspace folder name
This PR changes the logic for how we decide on an agent name.Previously it followed these steps:1. Use a name from `customizations.coder.name`2. Use a name from the terraform resource `coder_devcontainer`3. Use the dev container's friendly nameWith this change it now does:1. Use a name from `customizations.coder.name`2. Use a name from the terraform resource `coder_devcontainer`3. Use a name from the workspace folder4. Use the dev container's friendly nameWe now attempt to construct a valid agent name from the workspacefolder. Should we fail to construct a valid agent name from theworkspace folder, we will fall back to the dev container's friendlyname.
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit0660cbaa459ca29a39c72d2107dcadba5b9eebcc
There are no files selected for viewing
68 changes: 41 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,21 +1,21 @@ | ||
package agentcontainers | ||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"regexp" | ||
"runtime" | ||
"slices" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
"unicode" | ||
"github.com/fsnotify/fsnotify" | ||
"github.com/go-chi/chi/v5" | ||
@@ -585,10 +585,10 @@ | ||
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 +633,39 @@ | ||
api.containersErr = nil | ||
} | ||
var consecutiveHyphenRegex = regexp.MustCompile("-+") | ||
// safeAgentName returns an agent name safe version | ||
// of a folder name, falling back to a safe version | ||
// of the container name if unable to create a name. | ||
DanielleMaywood marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
func safeAgentName(name string, friendlyName string) string { | ||
// Keep only letters and digits, replacing everything | ||
// else with a hyphen. | ||
var sb strings.Builder | ||
for _, r := range strings.ToLower(name) { | ||
if unicode.IsLetter(r) || unicode.IsDigit(r) { | ||
sb.WriteRune(r) | ||
} else { | ||
sb.WriteRune('-') | ||
} | ||
} | ||
// Remove any consecutive hyphens, and then trim and leading | ||
// and trailing hyphens. | ||
name = consecutiveHyphenRegex.ReplaceAllString(sb.String(), "-") | ||
name = strings.Trim(name, "-") | ||
name = strings.ToLower(name) | ||
name = strings.ReplaceAll(name, " ", "-") | ||
name = strings.ReplaceAll(name, "_", "-") | ||
if name == "" { | ||
return safeFriendlyName(name) | ||
} | ||
return name | ||
} | ||
// safeFriendlyName returns a API safe version of the container's | ||
// friendly name. | ||
// | ||
@@ -1114,27 +1147,6 @@ | ||
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. | ||
@@ -1207,6 +1219,8 @@ | ||
appsWithPossibleDuplicates = append(appsWithPossibleDuplicates, customization.Apps...) | ||
} | ||
subAgentConfig.Directory = config.Workspace.WorkspaceFolder | ||
return nil | ||
}(); err != nil { | ||
api.logger.Error(ctx, "unable to read devcontainer config", slog.Error(err)) | ||
65 changes: 23 additions & 42 deletionsagent/agentcontainers/api_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
2 changes: 0 additions & 2 deletionsagent/agentcontainers/devcontainer.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
5 changes: 5 additions & 0 deletionsagent/agentcontainers/devcontainercli.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
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.