Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow prallel container removal #1637

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 38 additions & 42 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
godigest "github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
errorUtils "k8s.io/apimachinery/pkg/util/errors"
internalapi "k8s.io/cri-api/pkg/apis"
pb "k8s.io/cri-api/pkg/apis/runtime/v1"
"k8s.io/kubelet/pkg/types"
Expand Down Expand Up @@ -428,58 +429,51 @@ var removeContainerCommand = &cli.Command{
return cli.ShowSubcommandHelp(ctx)
}

errored := false
funcs := []func() error{}
for _, id := range ids {
resp, err := InterruptableRPC(nil, func(ctx context.Context) (*pb.ContainerStatusResponse, error) {
return runtimeClient.ContainerStatus(ctx, id, false)
})
if err != nil {
logrus.Error(err)
errored = true
continue
}
if resp.GetStatus().GetState() == pb.ContainerState_CONTAINER_RUNNING {
if ctx.Bool("force") {
if err := StopContainer(runtimeClient, id, 0); err != nil {
logrus.Errorf("stopping the container %q failed: %v", id, err)
errored = true
continue
}
} else {
logrus.Errorf("container %q is running, please stop it first", id)
errored = true
continue
funcs = append(funcs, func() error {
resp, err := InterruptableRPC(nil, func(ctx context.Context) (*pb.ContainerStatusResponse, error) {
return runtimeClient.ContainerStatus(ctx, id, false)
})
if err != nil {
return fmt.Errorf("getting container status %q: %w", id, err)
}
}

err = RemoveContainer(runtimeClient, id)
if err != nil {
logrus.Errorf("removing container %q failed: %v", id, err)
errored = true
continue
} else if !ctx.Bool("keep-logs") {
logPath := resp.GetStatus().GetLogPath()
if logPath != "" {
logRotations, err := filepath.Glob(logPath + ".*")
if err != nil {
logRotations = []string{}
if resp.GetStatus().GetState() == pb.ContainerState_CONTAINER_RUNNING {
if ctx.Bool("force") {
if err := StopContainer(runtimeClient, id, 0); err != nil {
return fmt.Errorf("stopping the container %q: %w", id, err)
}
} else {
return fmt.Errorf("container %q is running, please stop it first", id)
}
logRotations = append(logRotations, logPath)
}

for _, logFile := range logRotations {
err = os.Remove(logFile)
err = RemoveContainer(runtimeClient, id)
if err != nil {
return fmt.Errorf("removing container %q: %w", id, err)
} else if !ctx.Bool("keep-logs") {
logPath := resp.GetStatus().GetLogPath()
if logPath != "" {
logRotations, err := filepath.Glob(logPath + ".*")
if err != nil {
logrus.Errorf("removing log file %s for container %q failed: %v", logFile, id, err)
logRotations = []string{}
}
logRotations = append(logRotations, logPath)

for _, logFile := range logRotations {
err = os.Remove(logFile)
if err != nil {
logrus.Errorf("removing log file %s for container %q failed: %v", logFile, id, err)
}
}
}
}
}
}

if errored {
return errors.New("unable to remove container(s)")
return nil
})
}
return nil

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

Expand Down Expand Up @@ -984,6 +978,7 @@ func StopContainer(client internalapi.RuntimeService, id string, timeout int64)
if id == "" {
return errors.New("ID cannot be empty")
}
logrus.Debugf("Stopping container: %s (timeout = %v)", id, timeout)
if _, err := InterruptableRPC(nil, func(ctx context.Context) (any, error) {
return nil, client.StopContainer(ctx, id, timeout)
}); err != nil {
Expand Down Expand Up @@ -1023,6 +1018,7 @@ func RemoveContainer(client internalapi.RuntimeService, id string) error {
if id == "" {
return errors.New("ID cannot be empty")
}
logrus.Debugf("Removing container: %s", id)
if _, err := InterruptableRPC(nil, func(ctx context.Context) (any, error) {
return nil, client.RemoveContainer(ctx, id)
}); err != nil {
Expand Down
17 changes: 9 additions & 8 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,27 +167,26 @@ var removePodCommand = &cli.Command{

funcs := []func() error{}
for _, id := range ids {
podID := id
funcs = append(funcs, func() error {
resp, err := InterruptableRPC(nil, func(ctx context.Context) (*pb.PodSandboxStatusResponse, error) {
return runtimeClient.PodSandboxStatus(ctx, podID, false)
return runtimeClient.PodSandboxStatus(ctx, id, false)
})
if err != nil {
return fmt.Errorf("getting sandbox status of pod %q: %w", podID, err)
return fmt.Errorf("getting sandbox status of pod %q: %w", id, err)
}
if resp.Status.State == pb.PodSandboxState_SANDBOX_READY {
if ctx.Bool("force") {
if err := StopPodSandbox(runtimeClient, podID); err != nil {
return fmt.Errorf("stopping the pod sandbox %q failed: %w", podID, err)
if err := StopPodSandbox(runtimeClient, id); err != nil {
return fmt.Errorf("stopping the pod sandbox %q failed: %w", id, err)
}
} else {
return fmt.Errorf("pod sandbox %q is running, please stop it first", podID)
return fmt.Errorf("pod sandbox %q is running, please stop it first", id)
}
}

err = RemovePodSandbox(runtimeClient, podID)
err = RemovePodSandbox(runtimeClient, id)
if err != nil {
return fmt.Errorf("removing the pod sandbox %q: %w", podID, err)
return fmt.Errorf("removing the pod sandbox %q: %w", id, err)
}

return nil
Expand Down Expand Up @@ -405,6 +404,7 @@ func StopPodSandbox(client internalapi.RuntimeService, id string) error {
if id == "" {
return errors.New("ID cannot be empty")
}
logrus.Debugf("Stopping pod sandbox: %s", id)
if _, err := InterruptableRPC(nil, func(ctx context.Context) (any, error) {
return nil, client.StopPodSandbox(ctx, id)
}); err != nil {
Expand All @@ -421,6 +421,7 @@ func RemovePodSandbox(client internalapi.RuntimeService, id string) error {
if id == "" {
return errors.New("ID cannot be empty")
}
logrus.Debugf("Removing pod sandbox: %s", id)
if _, err := InterruptableRPC(nil, func(ctx context.Context) (any, error) {
return nil, client.RemovePodSandbox(ctx, id)
}); err != nil {
Expand Down
Loading