Skip to content

Commit 3aa5469

Browse files
authored
Merge pull request kubernetes#94586 from BedivereZero/master
Stop container before remove for Docker
2 parents 4ad26f2 + 437c166 commit 3aa5469

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

cmd/kubeadm/app/util/runtime/runtime.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,15 @@ func (runtime *CRIRuntime) RemoveContainers(containers []string) error {
140140
func (runtime *DockerRuntime) RemoveContainers(containers []string) error {
141141
errs := []error{}
142142
for _, container := range containers {
143-
out, err := runtime.exec.Command("docker", "rm", "--force", "--volumes", container).CombinedOutput()
143+
out, err := runtime.exec.Command("docker", "stop", container).CombinedOutput()
144144
if err != nil {
145145
// don't stop on errors, try to remove as many containers as possible
146-
errs = append(errs, errors.Wrapf(err, "failed to remove running container %s: output: %s, error", container, string(out)))
146+
errs = append(errs, errors.Wrapf(err, "failed to stop running container %s: output: %s, error", container, string(out)))
147+
} else {
148+
out, err = runtime.exec.Command("docker", "rm", "--volumes", container).CombinedOutput()
149+
if err != nil {
150+
errs = append(errs, errors.Wrapf(err, "failed to remove running container %s: output: %s, error", container, string(out)))
151+
}
147152
}
148153
}
149154
return errorsutil.NewAggregate(errs)

cmd/kubeadm/app/util/runtime/runtime_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ func TestRemoveContainers(t *testing.T) {
185185
fakeOK, fakeOK, fakeOK, fakeOK, fakeOK, fakeOK, // Test case 1
186186
fakeOK, fakeOK, fakeOK, fakeErr, fakeOK, fakeOK,
187187
fakeErr, fakeOK, fakeOK, fakeErr, fakeOK,
188-
fakeOK, fakeOK, fakeOK,
189-
fakeOK, fakeErr, fakeOK,
188+
fakeOK, fakeOK, fakeOK, fakeOK, fakeOK, fakeOK,
189+
fakeOK, fakeOK, fakeOK, fakeErr, fakeOK, fakeOK,
190+
fakeErr, fakeOK, fakeOK, fakeErr, fakeOK,
190191
},
191192
}
192193
execer := fakeexec.FakeExec{
@@ -204,7 +205,8 @@ func TestRemoveContainers(t *testing.T) {
204205
{"invalid: CRI rmp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
205206
{"invalid: CRI stopp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
206207
{"valid: remove containers using docker", constants.DefaultDockerCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, false},
207-
{"invalid: remove containers using docker", constants.DefaultDockerCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, true},
208+
{"invalid: docker rm failure", constants.DefaultDockerCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, true},
209+
{"invalid: docker stop failure", constants.DefaultDockerCRISocket, []string{"k8s_c1", "k8s_c2", "k8s_c3"}, true},
208210
}
209211

210212
for _, tc := range cases {

0 commit comments

Comments
 (0)