Skip to content

Commit

Permalink
Merge pull request #110837 from SataQiu/fix-kubeadm-20220628
Browse files Browse the repository at this point in the history
kubeadm: support retry mechanism for removing container in reset phase
  • Loading branch information
k8s-ci-robot committed Jun 29, 2022
2 parents d0f5496 + 3889a6c commit dafa55b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
2 changes: 2 additions & 0 deletions cmd/kubeadm/app/constants/constants.go
Expand Up @@ -218,6 +218,8 @@ const (
APICallWithReadTimeout = 15 * time.Second
// PullImageRetry specifies how many times ContainerRuntime retries when pulling image failed
PullImageRetry = 5
// RemoveContainerRetry specifies how many times ContainerRuntime retries when removing container failed
RemoveContainerRetry = 5

// DefaultControlPlaneTimeout specifies the default control plane (actually API Server) timeout for use by kubeadm
DefaultControlPlaneTimeout = 4 * time.Minute
Expand Down
23 changes: 17 additions & 6 deletions cmd/kubeadm/app/util/runtime/runtime.go
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pkg/errors"

errorsutil "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
utilsexec "k8s.io/utils/exec"

"k8s.io/kubernetes/cmd/kubeadm/app/constants"
Expand Down Expand Up @@ -97,15 +98,25 @@ func (runtime *CRIRuntime) ListKubeContainers() ([]string, error) {
func (runtime *CRIRuntime) RemoveContainers(containers []string) error {
errs := []error{}
for _, container := range containers {
out, err := runtime.crictl("stopp", container).CombinedOutput()
if err != nil {
// don't stop on errors, try to remove as many containers as possible
errs = append(errs, errors.Wrapf(err, "failed to stop running pod %s: output: %s, error", container, string(out)))
} else {
var lastErr error
for i := 0; i < constants.RemoveContainerRetry; i++ {
klog.V(5).Infof("Attempting to remove container %v", container)
out, err := runtime.crictl("stopp", container).CombinedOutput()
if err != nil {
lastErr = errors.Wrapf(err, "failed to stop running pod %s: output: %s", container, string(out))
continue
}
out, err = runtime.crictl("rmp", container).CombinedOutput()
if err != nil {
errs = append(errs, errors.Wrapf(err, "failed to remove running container %s: output: %s, error", container, string(out)))
lastErr = errors.Wrapf(err, "failed to remove running container %s: output: %s", container, string(out))
continue
}
lastErr = nil
break
}

if lastErr != nil {
errs = append(errs, lastErr)
}
}
return errorsutil.NewAggregate(errs)
Expand Down
11 changes: 4 additions & 7 deletions cmd/kubeadm/app/util/runtime/runtime_test.go
Expand Up @@ -167,11 +167,8 @@ func TestRemoveContainers(t *testing.T) {
fcmd := fakeexec.FakeCmd{
CombinedOutputScript: []fakeexec.FakeAction{
fakeOK, fakeOK, fakeOK, fakeOK, fakeOK, fakeOK, // Test case 1
fakeOK, fakeOK, fakeOK, fakeErr, fakeOK, fakeOK,
fakeErr, fakeOK, fakeOK, fakeErr, fakeOK,
fakeOK, fakeOK, fakeOK, fakeOK, fakeOK, fakeOK,
fakeOK, fakeOK, fakeOK, fakeErr, fakeOK, fakeOK,
fakeErr, fakeOK, fakeOK, fakeErr, fakeOK,
fakeOK, fakeOK, fakeOK, fakeErr, fakeOK, fakeErr, fakeOK, fakeErr, fakeOK, fakeErr, fakeOK, fakeErr, fakeOK, fakeOK, // Test case 2
fakeErr, fakeErr, fakeErr, fakeErr, fakeErr, fakeOK, fakeOK, fakeOK, fakeOK, // Test case 3
},
}
execer := fakeexec.FakeExec{
Expand All @@ -186,8 +183,8 @@ func TestRemoveContainers(t *testing.T) {
isError bool
}{
{"valid: remove containers using CRI", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, false}, // Test case 1
{"invalid: CRI rmp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
{"invalid: CRI stopp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true},
{"invalid: CRI rmp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true}, // Test case 2
{"invalid: CRI stopp failure", "unix:///var/run/crio/crio.sock", []string{"k8s_p1", "k8s_p2", "k8s_p3"}, true}, // Test case 3
}

for _, tc := range cases {
Expand Down

0 comments on commit dafa55b

Please sign in to comment.