- Notifications
You must be signed in to change notification settings - Fork926
fix!: use devcontainer ID when rebuilding a devcontainer#18604
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -494,8 +494,8 @@ func (api *API) Routes() http.Handler { | ||
r.Get("/", api.handleList) | ||
// TODO(mafredri): Simplify this route as the previous /devcontainers | ||
// /-route was dropped. We can drop the /devcontainers prefix here too. | ||
r.Route("/devcontainers/{devcontainer}", func(r chi.Router) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This dawned on me just now, but we could take There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Oooh that could be good to add in the future. | ||
r.Post("/recreate", api.handleDevcontainerRecreate) | ||
}) | ||
return r | ||
@@ -859,68 +859,40 @@ func (api *API) getContainers() (codersdk.WorkspaceAgentListContainersResponse, | ||
// devcontainer by referencing the container. | ||
func (api *API) handleDevcontainerRecreate(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
devcontainerID := chi.URLParam(r, "devcontainer") | ||
ifdevcontainerID == "" { | ||
httpapi.Write(ctx, w, http.StatusBadRequest, codersdk.Response{ | ||
Message: "Missing devcontainer ID", | ||
Detail: "Devcontainer ID is required to recreate a devcontainer.", | ||
}) | ||
return | ||
} | ||
api.mu.Lock() | ||
var dc codersdk.WorkspaceAgentDevcontainer | ||
for _, knownDC := range api.knownDevcontainers { | ||
if knownDC.ID.String() == devcontainerID { | ||
dc = knownDC | ||
break | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Suggestion: This could be simplified by storing | ||
} | ||
if dc.ID == uuid.Nil { | ||
api.mu.Unlock() | ||
httpapi.Write(ctx, w, http.StatusNotFound, codersdk.Response{ | ||
Message: "Devcontainer not found.", | ||
Detail: fmt.Sprintf("Could not find devcontainerwith ID: %q",devcontainerID), | ||
}) | ||
return | ||
} | ||
if dc.Status == codersdk.WorkspaceAgentDevcontainerStatusStarting { | ||
api.mu.Unlock() | ||
httpapi.Write(ctx, w, http.StatusConflict, codersdk.Response{ | ||
Message: "Devcontainer recreation already in progress", | ||
Detail: fmt.Sprintf("Recreation fordevcontainer%q is already underway.", dc.Name), | ||
}) | ||
return | ||
} | ||
@@ -931,14 +903,14 @@ func (api *API) handleDevcontainerRecreate(w http.ResponseWriter, r *http.Reques | ||
dc.Container = nil | ||
api.knownDevcontainers[dc.WorkspaceFolder] = dc | ||
go func() { | ||
_ = api.CreateDevcontainer(dc.WorkspaceFolder,dc.ConfigPath, WithRemoveExistingContainer()) | ||
}() | ||
api.mu.Unlock() | ||
httpapi.Write(ctx, w, http.StatusAccepted, codersdk.Response{ | ||
Message: "Devcontainer recreation initiated", | ||
Detail: fmt.Sprintf("Recreation process fordevcontainer%q has started.", dc.Name), | ||
}) | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -493,78 +493,77 @@ func TestAPI(t *testing.T) { | ||
t.Run("Recreate",func(t*testing.T) { | ||
t.Parallel() | ||
devcontainerID1:=uuid.New() | ||
devcontainerID2:=uuid.New() | ||
workspaceFolder1:="/workspace/test1" | ||
workspaceFolder2:="/workspace/test2" | ||
configPath1:="/workspace/test1/.devcontainer/devcontainer.json" | ||
configPath2:="/workspace/test2/.devcontainer/devcontainer.json" | ||
// Create a container that represents an existing devcontainer | ||
devContainer1:= codersdk.WorkspaceAgentContainer{ | ||
ID:"container-1", | ||
FriendlyName:"test-container-1", | ||
Running:true, | ||
Labels:map[string]string{ | ||
agentcontainers.DevcontainerLocalFolderLabel:workspaceFolder1, | ||
agentcontainers.DevcontainerConfigFileLabel:configPath1, | ||
}, | ||
} | ||
devContainer2:= codersdk.WorkspaceAgentContainer{ | ||
ID:"container-2", | ||
FriendlyName:"test-container-2", | ||
Running:true, | ||
Labels:map[string]string{ | ||
agentcontainers.DevcontainerLocalFolderLabel:workspaceFolder2, | ||
agentcontainers.DevcontainerConfigFileLabel:configPath2, | ||
}, | ||
} | ||
tests:= []struct { | ||
namestring | ||
devcontainerIDstring | ||
setupDevcontainers []codersdk.WorkspaceAgentDevcontainer | ||
lister*fakeContainerCLI | ||
devcontainerCLI*fakeDevcontainerCLI | ||
wantStatus []int | ||
wantBody []string | ||
}{ | ||
{ | ||
name:"Missingdevcontainer ID", | ||
devcontainerID:"", | ||
lister:&fakeContainerCLI{}, | ||
devcontainerCLI:&fakeDevcontainerCLI{}, | ||
wantStatus: []int{http.StatusBadRequest}, | ||
wantBody: []string{"Missingdevcontainer ID"}, | ||
}, | ||
{ | ||
name:"Devcontainer not found", | ||
devcontainerID:uuid.NewString(), | ||
lister:&fakeContainerCLI{ | ||
arch:"<none>",// Unsupported architecture, don't inject subagent. | ||
}, | ||
devcontainerCLI:&fakeDevcontainerCLI{}, | ||
wantStatus: []int{http.StatusNotFound}, | ||
wantBody: []string{"Devcontainer not found"}, | ||
}, | ||
{ | ||
name:"Devcontainer CLI error", | ||
devcontainerID:devcontainerID1.String(), | ||
setupDevcontainers: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
ID:devcontainerID1, | ||
Name:"test-devcontainer-1", | ||
WorkspaceFolder:workspaceFolder1, | ||
ConfigPath:configPath1, | ||
Status:codersdk.WorkspaceAgentDevcontainerStatusRunning, | ||
Container:&devContainer1, | ||
}, | ||
}, | ||
lister:&fakeContainerCLI{ | ||
containers: codersdk.WorkspaceAgentListContainersResponse{ | ||
Containers: []codersdk.WorkspaceAgentContainer{devContainer1}, | ||
}, | ||
arch:"<none>",// Unsupported architecture, don't inject subagent. | ||
}, | ||
@@ -575,11 +574,21 @@ func TestAPI(t *testing.T) { | ||
wantBody: []string{"Devcontainer recreation initiated","Devcontainer recreation already in progress"}, | ||
}, | ||
{ | ||
name:"OK", | ||
devcontainerID:devcontainerID2.String(), | ||
setupDevcontainers: []codersdk.WorkspaceAgentDevcontainer{ | ||
{ | ||
ID:devcontainerID2, | ||
Name:"test-devcontainer-2", | ||
WorkspaceFolder:workspaceFolder2, | ||
ConfigPath:configPath2, | ||
Status:codersdk.WorkspaceAgentDevcontainerStatusRunning, | ||
Container:&devContainer2, | ||
}, | ||
}, | ||
lister:&fakeContainerCLI{ | ||
containers: codersdk.WorkspaceAgentListContainersResponse{ | ||
Containers: []codersdk.WorkspaceAgentContainer{devContainer2}, | ||
}, | ||
arch:"<none>",// Unsupported architecture, don't inject subagent. | ||
}, | ||
@@ -608,13 +617,16 @@ func TestAPI(t *testing.T) { | ||
// Setup router with the handler under test. | ||
r:=chi.NewRouter() | ||
api:=agentcontainers.NewAPI( | ||
logger, | ||
agentcontainers.WithClock(mClock), | ||
agentcontainers.WithContainerCLI(tt.lister), | ||
agentcontainers.WithDevcontainerCLI(tt.devcontainerCLI), | ||
agentcontainers.WithWatcher(watcher.NewNoop()), | ||
agentcontainers.WithDevcontainers(tt.setupDevcontainers,nil), | ||
) | ||
api.Init() | ||
deferapi.Close() | ||
r.Mount("/",api.Routes()) | ||
@@ -626,7 +638,7 @@ func TestAPI(t *testing.T) { | ||
fori:=rangett.wantStatus { | ||
// Simulate HTTP request to the recreate endpoint. | ||
req:=httptest.NewRequest(http.MethodPost,"/devcontainers/"+tt.devcontainerID+"/recreate",nil). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This is a much more sensible URL 😍 | ||
WithContext(ctx) | ||
rec:=httptest.NewRecorder() | ||
r.ServeHTTP(rec,req) | ||
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.