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

Add timeout option to CreateContainer, RunContainer, RunPodSandbox #666

Merged
merged 1 commit into from
Sep 24, 2020
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
23 changes: 20 additions & 3 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type runOptions struct {
// podConfig is path to the config for sandbox
podConfig string

// the create timeout
timeout time.Duration

// the image pull options
*pullOptions
}
Expand Down Expand Up @@ -120,7 +123,12 @@ var createContainerCommand = &cli.Command{
Name: "create",
Usage: "Create a new container",
ArgsUsage: "POD container-config.[json|yaml] pod-config.[json|yaml]",
Flags: createPullFlags,
Flags: append(createPullFlags, &cli.DurationFlag{
Name: "timeout",
Aliases: []string{"t"},
Value: 0,
Usage: "Seconds to wait for a container create request before cancelling the request",
}),

Action: func(context *cli.Context) error {
if context.Args().Len() != 3 {
Expand Down Expand Up @@ -159,6 +167,7 @@ var createContainerCommand = &cli.Command{
creds: context.String("creds"),
auth: context.String("auth"),
},
timeout: context.Duration("timeout"),
},
}

Expand Down Expand Up @@ -530,6 +539,11 @@ var runContainerCommand = &cli.Command{
Name: "runtime",
Aliases: []string{"r"},
Usage: "Runtime handler to use. Available options are defined by the container runtime.",
}, &cli.DurationFlag{
Name: "timeout",
Aliases: []string{"t"},
Value: 10,
Usage: "Seconds to wait for a container create request before cancelling the request",
}),

Action: func(context *cli.Context) error {
Expand Down Expand Up @@ -569,6 +583,7 @@ var runContainerCommand = &cli.Command{
creds: context.String("creds"),
auth: context.String("auth"),
},
timeout: context.Duration("timeout"),
}

err = RunContainer(imageClient, runtimeClient, opts, context.String("runtime"))
Expand All @@ -591,7 +606,9 @@ func RunContainer(
if err != nil {
return errors.Wrap(err, "load podSandboxConfig")
}
podID, err := RunPodSandbox(rClient, podSandboxConfig, runtime)
// set the timeout for the RunPodSandbox request to 0, because the
// timeout option is documented as being for container creation.
podID, err := RunPodSandbox(rClient, podSandboxConfig, runtime, 0)
if err != nil {
return errors.Wrap(err, "run pod sandbox")
}
Expand Down Expand Up @@ -655,7 +672,7 @@ func CreateContainer(
SandboxConfig: podConfig,
}
logrus.Debugf("CreateContainerRequest: %v", request)
r, err := rClient.CreateContainer(context.Background(), request)
r, err := rClient.CreateContainer(ctxWithTimeout(opts.timeout), request)
logrus.Debugf("CreateContainerResponse: %v", r)
if err != nil {
return "", err
Expand Down
12 changes: 9 additions & 3 deletions cmd/crictl/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ var runPodCommand = &cli.Command{
Aliases: []string{"r"},
Usage: "Runtime handler to use. Available options are defined by the container runtime.",
},
&cli.DurationFlag{
Name: "timeout",
Aliases: []string{"t"},
Value: 0,
Usage: "Seconds to wait for a run pod sandox request to complete before cancelling the request",
},
},

Action: func(context *cli.Context) error {
Expand All @@ -72,7 +78,7 @@ var runPodCommand = &cli.Command{
}

// Test RuntimeServiceClient.RunPodSandbox
podID, err := RunPodSandbox(runtimeClient, podSandboxConfig, context.String("runtime"))
podID, err := RunPodSandbox(runtimeClient, podSandboxConfig, context.String("runtime"), context.Duration("timeout"))
if err != nil {
return errors.Wrap(err, "run pod sandbox")
}
Expand Down Expand Up @@ -314,13 +320,13 @@ var listPodCommand = &cli.Command{

// RunPodSandbox sends a RunPodSandboxRequest to the server, and parses
// the returned RunPodSandboxResponse.
func RunPodSandbox(client pb.RuntimeServiceClient, config *pb.PodSandboxConfig, runtime string) (string, error) {
func RunPodSandbox(client pb.RuntimeServiceClient, config *pb.PodSandboxConfig, runtime string, timeout time.Duration) (string, error) {
request := &pb.RunPodSandboxRequest{
Config: config,
RuntimeHandler: runtime,
}
logrus.Debugf("RunPodSandboxRequest: %v", request)
r, err := client.RunPodSandbox(context.Background(), request)
r, err := client.RunPodSandbox(ctxWithTimeout(timeout), request)
logrus.Debugf("RunPodSandboxResponse: %v", r)
if err != nil {
return "", err
Expand Down
13 changes: 13 additions & 0 deletions cmd/crictl/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ package main

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"regexp"
"sort"
"strings"
"time"

"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -342,3 +344,14 @@ func matchesImage(imageClient pb.ImageServiceClient, image string, containerImag
}
return r1.Image.Id == r2.Image.Id, nil
}

func ctxWithTimeout(timeout time.Duration) context.Context {
ctx, cancel := context.WithCancel(context.Background())
if timeout > 0 {
go func() {
time.Sleep(timeout * time.Second)
cancel()
}()
}
return ctx
}