Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: redeploy swarm stack when saving edited stack source (#3559)
Kyle Mendell0edfdfc0d526
diff --git a/backend/internal/swarm/handler.go b/backend/internal/swarm/handler.goindex a34176bdf..64c875272 100644--- a/backend/internal/swarm/handler.go+++ b/backend/internal/swarm/handler.go@@ -1216,7 +1216,8 @@ func (h *SwarmHandler) GetStackSource(ctx context.Context, input *GetSwarmStackSreturn &GetSwarmStackSourceOutput{Body: base.ApiResponse[swarmtypes.StackSource]{Success: true, Data: *source}}, nil}-// UpdateStackSource persists the saved compose and env source for a swarm stack.+// UpdateStackSource persists the saved compose and env source for a swarm+// stack and redeploys the stack so the edit takes effect on running services.//// It requires admin privileges because stack source content can include// sensitive configuration. The stack name comes from the route, and the bodydiff --git a/backend/internal/swarm/service_test.go b/backend/internal/swarm/service_test.goindex e3ddb4c4e..b36dc03bb 100644--- a/backend/internal/swarm/service_test.go+++ b/backend/internal/swarm/service_test.go@@ -3,6 +3,7 @@ package swarmimport ("context""encoding/json"+ "errors""net/http""net/http/httptest""path/filepath"@@ -147,6 +148,18 @@ func TestSwarmService_FetchSwarmNodeIdentityViaEdgeInternal_UsesEnvironmentAccesrequire.True(t, identity.SwarmActive)}+func stubStackSourceUpdateDeployInternal(t *testing.T) *int {+ t.Helper()+ original := deployStackAfterSourceUpdateInternal+ t.Cleanup(func() { deployStackAfterSourceUpdateInternal = original })+ calls := 0+ deployStackAfterSourceUpdateInternal = func(_ *SwarmService, _ context.Context, _ string, req swarmtypes.StackDeployRequest) (*swarmtypes.StackDeployResponse, error) {+ calls+++ return &swarmtypes.StackDeployResponse{Name: req.Name}, nil+ }+ return &calls+}+func TestSwarmService_UpdateAndGetStackSource_UsesStoredFilesWithoutSwarmManager(t *testing.T) {ctx := context.Background()db := setupSwarmServiceTestDBInternal(t)@@ -157,6 +170,7 @@ func TestSwarmService_UpdateAndGetStackSource_UsesStoredFilesWithoutSwarmManagerrequire.NoError(t, err)svc := NewSwarmService(nil, settingsSvc, nil, nil, nil)+ deployCalls := stubStackSourceUpdateDeployInternal(t)updated, err := svc.UpdateStackSource(ctx, "0", "demo-stack", swarmtypes.StackSourceUpdateRequest{ComposeContent: "services:\n web:\n image: nginx:alpine\n",@@ -164,6 +178,8 @@ func TestSwarmService_UpdateAndGetStackSource_UsesStoredFilesWithoutSwarmManager})require.NoError(t, err)require.Equal(t, "demo-stack", updated.Name)+ // Saving stack source must trigger an actual stack deploy (#3463).+ require.Equal(t, 1, *deployCalls)composePath := filepath.Join(rootDir, "0", "demo-stack", "compose.yaml")envPath := filepath.Join(rootDir, "0", "demo-stack", ".env")@@ -215,6 +231,7 @@ func TestSwarmService_UpdateAndGetStackSource_RoundTripsOverride(t *testing.T) {require.NoError(t, err)svc := NewSwarmService(nil, settingsSvc, nil, nil, nil)+ stubStackSourceUpdateDeployInternal(t)overridePath := filepath.Join(rootDir, "0", "demo-stack", "compose.override.yaml")@@ -244,6 +261,50 @@ func TestSwarmService_UpdateAndGetStackSource_RoundTripsOverride(t *testing.T) {require.Empty(t, source.OverrideContent)}+func TestSwarmService_UpdateStackSource_PrunesAndRestoresOnDeployFailure(t *testing.T) {+ ctx := context.Background()+ db := setupSwarmServiceTestDBInternal(t)+ rootDir := t.TempDir()+ t.Setenv("SWARM_STACK_SOURCES_DIRECTORY", rootDir)++ settingsSvc, err := newSettingsServiceForSwarmTestInternal(t, ctx, db)+ require.NoError(t, err)++ svc := NewSwarmService(nil, settingsSvc, nil, nil, nil)++ original := deployStackAfterSourceUpdateInternal+ t.Cleanup(func() { deployStackAfterSourceUpdateInternal = original })+ var lastReq swarmtypes.StackDeployRequest+ var deployErr error+ deployStackAfterSourceUpdateInternal = func(_ *SwarmService, _ context.Context, _ string, req swarmtypes.StackDeployRequest) (*swarmtypes.StackDeployResponse, error) {+ lastReq = req+ if deployErr != nil {+ return nil, deployErr+ }+ return &swarmtypes.StackDeployResponse{Name: req.Name}, nil+ }++ firstCompose := "services:\n web:\n image: nginx:alpine\n"+ _, err = svc.UpdateStackSource(ctx, "0", "demo-stack", swarmtypes.StackSourceUpdateRequest{+ ComposeContent: firstCompose,+ })+ require.NoError(t, err)+ // The saved source is the full stack spec: services removed from it must+ // be pruned from the swarm on redeploy.+ require.True(t, lastReq.Prune)++ deployErr = errors.New("deploy failed")+ _, err = svc.UpdateStackSource(ctx, "0", "demo-stack", swarmtypes.StackSourceUpdateRequest{+ ComposeContent: "services:\n web:\n image: nginx:broken\n",+ })+ require.ErrorContains(t, err, "deploy failed")++ // A failed deploy must not commit the edited source.+ source, err := svc.GetStackSource(ctx, "0", "demo-stack")+ require.NoError(t, err)+ require.Equal(t, firstCompose, source.ComposeContent)+}+func TestSwarmService_ScaleService_HandlesServiceModesInternal(t *testing.T) {ctx := context.Background()replicas := uint64(5)diff --git a/backend/internal/swarm/swarm.go b/backend/internal/swarm/swarm.goindex 3fc2242a2..c5573a72b 100644--- a/backend/internal/swarm/swarm.go+++ b/backend/internal/swarm/swarm.go@@ -1830,6 +1830,8 @@ func (s *SwarmService) GetStackSource(ctx context.Context, environmentID, stackN}, nil}+var deployStackAfterSourceUpdateInternal = (*SwarmService).DeployStack+func (s *SwarmService) UpdateStackSource(ctx context.Context, environmentID, stackName string, req swarmtypes.StackSourceUpdateRequest) (*swarmtypes.StackSource, error) {stackName = strings.TrimSpace(stackName)if stackName == "" {@@ -1839,10 +1841,41 @@ func (s *SwarmService) UpdateStackSource(ctx context.Context, environmentID, stareturn nil, errors.New("stack compose source is required")}+ previous, err := s.GetStackSource(ctx, environmentID, stackName)+ if err != nil && !errors.Is(err, cerrdefs.ErrNotFound) {+ return nil, err+ }+if err := s.upsertStackSourceInternal(ctx, environmentID, stackName, req.ComposeContent, req.OverrideContent, req.EnvContent, req.Files); err != nil {return nil, err}+ // Saving an edited stack must behave like `docker stack deploy` (#3463):+ // push the updated spec to the running services so the edit takes effect.+ // The saved source is the full stack spec, so services removed from it+ // must also be removed from the swarm.+ if _, err := deployStackAfterSourceUpdateInternal(s, ctx, environmentID, swarmtypes.StackDeployRequest{+ Name: stackName,+ ComposeContent: req.ComposeContent,+ OverrideContent: req.OverrideContent,+ EnvContent: req.EnvContent,+ Files: req.Files,+ Prune: true,+ }); err != nil {+ // Roll back the persisted source so reads never return an edit that+ // was never successfully deployed.+ var restoreErr error+ if previous != nil {+ restoreErr = s.upsertStackSourceInternal(ctx, environmentID, stackName, previous.ComposeContent, previous.OverrideContent, previous.EnvContent, previous.Files)+ } else {+ restoreErr = s.deleteStackSourceInternal(ctx, environmentID, stackName)+ }+ if restoreErr != nil {+ slog.WarnContext(ctx, "failed to restore swarm stack source after deploy failure", "environmentID", normalizeSwarmEnvironmentIDInternal(environmentID), "stackName", stackName, "error", restoreErr)+ }+ return nil, errors.WrapIf(err, "failed to redeploy swarm stack from updated source")+ }+return &swarmtypes.StackSource{Name: stackName,ComposeContent: req.ComposeContent,