Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 4d05e02

Browse files
authored
Merge pull request #1287 from docker/scale
2 parents f52bdc5 + bbdced6 commit 4d05e02

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

cli/cmd/compose/up.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ import (
2222
"os"
2323
"os/signal"
2424
"path/filepath"
25+
"strconv"
26+
"strings"
2527
"syscall"
2628

2729
"github.com/docker/compose-cli/api/client"
@@ -54,6 +56,7 @@ type upOptions struct {
5456
noStart bool
5557
cascadeStop bool
5658
exitCodeFrom string
59+
scale []string
5760
}
5861

5962
func (o upOptions) recreateStrategy() string {
@@ -98,6 +101,7 @@ func upCommand(p *projectOptions, contextType string) *cobra.Command {
98101
flags.BoolVarP(&opts.Detach, "detach", "d", false, "Detached mode: Run containers in the background")
99102
flags.BoolVar(&opts.Build, "build", false, "Build images before starting containers.")
100103
flags.BoolVar(&opts.removeOrphans, "remove-orphans", false, "Remove containers for services not defined in the Compose file.")
104+
flags.StringArrayVar(&opts.scale, "scale", []string{}, "Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.")
101105

102106
switch contextType {
103107
case store.AciContextType:
@@ -119,6 +123,11 @@ func runUp(ctx context.Context, opts upOptions, services []string) error {
119123
return err
120124
}
121125

126+
err = applyScaleOpt(opts.scale, project)
127+
if err != nil {
128+
return err
129+
}
130+
122131
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
123132
return "", c.ComposeService().Up(ctx, project, compose.UpOptions{
124133
Detach: opts.Detach,
@@ -133,6 +142,11 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
133142
return err
134143
}
135144

145+
err = applyScaleOpt(opts.scale, project)
146+
if err != nil {
147+
return err
148+
}
149+
136150
if opts.exitCodeFrom != "" {
137151
_, err := project.GetService(opts.exitCodeFrom)
138152
if err != nil {
@@ -201,6 +215,44 @@ func runCreateStart(ctx context.Context, opts upOptions, services []string) erro
201215
return err
202216
}
203217

218+
func applyScaleOpt(opts []string, project *types.Project) error {
219+
for _, scale := range opts {
220+
split := strings.Split(scale, "=")
221+
if len(split) != 2 {
222+
return fmt.Errorf("invalid --scale option %q. Should be SERVICE=NUM", scale)
223+
}
224+
name := split[0]
225+
replicas, err := strconv.Atoi(split[1])
226+
if err != nil {
227+
return err
228+
}
229+
err = setServiceScale(project, name, replicas)
230+
if err != nil {
231+
return err
232+
}
233+
}
234+
return nil
235+
}
236+
237+
func setServiceScale(project *types.Project, name string, replicas int) error {
238+
for i, s := range project.Services {
239+
if s.Name == name {
240+
service, err := project.GetService(name)
241+
if err != nil {
242+
return err
243+
}
244+
if service.Deploy == nil {
245+
service.Deploy = &types.DeployConfig{}
246+
}
247+
count := uint64(replicas)
248+
service.Deploy.Replicas = &count
249+
project.Services[i] = service
250+
return nil
251+
}
252+
}
253+
return fmt.Errorf("unknown service %q", name)
254+
}
255+
204256
func setup(ctx context.Context, opts composeOptions, services []string) (*client.Client, *types.Project, error) {
205257
c, err := client.NewWithDefaultLocalBackend(ctx)
206258
if err != nil {

cli/cmd/compose/up_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright 2020 Docker Compose CLI authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package compose
18+
19+
import (
20+
"testing"
21+
22+
"github.com/compose-spec/compose-go/types"
23+
"gotest.tools/v3/assert"
24+
)
25+
26+
func TestApplyScaleOpt(t *testing.T) {
27+
p := types.Project{
28+
Services: []types.ServiceConfig{
29+
{
30+
Name: "foo",
31+
},
32+
{
33+
Name: "bar",
34+
},
35+
},
36+
}
37+
err := applyScaleOpt([]string{"foo=2"}, &p)
38+
assert.NilError(t, err)
39+
foo, err := p.GetService("foo")
40+
assert.NilError(t, err)
41+
assert.Check(t, *foo.Deploy.Replicas == 2)
42+
}

0 commit comments

Comments
 (0)