Merge pull request #1375 from docker/always-recreate-deps

introduce --always-recreate-deps
This commit is contained in:
Nicolas De loof 2021-03-02 09:27:47 +01:00 committed by GitHub
commit e6b8048e56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 5 deletions

View File

@ -67,10 +67,14 @@ type Service interface {
// CreateOptions group options of the Create API // CreateOptions group options of the Create API
type CreateOptions struct { type CreateOptions struct {
// Services defines the services user interacts with
Services []string
// Remove legacy containers for services that are not defined in the project // Remove legacy containers for services that are not defined in the project
RemoveOrphans bool RemoveOrphans bool
// Recreate define the strategy to apply on existing containers // Recreate define the strategy to apply on existing containers
Recreate string Recreate string
// RecreateDependencies define the strategy to apply on dependencies services
RecreateDependencies string
// Inherit reuse anonymous volumes from previous container // Inherit reuse anonymous volumes from previous container
Inherit bool Inherit bool
} }

View File

@ -56,7 +56,9 @@ type upOptions struct {
removeOrphans bool removeOrphans bool
forceRecreate bool forceRecreate bool
noRecreate bool noRecreate bool
recreateDeps bool
noStart bool noStart bool
noDeps bool
cascadeStop bool cascadeStop bool
exitCodeFrom string exitCodeFrom string
scale []string scale []string
@ -64,7 +66,6 @@ type upOptions struct {
noPrefix bool noPrefix bool
timeChanged bool timeChanged bool
timeout int timeout int
noDeps bool
noInherit bool noInherit bool
} }
@ -78,6 +79,16 @@ func (o upOptions) recreateStrategy() string {
return compose.RecreateDiverged return compose.RecreateDiverged
} }
func (o upOptions) dependenciesRecreateStrategy() string {
if o.noRecreate {
return compose.RecreateNever
}
if o.recreateDeps {
return compose.RecreateForce
}
return compose.RecreateDiverged
}
func upCommand(p *projectOptions, contextType string) *cobra.Command { func upCommand(p *projectOptions, contextType string) *cobra.Command {
opts := upOptions{ opts := upOptions{
composeOptions: &composeOptions{ composeOptions: &composeOptions{
@ -103,6 +114,9 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
if opts.forceRecreate && opts.noRecreate { if opts.forceRecreate && opts.noRecreate {
return fmt.Errorf("--force-recreate and --no-recreate are incompatible") return fmt.Errorf("--force-recreate and --no-recreate are incompatible")
} }
if opts.recreateDeps && opts.noRecreate {
return fmt.Errorf("--always-recreate-deps and --no-recreate are incompatible")
}
return runCreateStart(cmd.Context(), opts, args) return runCreateStart(cmd.Context(), opts, args)
default: default:
return runUp(cmd.Context(), opts, args) return runUp(cmd.Context(), opts, args)
@ -130,6 +144,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.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.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.BoolVar(&opts.noDeps, "no-deps", false, "Don't start linked services.")
flags.BoolVar(&opts.recreateDeps, "always-recreate-deps", false, "Recreate dependent containers. Incompatible with --no-recreate.")
flags.BoolVarP(&opts.noInherit, "renew-anon-volumes", "V", false, "Recreate anonymous volumes instead of retrieving data from the previous containers.") flags.BoolVarP(&opts.noInherit, "renew-anon-volumes", "V", false, "Recreate anonymous volumes instead of retrieving data from the previous containers.")
} }
@ -192,9 +207,11 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) { _, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
err := c.ComposeService().Create(ctx, project, compose.CreateOptions{ err := c.ComposeService().Create(ctx, project, compose.CreateOptions{
RemoveOrphans: opts.removeOrphans, Services: services,
Recreate: opts.recreateStrategy(), RemoveOrphans: opts.removeOrphans,
Inherit: !opts.noInherit, Recreate: opts.recreateStrategy(),
RecreateDependencies: opts.dependenciesRecreateStrategy(),
Inherit: !opts.noInherit,
}) })
if err != nil { if err != nil {
return "", err return "", err

View File

@ -42,6 +42,10 @@ import (
) )
func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error { func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
if len(opts.Services) == 0 {
opts.Services = project.ServiceNames()
}
err := s.ensureImagesExists(ctx, project) err := s.ensureImagesExists(ctx, project)
if err != nil { if err != nil {
return err return err
@ -97,7 +101,10 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt
prepareNetworkMode(project) prepareNetworkMode(project)
return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error { return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
return s.ensureService(c, project, service, opts.Recreate, opts.Inherit) if contains(opts.Services, service.Name) {
return s.ensureService(c, project, service, opts.Recreate, opts.Inherit)
}
return s.ensureService(c, project, service, opts.RecreateDependencies, opts.Inherit)
}) })
} }