fix: new Compose deployment with volumes and --recreate flag (fixes #176)

This commit is contained in:
Pasha Sviderski
2025-11-20 15:19:44 +10:00
parent ce4a8cce72
commit 57205e1149
7 changed files with 89 additions and 23 deletions
+3 -6
View File
@@ -49,10 +49,6 @@ func NewDeploymentWithStrategy(ctx context.Context, cli Client, project *types.P
ClusterDomain: domain,
}
if strategy == nil {
strategy = &deploy.RollingStrategy{State: state}
}
return &Deployment{
Client: cli,
Project: project,
@@ -88,6 +84,7 @@ func (d *Deployment) Plan(ctx context.Context) (deploy.SequenceOperation, error)
}
// Check external volumes and plan the creation of missing volumes before deploying services.
// Updates the cluster state (d.state) with the scheduled volumes.
volumeOps, err := d.planVolumes(serviceSpecs)
if err != nil {
return plan, err
@@ -98,8 +95,8 @@ func (d *Deployment) Plan(ctx context.Context) (deploy.SequenceOperation, error)
for _, spec := range serviceSpecs {
// TODO: properly handle depends_on conditions in the service deployment plan as the first operation.
// Pass the update cluster state with scheduled volumes to the deployment.
deployment := deploy.NewDeployment(d.Client, spec, d.Strategy)
// Pass the updated cluster state with the scheduled volumes to the deployment.
deployment := deploy.NewDeploymentWithClusterState(d.Client, spec, d.Strategy, d.state)
servicePlan, err := deployment.Plan(ctx)
if err != nil {
return plan, fmt.Errorf("create deployment plan for service '%s': %w", spec.Name, err)
+23
View File
@@ -3,12 +3,15 @@ package compose
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
composecli "github.com/compose-spec/compose-go/v2/cli"
"github.com/compose-spec/compose-go/v2/types"
)
// LoadProject loads a Compose project from the default locations or the given paths.
func LoadProject(ctx context.Context, paths []string, opts ...composecli.ProjectOptionsFn) (*types.Project, error) {
defaultOpts := []composecli.ProjectOptionsFn{
// First apply os.Environment, always wins.
@@ -61,6 +64,26 @@ func LoadProject(ctx context.Context, paths []string, opts ...composecli.Project
return project, nil
}
// LoadProjectFromContent loads a Compose project from the given YAML content.
func LoadProjectFromContent(
ctx context.Context, content string, opts ...composecli.ProjectOptionsFn,
) (*types.Project, error) {
// Create a temporary directory for the compose file.
tmpDir, err := os.MkdirTemp("", "uncloud-compose-*")
if err != nil {
return nil, fmt.Errorf("create temporary directory: %w", err)
}
defer os.RemoveAll(tmpDir)
// Write the YAML content to compose.yaml in the temporary directory.
composePath := filepath.Join(tmpDir, "compose.yaml")
if err := os.WriteFile(composePath, []byte(content), 0644); err != nil {
return nil, fmt.Errorf("write compose file: %w", err)
}
return LoadProject(ctx, []string{composePath}, opts...)
}
// removeProjectPrefixFromNames removes the project name prefix from volume names.
func removeProjectPrefixFromNames(project *types.Project) {
prefix := project.Name + "_"
+1
View File
@@ -22,6 +22,7 @@ import (
// loadProjectFromContent loads a compose project from YAML content.
// Keep the implementation in sync with LoadProject.
// TODO(lhf): remove and replace with compose.LoadProjectFromContent
func loadProjectFromContent(t *testing.T, content string) (*types.Project, error) {
t.Helper()
ctx := context.Background()