Repository navigation
Clean up isolation resources when workload setup fails #7049
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pcbeingused333
wants to merge
6
commits into
stacklok:main
Choose a base branch
from
pcbeingused333:c0773-cleanup-failed-setup
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8761a68
fix(docker): clean up isolation resources when workload setup fails
pcbeingused333 2200b49
fix(docker): only clean up isolation resources before the MCP contain…
pcbeingused333 df1e870
Keep isolation resources on restart failure
pcbeingused333 9a749be
Test isolation cleanup against a fake daemon
pcbeingused333 316a7f5
Merge isolation cleanup tests into one table
pcbeingused333 c8ac304
Merge upstream main into c0773-cleanup-failed-setup
pcbeingused333 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,121 @@ | ||
| // SPDX-FileCopyrightText: Copyright 2026 Stacklok, Inc. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package docker | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // fakeDaemon is a minimal Docker daemon that serves container and network | ||
| // lookups for the given names and records every removal request. | ||
| type fakeDaemon struct { | ||
| mu sync.Mutex | ||
| containers map[string]bool // container names that exist | ||
| networks map[string]bool // network names that exist | ||
| failRemove map[string]bool // container IDs whose removal returns 500 | ||
| removed []string // "container/<id>" or "network/<id>" | ||
| } | ||
|
|
||
| func (d *fakeDaemon) handler(t *testing.T) http.HandlerFunc { | ||
| t.Helper() | ||
| // nameFilter extracts the single "name" filter value of a list request. | ||
| nameFilter := func(r *http.Request) string { | ||
| var filters map[string]map[string]bool | ||
| require.NoError(t, json.Unmarshal([]byte(r.URL.Query().Get("filters")), &filters)) | ||
| for name := range filters["name"] { | ||
| return name | ||
| } | ||
| return "" | ||
| } | ||
| return func(w http.ResponseWriter, r *http.Request) { | ||
| d.mu.Lock() | ||
| defer d.mu.Unlock() | ||
| path := strings.TrimPrefix(r.URL.Path, "/v1.44") | ||
| switch { | ||
| case r.Method == http.MethodGet && path == "/containers/json": | ||
| name := nameFilter(r) | ||
| list := []map[string]any{} | ||
| if d.containers[name] { | ||
| list = append(list, map[string]any{"Id": "id-" + name, "Names": []string{"/" + name}}) | ||
| } | ||
| assert.NoError(t, json.NewEncoder(w).Encode(list)) | ||
| case r.Method == http.MethodDelete && strings.HasPrefix(path, "/containers/"): | ||
| id := strings.TrimPrefix(path, "/containers/") | ||
| d.removed = append(d.removed, "container/"+id) | ||
| if d.failRemove[id] { | ||
| http.Error(w, `{"message":"cannot remove"}`, http.StatusInternalServerError) | ||
| return | ||
| } | ||
| w.WriteHeader(http.StatusNoContent) | ||
| case r.Method == http.MethodGet && path == "/networks": | ||
| name := nameFilter(r) | ||
| list := []map[string]any{} | ||
| if d.networks[name] { | ||
| list = append(list, map[string]any{"Name": name, "Id": "net-" + name}) | ||
| } | ||
| assert.NoError(t, json.NewEncoder(w).Encode(list)) | ||
| case r.Method == http.MethodDelete && strings.HasPrefix(path, "/networks/"): | ||
| d.removed = append(d.removed, "network/"+strings.TrimPrefix(path, "/networks/")) | ||
| w.WriteHeader(http.StatusNoContent) | ||
| default: | ||
| t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) | ||
| http.Error(w, "unexpected", http.StatusNotImplemented) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestCleanupFailedIsolation(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| const network = "toolhive-app-internal" | ||
| tests := []struct { | ||
| name string | ||
| daemon *fakeDaemon | ||
| wantRemoved []string | ||
| }{ | ||
| { | ||
| name: "removes all sidecars and the network", | ||
| daemon: &fakeDaemon{ | ||
| containers: map[string]bool{"app-egress": true, "app-ingress": true, "app-dns": true}, | ||
| networks: map[string]bool{network: true}, | ||
| }, | ||
| wantRemoved: []string{"container/id-app-egress", "container/id-app-ingress", "container/id-app-dns", "network/net-" + network}, | ||
| }, | ||
| { | ||
| name: "a sidecar that fails to remove does not stop the network deletion", | ||
| daemon: &fakeDaemon{ | ||
| containers: map[string]bool{"app-egress": true, "app-ingress": true, "app-dns": true}, | ||
| networks: map[string]bool{network: true}, | ||
| failRemove: map[string]bool{"id-app-egress": true}, | ||
| }, | ||
| wantRemoved: []string{"container/id-app-egress", "container/id-app-ingress", "container/id-app-dns", "network/net-" + network}, | ||
| }, | ||
| { | ||
| name: "absent network is not an error", | ||
| daemon: &fakeDaemon{ | ||
| containers: map[string]bool{"app-dns": true}, | ||
| networks: map[string]bool{}, | ||
| }, | ||
| wantRemoved: []string{"container/id-app-dns"}, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| api := discoveryHTTPClient(t, tt.daemon.handler(t)) | ||
| client := &Client{client: api, api: api} | ||
|
|
||
| client.cleanupFailedIsolation(t.Context(), "app", network) | ||
|
|
||
| assert.Equal(t, tt.wantRemoved, tt.daemon.removed) | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[MEDIUM] Failure cleanup removes the reused network and sidecars of a restarted workload
mcpCreatedonly says whether this call created the MCP container.thv restartstops the workload (StopWorkloadleaves the MCP container and network in place) and then reachesDeployWorkload, wherecreateNetwork,createDnsContainerandcreateContainerreuse what already exists. If a step beforecreateMcpContainerfails (gateway discovery, egress setup, port bindings), this defer removes the network and the dns/egress/ingress containers of a workload whose MCP container is still there, so the comment above ("thv rmcannot find them") is not true on that path. Before this change the network survived and a retry could succeed. I could not confirm from the code whether Docker then refuses to start the old container against the recreated network, butcreateAndStartContaineralready handles a start that fails because "its network vanished", so the failure mode is known.Recommendation: only clean up what this call created. For example, check
findExistingContainer(ctx, name)beforecreateNetworkand skip the defer (or at least the network removal) when the MCP container already exists. If removing them is the intended trade-off, say so in a comment here. Either way, add a test with a pre-existing MCP container and a pre-MCP failure.Raised by: correctness, scope discipline, tests. Verified: confirmed.