Add --scale
to compose create
, refactor scale option
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
This commit is contained in:
parent
d5d9f67547
commit
9d53ed8f63
@ -19,6 +19,8 @@ package compose
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/compose-spec/compose-go/types"
|
"github.com/compose-spec/compose-go/types"
|
||||||
@ -41,6 +43,7 @@ type createOptions struct {
|
|||||||
timeChanged bool
|
timeChanged bool
|
||||||
timeout int
|
timeout int
|
||||||
quietPull bool
|
quietPull bool
|
||||||
|
scale []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
|
func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
|
||||||
@ -59,7 +62,9 @@ func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
|
|||||||
return nil
|
return nil
|
||||||
}),
|
}),
|
||||||
RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
|
RunE: p.WithProject(func(ctx context.Context, project *types.Project) error {
|
||||||
opts.Apply(project)
|
if err := opts.Apply(project); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return backend.Create(ctx, project, api.CreateOptions{
|
return backend.Create(ctx, project, api.CreateOptions{
|
||||||
RemoveOrphans: opts.removeOrphans,
|
RemoveOrphans: opts.removeOrphans,
|
||||||
IgnoreOrphans: opts.ignoreOrphans,
|
IgnoreOrphans: opts.ignoreOrphans,
|
||||||
@ -79,6 +84,7 @@ func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
|
|||||||
flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
|
flags.BoolVar(&opts.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
|
||||||
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
|
flags.BoolVar(&opts.noRecreate, "no-recreate", false, "If containers already exist, don't recreate them. Incompatible with --force-recreate.")
|
||||||
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
|
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
|
||||||
|
flags.StringArrayVar(&opts.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +116,7 @@ func (opts createOptions) GetTimeout() *time.Duration {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (opts createOptions) Apply(project *types.Project) {
|
func (opts createOptions) Apply(project *types.Project) error {
|
||||||
if opts.pullChanged {
|
if opts.pullChanged {
|
||||||
for i, service := range project.Services {
|
for i, service := range project.Services {
|
||||||
service.PullPolicy = opts.Pull
|
service.PullPolicy = opts.Pull
|
||||||
@ -135,4 +141,20 @@ func (opts createOptions) Apply(project *types.Project) {
|
|||||||
project.Services[i] = service
|
project.Services[i] = service
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, scale := range opts.scale {
|
||||||
|
split := strings.Split(scale, "=")
|
||||||
|
if len(split) != 2 {
|
||||||
|
return fmt.Errorf("invalid --scale option %q. Should be SERVICE=NUM", scale)
|
||||||
|
}
|
||||||
|
name := split[0]
|
||||||
|
replicas, err := strconv.Atoi(split[1])
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = setServiceScale(project, name, uint64(replicas))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -197,7 +197,10 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
createOpts.Apply(project)
|
err = createOpts.Apply(project)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
err = progress.Run(ctx, func(ctx context.Context) error {
|
err = progress.Run(ctx, func(ctx context.Context) error {
|
||||||
return startDependencies(ctx, backend, *project, opts.Service, opts.ignoreOrphans)
|
return startDependencies(ctx, backend, *project, opts.Service, opts.ignoreOrphans)
|
||||||
|
@ -19,8 +19,6 @@ package compose
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/docker/compose/v2/cmd/formatter"
|
"github.com/docker/compose/v2/cmd/formatter"
|
||||||
|
|
||||||
@ -43,7 +41,6 @@ type upOptions struct {
|
|||||||
noDeps bool
|
noDeps bool
|
||||||
cascadeStop bool
|
cascadeStop bool
|
||||||
exitCodeFrom string
|
exitCodeFrom string
|
||||||
scale []string
|
|
||||||
noColor bool
|
noColor bool
|
||||||
noPrefix bool
|
noPrefix bool
|
||||||
attachDependencies bool
|
attachDependencies bool
|
||||||
@ -68,22 +65,6 @@ func (opts upOptions) apply(project *types.Project, services []string) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, scale := range opts.scale {
|
|
||||||
split := strings.Split(scale, "=")
|
|
||||||
if len(split) != 2 {
|
|
||||||
return fmt.Errorf("invalid --scale option %q. Should be SERVICE=NUM", scale)
|
|
||||||
}
|
|
||||||
name := split[0]
|
|
||||||
replicas, err := strconv.Atoi(split[1])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = setServiceScale(project, name, uint64(replicas))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +94,7 @@ func upCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *cob
|
|||||||
flags.BoolVar(&create.noBuild, "no-build", false, "Don't build an image, even if it's missing.")
|
flags.BoolVar(&create.noBuild, "no-build", false, "Don't build an image, even if it's missing.")
|
||||||
flags.StringVar(&create.Pull, "pull", "missing", `Pull image before running ("always"|"missing"|"never")`)
|
flags.StringVar(&create.Pull, "pull", "missing", `Pull image before running ("always"|"missing"|"never")`)
|
||||||
flags.BoolVar(&create.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
|
flags.BoolVar(&create.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
|
||||||
flags.StringArrayVar(&up.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
|
flags.StringArrayVar(&create.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
|
||||||
flags.BoolVar(&up.noColor, "no-color", false, "Produce monochrome output.")
|
flags.BoolVar(&up.noColor, "no-color", false, "Produce monochrome output.")
|
||||||
flags.BoolVar(&up.noPrefix, "no-log-prefix", false, "Don't print prefix in logs.")
|
flags.BoolVar(&up.noPrefix, "no-log-prefix", false, "Don't print prefix in logs.")
|
||||||
flags.BoolVar(&create.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
|
flags.BoolVar(&create.forceRecreate, "force-recreate", false, "Recreate containers even if their configuration and image haven't changed.")
|
||||||
@ -165,9 +146,12 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
|
|||||||
return fmt.Errorf("no service selected")
|
return fmt.Errorf("no service selected")
|
||||||
}
|
}
|
||||||
|
|
||||||
createOptions.Apply(project)
|
err := createOptions.Apply(project)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
err := upOptions.apply(project, services)
|
err = upOptions.apply(project, services)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -34,8 +34,8 @@ func TestApplyScaleOpt(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
opt := upOptions{scale: []string{"foo=2"}}
|
opt := createOptions{scale: []string{"foo=2"}}
|
||||||
err := opt.apply(&p, nil)
|
err := opt.Apply(&p)
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
foo, err := p.GetService("foo")
|
foo, err := p.GetService("foo")
|
||||||
assert.NilError(t, err)
|
assert.NilError(t, err)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user