From 32c3d0a3ff4f6f5dd2dcfba17d66ef96601202b3 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Thu, 31 Aug 2023 09:43:39 +0200 Subject: [PATCH] Enable service explicitly requested to be restarted Signed-off-by: Nicolas De Loof --- cmd/compose/restart.go | 16 ++++++++-------- pkg/api/api.go | 2 ++ pkg/compose/restart.go | 7 +++++++ pkg/e2e/fixtures/restart-test/compose.yaml | 7 +++++++ pkg/e2e/restart_test.go | 16 ++++++++++++++++ 5 files changed, 40 insertions(+), 8 deletions(-) diff --git a/cmd/compose/restart.go b/cmd/compose/restart.go index 90848cd44..5c87616fc 100644 --- a/cmd/compose/restart.go +++ b/cmd/compose/restart.go @@ -20,7 +20,6 @@ import ( "context" "time" - "github.com/compose-spec/compose-go/types" "github.com/spf13/cobra" "github.com/docker/compose/v2/pkg/api" @@ -61,22 +60,23 @@ func runRestart(ctx context.Context, backend api.Service, opts restartOptions, s return err } + if project != nil && len(services) > 0 { + err := project.EnableServices(services...) + if err != nil { + return err + } + } + var timeout *time.Duration if opts.timeChanged { timeoutValue := time.Duration(opts.timeout) * time.Second timeout = &timeoutValue } - if opts.noDeps { - err := project.ForServices(services, types.IgnoreDependencies) - if err != nil { - return err - } - } - return backend.Restart(ctx, name, api.RestartOptions{ Timeout: timeout, Services: services, Project: project, + NoDeps: opts.noDeps, }) } diff --git a/pkg/api/api.go b/pkg/api/api.go index b1442eb5e..28595941b 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -213,6 +213,8 @@ type RestartOptions struct { Timeout *time.Duration // Services passed in the command line to be restarted Services []string + // NoDeps ignores services dependencies + NoDeps bool } // StopOptions group options of the Stop API diff --git a/pkg/compose/restart.go b/pkg/compose/restart.go index 72cf0a816..db27b1fba 100644 --- a/pkg/compose/restart.go +++ b/pkg/compose/restart.go @@ -48,6 +48,13 @@ func (s *composeService) restart(ctx context.Context, projectName string, option } } + if options.NoDeps { + err := project.ForServices(options.Services, types.IgnoreDependencies) + if err != nil { + return err + } + } + // ignore depends_on relations which are not impacted by restarting service or not required for i, service := range project.Services { for name, r := range service.DependsOn { diff --git a/pkg/e2e/fixtures/restart-test/compose.yaml b/pkg/e2e/fixtures/restart-test/compose.yaml index 9055af4e7..92c28d3a7 100644 --- a/pkg/e2e/fixtures/restart-test/compose.yaml +++ b/pkg/e2e/fixtures/restart-test/compose.yaml @@ -3,3 +3,10 @@ services: image: alpine init: true command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi" + + test: + profiles: + - test + image: alpine + init: true + command: ash -c "if [[ -f /tmp/restart.lock ]] ; then sleep infinity; else touch /tmp/restart.lock; fi" diff --git a/pkg/e2e/restart_test.go b/pkg/e2e/restart_test.go index 6d00159b8..91ade0b30 100644 --- a/pkg/e2e/restart_test.go +++ b/pkg/e2e/restart_test.go @@ -84,3 +84,19 @@ func TestRestartWithDependencies(t *testing.T) { assert.Assert(t, strings.Contains(res.Combined(), fmt.Sprintf("Container e2e-restart-deps-%s-1 Started", depWithRestart)), res.Combined()) assert.Assert(t, !strings.Contains(res.Combined(), depNoRestart), res.Combined()) } + +func TestRestartWithProfiles(t *testing.T) { + c := NewParallelCLI(t, WithEnv( + "COMPOSE_PROJECT_NAME=e2e-restart-profiles", + )) + + t.Cleanup(func() { + c.RunDockerComposeCmd(t, "down", "--remove-orphans") + }) + + c.RunDockerComposeCmd(t, "-f", "./fixtures/restart-test/compose.yaml", "--profile", "test", "up", "-d") + + res := c.RunDockerComposeCmd(t, "restart", "test") + fmt.Println(res.Combined()) + assert.Assert(t, strings.Contains(res.Combined(), "Container e2e-restart-profiles-test-1 Started"), res.Combined()) +}