Skip to content

Commit 2f65ace

Browse files
ndeloofglours
authored andcommitted
remove obsolete containers first on scale down
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent aa0a418 commit 2f65ace

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

pkg/compose/convergence.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"errors"
2222
"fmt"
23+
"slices"
2324
"sort"
2425
"strconv"
2526
"strings"
@@ -136,16 +137,18 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
136137
ni, erri := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
137138
nj, errj := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
138139
if erri == nil && errj == nil {
139-
return ni < nj
140+
return ni > nj
140141
}
141142

142143
// If we don't get a container number (?) just sort by creation date
143144
return containers[i].Created < containers[j].Created
144145
})
145146

147+
slices.Reverse(containers)
146148
for i, container := range containers {
147149
if i >= expected {
148150
// Scale Down
151+
// As we sorted containers, obsolete ones and/or highest number will be removed
149152
container := container
150153
traceOpts := append(tracing.ServiceOptions(service), tracing.ContainerOptions(container)...)
151154
eg.Go(tracing.SpanWrapFuncForErrGroup(ctx, "service/scale/down", traceOpts, func(ctx context.Context) error {

pkg/e2e/fixtures/scale/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2020 Docker Compose CLI authors
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
FROM nginx:alpine
16+
ARG FOO
17+
LABEL FOO=$FOO

pkg/e2e/fixtures/scale/build.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
services:
2+
test:
3+
build: .

pkg/e2e/scale_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,34 @@ func checkServiceContainer(t *testing.T, stdout, containerName, containerState s
184184
}
185185
testify.Fail(t, errMessage, stdout)
186186
}
187+
188+
func TestScaleDownNoRecreate(t *testing.T) {
189+
const projectName = "scale-down-recreated-test"
190+
c := NewCLI(t, WithEnv(
191+
"COMPOSE_PROJECT_NAME="+projectName))
192+
193+
reset := func() {
194+
c.RunDockerComposeCmd(t, "down", "--rmi", "all")
195+
}
196+
t.Cleanup(reset)
197+
c.RunDockerComposeCmd(t, "-f", "fixtures/scale/build.yaml", "build", "--build-arg", "FOO=test")
198+
c.RunDockerComposeCmd(t, "-f", "fixtures/scale/build.yaml", "up", "-d", "--scale", "test=2")
199+
200+
c.RunDockerComposeCmd(t, "-f", "fixtures/scale/build.yaml", "build", "--build-arg", "FOO=updated")
201+
c.RunDockerComposeCmd(t, "-f", "fixtures/scale/build.yaml", "up", "-d", "--scale", "test=4", "--no-recreate")
202+
203+
res := c.RunDockerComposeCmd(t, "ps", "--format", "{{.Name}}", "test")
204+
res.Assert(t, icmd.Success)
205+
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-1"))
206+
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-2"))
207+
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-3"))
208+
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-4"))
209+
210+
t.Log("scale down removes obsolete replica #1 and #2")
211+
c.NewDockerComposeCmd(t, "--project-directory", "fixtures/scale", "up", "-d", "--scale", "test=2")
212+
213+
res = c.RunDockerComposeCmd(t, "ps", "--format", "{{.Name}}", "test")
214+
res.Assert(t, icmd.Success)
215+
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-3"))
216+
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-4"))
217+
}

0 commit comments

Comments
 (0)