Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --scale to compose create #10208

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions cmd/compose/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package compose
import (
"context"
"fmt"
"strconv"
"strings"
"time"

"github.com/compose-spec/compose-go/types"
Expand All @@ -41,6 +43,7 @@ type createOptions struct {
timeChanged bool
timeout int
quietPull bool
scale []string
}

func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
Expand All @@ -59,7 +62,9 @@ func createCommand(p *ProjectOptions, backend api.Service) *cobra.Command {
return nil
}),
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{
RemoveOrphans: opts.removeOrphans,
IgnoreOrphans: opts.ignoreOrphans,
Expand All @@ -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.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.StringArrayVar(&opts.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
return cmd
}

Expand Down Expand Up @@ -110,7 +116,7 @@ func (opts createOptions) GetTimeout() *time.Duration {
return nil
}

func (opts createOptions) Apply(project *types.Project) {
func (opts createOptions) Apply(project *types.Project) error {
if opts.pullChanged {
for i, service := range project.Services {
service.PullPolicy = opts.Pull
Expand All @@ -135,4 +141,20 @@ func (opts createOptions) Apply(project *types.Project) {
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
}
5 changes: 4 additions & 1 deletion cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
return err
}

createOpts.Apply(project)
err = createOpts.Apply(project)
if err != nil {
return err
}

err = progress.Run(ctx, func(ctx context.Context) error {
return startDependencies(ctx, backend, *project, opts.Service, opts.ignoreOrphans)
Expand Down
28 changes: 6 additions & 22 deletions cmd/compose/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package compose
import (
"context"
"fmt"
"strconv"
"strings"

"github.com/docker/compose/v2/cmd/formatter"

Expand All @@ -43,7 +41,6 @@ type upOptions struct {
noDeps bool
cascadeStop bool
exitCodeFrom string
scale []string
noColor bool
noPrefix bool
attachDependencies bool
Expand All @@ -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
}

Expand Down Expand Up @@ -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.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.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.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.")
Expand Down Expand Up @@ -165,9 +146,12 @@ func runUp(ctx context.Context, streams api.Streams, backend api.Service, create
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 {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/compose/up_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func TestApplyScaleOpt(t *testing.T) {
},
},
}
opt := upOptions{scale: []string{"foo=2"}}
err := opt.apply(&p, nil)
opt := createOptions{scale: []string{"foo=2"}}
err := opt.Apply(&p)
assert.NilError(t, err)
foo, err := p.GetService("foo")
assert.NilError(t, err)
Expand Down
17 changes: 9 additions & 8 deletions docs/reference/compose_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ Creates containers for a service.

### Options

| Name | Type | Default | Description |
|:-------------------|:---------|:----------|:--------------------------------------------------------------------------------------|
| `--build` | | | Build images before starting containers. |
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
| `--no-build` | | | Don't build an image, even if it's missing. |
| `--no-recreate` | | | If containers already exist, don't recreate them. Incompatible with --force-recreate. |
| `--pull` | `string` | `missing` | Pull image before running ("always"\|"missing"\|"never") |
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
| Name | Type | Default | Description |
|:-------------------|:--------------|:----------|:----------------------------------------------------------------------------------------------|
| `--build` | | | Build images before starting containers. |
| `--force-recreate` | | | Recreate containers even if their configuration and image haven't changed. |
| `--no-build` | | | Don't build an image, even if it's missing. |
| `--no-recreate` | | | If containers already exist, don't recreate them. Incompatible with --force-recreate. |
| `--pull` | `string` | `missing` | Pull image before running ("always"\|"missing"\|"never") |
| `--remove-orphans` | | | Remove containers for services not defined in the Compose file. |
| `--scale` | `stringArray` | | Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present. |


<!---MARKER_GEN_END-->
Expand Down
11 changes: 11 additions & 0 deletions docs/reference/docker_compose_create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ options:
experimentalcli: false
kubernetes: false
swarm: false
- option: scale
value_type: stringArray
default_value: '[]'
description: |
Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.
deprecated: false
hidden: false
experimental: false
experimentalcli: false
kubernetes: false
swarm: false
deprecated: false
experimental: false
experimentalcli: false
Expand Down