mirror of
https://github.com/docker/compose.git
synced 2025-07-23 05:34:36 +02:00
remove obsolete containers first on scale down
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
aa0a4189ee
commit
2f65ace2aa
@ -20,6 +20,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -136,16 +137,18 @@ func (c *convergence) ensureService(ctx context.Context, project *types.Project,
|
|||||||
ni, erri := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
|
ni, erri := strconv.Atoi(containers[i].Labels[api.ContainerNumberLabel])
|
||||||
nj, errj := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
|
nj, errj := strconv.Atoi(containers[j].Labels[api.ContainerNumberLabel])
|
||||||
if erri == nil && errj == nil {
|
if erri == nil && errj == nil {
|
||||||
return ni < nj
|
return ni > nj
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we don't get a container number (?) just sort by creation date
|
// If we don't get a container number (?) just sort by creation date
|
||||||
return containers[i].Created < containers[j].Created
|
return containers[i].Created < containers[j].Created
|
||||||
})
|
})
|
||||||
|
|
||||||
|
slices.Reverse(containers)
|
||||||
for i, container := range containers {
|
for i, container := range containers {
|
||||||
if i >= expected {
|
if i >= expected {
|
||||||
// Scale Down
|
// Scale Down
|
||||||
|
// As we sorted containers, obsolete ones and/or highest number will be removed
|
||||||
container := container
|
container := container
|
||||||
traceOpts := append(tracing.ServiceOptions(service), tracing.ContainerOptions(container)...)
|
traceOpts := append(tracing.ServiceOptions(service), tracing.ContainerOptions(container)...)
|
||||||
eg.Go(tracing.SpanWrapFuncForErrGroup(ctx, "service/scale/down", traceOpts, func(ctx context.Context) error {
|
eg.Go(tracing.SpanWrapFuncForErrGroup(ctx, "service/scale/down", traceOpts, func(ctx context.Context) error {
|
||||||
|
17
pkg/e2e/fixtures/scale/Dockerfile
Normal file
17
pkg/e2e/fixtures/scale/Dockerfile
Normal file
@ -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
pkg/e2e/fixtures/scale/build.yaml
Normal file
3
pkg/e2e/fixtures/scale/build.yaml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
services:
|
||||||
|
test:
|
||||||
|
build: .
|
@ -184,3 +184,34 @@ func checkServiceContainer(t *testing.T, stdout, containerName, containerState s
|
|||||||
}
|
}
|
||||||
testify.Fail(t, errMessage, stdout)
|
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"))
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user