Skip to content

Commit

Permalink
Remove pod sandoxes in parallel
Browse files Browse the repository at this point in the history
We now use a classic work-stealing approach to remove the sandboxes in
parallel if multiple are defined.

Signed-off-by: Sascha Grunert <sgrunert@suse.com>
  • Loading branch information
saschagrunert committed Apr 6, 2020
1 parent fad4c4e commit 1655f54
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import (
"time"

"github.com/docker/go-units"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
"golang.org/x/net/context"

errorUtils "k8s.io/apimachinery/pkg/util/errors"
pb "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
)

Expand Down Expand Up @@ -140,45 +142,39 @@ var removePodCommand = &cli.Command{
}
}

if len(ids) == 0 {
lenIDs := len(ids)
if lenIDs == 0 {
return cli.ShowSubcommandHelp(ctx)
}

errored := false
funcs := []func() error{}
for _, id := range ids {
resp, err := runtimeClient.PodSandboxStatus(context.Background(),
&pb.PodSandboxStatusRequest{PodSandboxId: id})
if err != nil {
logrus.Error(err)
errored = true
continue
}
if resp.Status.State == pb.PodSandboxState_SANDBOX_READY {
if ctx.Bool("force") {
if err := StopPodSandbox(runtimeClient, id); err != nil {
logrus.Errorf("stopping the pod sandbox %q failed: %v", id, err)
errored = true
continue
funcs = append(funcs, func() error {
resp, err := runtimeClient.PodSandboxStatus(context.Background(),
&pb.PodSandboxStatusRequest{PodSandboxId: id})
if err != nil {
return errors.Wrapf(err, "getting sandbox status of pod %q", id)
}
if resp.Status.State == pb.PodSandboxState_SANDBOX_READY {
if ctx.Bool("force") {
if err := StopPodSandbox(runtimeClient, id); err != nil {
return errors.Wrapf(err, "stopping the pod sandbox %q failed", id)
}
} else {
return errors.Errorf("pod sandbox %q is running, please stop it first", id)
}
} else {
logrus.Errorf("pod sandbox %q is running, please stop it first", id)
errored = true
continue
}
}

err = RemovePodSandbox(runtimeClient, id)
if err != nil {
logrus.Errorf("removing the pod sandbox %q failed: %v", id, err)
errored = true
continue
}
}
err = RemovePodSandbox(runtimeClient, id)
if err != nil {
return errors.Wrapf(err, "removing the pod sandbox %q", id)
}

if errored {
return fmt.Errorf("unable to remove sandbox(es)")
return nil
})
}
return nil

return errorUtils.AggregateGoroutines(funcs...)
},
}

Expand Down

0 comments on commit 1655f54

Please sign in to comment.