Skip to content

Commit

Permalink
remove obsolete containers first on scale down
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Nov 6, 2024
1 parent fbbd6f8 commit fa78cef
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/compose/convergence.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -136,16 +137,18 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
ni, erri := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
nj, errj := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
if erri == nil && errj == nil {
return ni < nj
return ni > nj
}

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

slices.Reverse(containers)
for i, container := range containers {
if i >= expected {
// Scale Down
// As we sorted containers, obsolete ones and/or highest number will be removed
container := container
traceOpts := append(tracing.ServiceOptions(service), tracing.ContainerOptions(container)...)
eg.Go(tracing.SpanWrapFuncForErrGroup(ctx, "service/scale/down", traceOpts, func(ctx context.Context) error {
Expand Down
17 changes: 17 additions & 0 deletions pkg/e2e/fixtures/scale/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright 2020 Docker Compose CLI authors

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM nginx:alpine
ARG FOO
LABEL FOO=$FOO
3 changes: 3 additions & 0 deletions pkg/e2e/fixtures/scale/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
services:
test:
build: .
31 changes: 31 additions & 0 deletions pkg/e2e/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,34 @@ func checkServiceContainer(t *testing.T, stdout, containerName, containerState s
}
testify.Fail(t, errMessage, stdout)
}

func TestScaleDownNoRecreate(t *testing.T) {
const projectName = "scale-down-recreated-test"
c := NewCLI(t, WithEnv(
"COMPOSE_PROJECT_NAME="+projectName))

reset := func() {
c.RunDockerComposeCmd(t, "down", "--rmi", "all")
}
t.Cleanup(reset)
c.RunDockerComposeCmd(t, "-f", "fixtures/scale/build.yaml", "build", "--build-arg", "FOO=test")
c.RunDockerComposeCmd(t, "-f", "fixtures/scale/build.yaml", "up", "-d", "--scale", "test=2")

c.RunDockerComposeCmd(t, "-f", "fixtures/scale/build.yaml", "build", "--build-arg", "FOO=updated")
c.RunDockerComposeCmd(t, "-f", "fixtures/scale/build.yaml", "up", "-d", "--scale", "test=4", "--no-recreate")

res := c.RunDockerComposeCmd(t, "ps", "--format", "{{.Name}}", "test")
res.Assert(t, icmd.Success)
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-1"))
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-2"))
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-3"))
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-4"))

t.Log("scale down removes obsolete replica #1 and #2")
c.NewDockerComposeCmd(t, "--project-directory", "fixtures/scale", "up", "-d", "--scale", "test=2")

res = c.RunDockerComposeCmd(t, "ps", "--format", "{{.Name}}", "test")
res.Assert(t, icmd.Success)
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-3"))
assert.Check(t, strings.Contains(res.Stdout(), "scale-down-recreated-test-test-4"))
}

0 comments on commit fa78cef

Please sign in to comment.