From 7c4c534b3619a116e0637932039cf1f59c04d7ce Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 2 Mar 2021 08:38:15 +0100 Subject: [PATCH] introduce --renew-anon-volumes Signed-off-by: Nicolas De Loof --- api/compose/api.go | 2 ++ cli/cmd/compose/up.go | 3 +++ local/compose/convergence.go | 17 +++++++++++------ local/compose/create.go | 2 +- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/api/compose/api.go b/api/compose/api.go index cd9dc4d86..aab686da1 100644 --- a/api/compose/api.go +++ b/api/compose/api.go @@ -71,6 +71,8 @@ type CreateOptions struct { RemoveOrphans bool // Recreate define the strategy to apply on existing containers Recreate string + // Inherit reuse anonymous volumes from previous container + Inherit bool } // StartOptions group options of the Start API diff --git a/cli/cmd/compose/up.go b/cli/cmd/compose/up.go index 73d022f6d..763be0952 100644 --- a/cli/cmd/compose/up.go +++ b/cli/cmd/compose/up.go @@ -65,6 +65,7 @@ type upOptions struct { timeChanged bool timeout int noDeps bool + noInherit bool } func (o upOptions) recreateStrategy() string { @@ -129,6 +130,7 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command { flags.StringVar(&opts.exitCodeFrom, "exit-code-from", "", "Return the exit code of the selected service container. Implies --abort-on-container-exit") flags.IntVarP(&opts.timeout, "timeout", "t", 10, "Use this timeout in seconds for container shutdown when attached or when containers are already running.") flags.BoolVar(&opts.noDeps, "no-deps", false, "Don't start linked services.") + flags.BoolVarP(&opts.noInherit, "renew-anon-volumes", "V", false, "Recreate anonymous volumes instead of retrieving data from the previous containers.") } return upCmd @@ -192,6 +194,7 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro err := c.ComposeService().Create(ctx, project, compose.CreateOptions{ RemoveOrphans: opts.removeOrphans, Recreate: opts.recreateStrategy(), + Inherit: !opts.noInherit, }) if err != nil { return "", err diff --git a/local/compose/convergence.go b/local/compose/convergence.go index 97fa40fc1..aaf8bc0a2 100644 --- a/local/compose/convergence.go +++ b/local/compose/convergence.go @@ -85,7 +85,7 @@ func (s *composeService) ensureScale(ctx context.Context, project *types.Project return eg, actual, nil } -func (s *composeService) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig, recreate string) error { +func (s *composeService) ensureService(ctx context.Context, project *types.Project, service types.ServiceConfig, recreate string, inherit bool) error { eg, actual, err := s.ensureScale(ctx, project, service) if err != nil { return err @@ -106,7 +106,7 @@ func (s *composeService) ensureService(ctx context.Context, project *types.Proje diverged := container.Labels[configHashLabel] != expected if diverged || recreate == compose.RecreateForce || service.Extensions[extLifecycle] == forceRecreate { eg.Go(func() error { - return s.recreateContainer(ctx, project, service, container) + return s.recreateContainer(ctx, project, service, container, inherit) }) continue } @@ -209,7 +209,7 @@ func (s *composeService) createContainer(ctx context.Context, project *types.Pro return nil } -func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, container moby.Container) error { +func (s *composeService) recreateContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, container moby.Container, inherit bool) error { w := progress.ContextWriter(ctx) w.Event(progress.NewEvent(getContainerProgressName(container), progress.Working, "Recreate")) err := s.apiClient.ContainerStop(ctx, container.ID, nil) @@ -226,7 +226,12 @@ func (s *composeService) recreateContainer(ctx context.Context, project *types.P if err != nil { return err } - err = s.createMobyContainer(ctx, project, service, name, number, &container, false) + + var inherited *moby.Container + if inherit { + inherited = &container + } + err = s.createMobyContainer(ctx, project, service, name, number, inherited, false) if err != nil { return err } @@ -263,13 +268,13 @@ func (s *composeService) restartContainer(ctx context.Context, container moby.Co return nil } -func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, container *moby.Container, +func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, name string, number int, inherit *moby.Container, autoRemove bool) error { cState, err := GetContextContainerState(ctx) if err != nil { return err } - containerConfig, hostConfig, networkingConfig, err := s.getCreateOptions(ctx, project, service, number, container, autoRemove) + containerConfig, hostConfig, networkingConfig, err := s.getCreateOptions(ctx, project, service, number, inherit, autoRemove) if err != nil { return err } diff --git a/local/compose/create.go b/local/compose/create.go index 13cd307a0..61b774932 100644 --- a/local/compose/create.go +++ b/local/compose/create.go @@ -97,7 +97,7 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt prepareNetworkMode(project) return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error { - return s.ensureService(c, project, service, opts.Recreate) + return s.ensureService(c, project, service, opts.Recreate, opts.Inherit) }) }