Skip to content

Commit

Permalink
Merge pull request #1462 from saschagrunert/interruptable-image
Browse files Browse the repository at this point in the history
Make image RPCs interruptable using Ctrl-C
  • Loading branch information
k8s-ci-robot authored Jun 21, 2024
2 parents 52a3c8b + c6918de commit 8516ef8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
28 changes: 19 additions & 9 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,9 @@ var removeImageCommand = &cli.Command{
}

// Container images
containers, err := runtimeClient.ListContainers(context.TODO(), nil)
containers, err := InterruptableRPC(nil, func(ctx context.Context) ([]*pb.Container, error) {
return runtimeClient.ListContainers(ctx, nil)
})
if err != nil {
return err
}
Expand Down Expand Up @@ -669,7 +671,9 @@ func PullImageWithSandbox(client internalapi.ImageManagerService, image string,
defer cancel()
}

res, err := client.PullImage(ctx, request.Image, request.Auth, request.SandboxConfig)
res, err := InterruptableRPC(ctx, func(ctx context.Context) (string, error) {
return client.PullImage(ctx, request.Image, request.Auth, request.SandboxConfig)
})
if err != nil {
return nil, err
}
Expand All @@ -683,7 +687,9 @@ func PullImageWithSandbox(client internalapi.ImageManagerService, image string,
func ListImages(client internalapi.ImageManagerService, image string) (*pb.ListImagesResponse, error) {
request := &pb.ListImagesRequest{Filter: &pb.ImageFilter{Image: &pb.ImageSpec{Image: image}}}
logrus.Debugf("ListImagesRequest: %v", request)
res, err := client.ListImages(context.TODO(), request.Filter)
res, err := InterruptableRPC(nil, func(ctx context.Context) ([]*pb.Image, error) {
return client.ListImages(ctx, request.Filter)
})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -790,7 +796,9 @@ func ImageStatus(client internalapi.ImageManagerService, image string, verbose b
Verbose: verbose,
}
logrus.Debugf("ImageStatusRequest: %v", request)
res, err := client.ImageStatus(context.TODO(), request.Image, request.Verbose)
res, err := InterruptableRPC(nil, func(ctx context.Context) (*pb.ImageStatusResponse, error) {
return client.ImageStatus(ctx, request.Image, request.Verbose)
})
if err != nil {
return nil, err
}
Expand All @@ -806,16 +814,18 @@ func RemoveImage(client internalapi.ImageManagerService, image string) error {
}
request := &pb.RemoveImageRequest{Image: &pb.ImageSpec{Image: image}}
logrus.Debugf("RemoveImageRequest: %v", request)
if err := client.RemoveImage(context.TODO(), request.Image); err != nil {
return err
}
return nil
_, err := InterruptableRPC(nil, func(ctx context.Context) (*pb.RemoveImageResponse, error) {
return nil, client.RemoveImage(ctx, request.Image)
})
return err
}

// ImageFsInfo sends an ImageStatusRequest to the server, and parses
// the returned ImageFsInfoResponse.
func ImageFsInfo(client internalapi.ImageManagerService) (*pb.ImageFsInfoResponse, error) {
res, err := client.ImageFsInfo(context.TODO())
res, err := InterruptableRPC(nil, func(ctx context.Context) (*pb.ImageFsInfoResponse, error) {
return client.ImageFsInfo(ctx)
})
if err != nil {
return nil, err
}
Expand Down
33 changes: 33 additions & 0 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,39 @@ func SetupInterruptSignalHandler() <-chan struct{} {
return signalIntStopCh
}

func InterruptableRPC[T any](
ctx context.Context,
rpcFunc func(context.Context) (T, error),
) (res T, err error) {
if ctx == nil {
ctx = context.Background()
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()

resCh := make(chan T, 1)
errCh := make(chan error, 1)

go func() {
res, err := rpcFunc(ctx)
if err != nil {
errCh <- err
return
}
resCh <- res
}()

select {
case <-SetupInterruptSignalHandler():
cancel()
return res, fmt.Errorf("interrupted: %w", ctx.Err())
case err := <-errCh:
return res, err
case res := <-resCh:
return res, nil
}
}

type listOptions struct {
// id of container or sandbox
id string
Expand Down

0 comments on commit 8516ef8

Please sign in to comment.