diff --git a/cmd/crictl/attach.go b/cmd/crictl/attach.go index a14e7d87e2..5b25ce0e08 100644 --- a/cmd/crictl/attach.go +++ b/cmd/crictl/attach.go @@ -48,24 +48,24 @@ var runtimeAttachCommand = cli.Command{ return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, conn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, conn) var opts = attachOptions{ id: id, tty: context.Bool("tty"), stdin: context.Bool("stdin"), } - err := Attach(runtimeClient, opts) + err = Attach(runtimeClient, opts) if err != nil { return fmt.Errorf("attaching running container failed: %v", err) } return nil - }, - After: closeConnection, } // Attach sends an AttachRequest to server, and parses the returned AttachResponse diff --git a/cmd/crictl/container.go b/cmd/crictl/container.go index 12f835bad5..aeabf60312 100644 --- a/cmd/crictl/container.go +++ b/cmd/crictl/container.go @@ -100,12 +100,17 @@ var createContainerCommand = cli.Command{ return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } - if err := getImageClient(context); err != nil { + defer closeConnection(context, runtimeConn) + + imageClient, imageConn, err := getImageClient(context) + if err != nil { return err } + defer closeConnection(context, imageConn) opts := createOptions{ podID: context.Args().Get(0), @@ -137,9 +142,11 @@ var startContainerCommand = cli.Command{ if context.NArg() == 0 { return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) for i := 0; i < context.NArg(); i++ { containerID := context.Args().Get(i) @@ -186,9 +193,11 @@ var updateContainerCommand = cli.Command{ if context.NArg() == 0 { return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) options := updateOptions{ CPUPeriod: context.Int64("cpu-period"), @@ -227,9 +236,11 @@ var stopContainerCommand = cli.Command{ if context.NArg() == 0 { return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) for i := 0; i < context.NArg(); i++ { containerID := context.Args().Get(i) @@ -250,9 +261,11 @@ var removeContainerCommand = cli.Command{ if context.NArg() == 0 { return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) for i := 0; i < context.NArg(); i++ { containerID := context.Args().Get(i) @@ -283,9 +296,11 @@ var containerStatusCommand = cli.Command{ if context.NArg() == 0 { return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) for i := 0; i < context.NArg(); i++ { containerID := context.Args().Get(i) @@ -363,13 +378,17 @@ var listContainersCommand = cli.Command{ }, }, Action: func(context *cli.Context) error { - var err error - if err = getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } - if err = getImageClient(context); err != nil { + defer closeConnection(context, runtimeConn) + + imageClient, imageConn, err := getImageClient(context) + if err != nil { return err } + defer closeConnection(context, imageConn) opts := listOptions{ id: context.String("id"), @@ -390,7 +409,7 @@ var listContainersCommand = cli.Command{ return err } - if err = ListContainers(runtimeClient, opts); err != nil { + if err = ListContainers(runtimeClient, imageClient, opts); err != nil { return fmt.Errorf("listing containers failed: %v", err) } return nil @@ -411,12 +430,17 @@ var runContainerCommand = cli.Command{ return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } - if err := getImageClient(context); err != nil { + defer closeConnection(context, runtimeConn) + + imageClient, imageConn, err := getImageClient(context) + if err != nil { return err } + defer closeConnection(context, imageConn) opts := runOptions{ configPath: context.Args().Get(0), @@ -428,7 +452,7 @@ var runContainerCommand = cli.Command{ }, } - err := RunContainer(imageClient, runtimeClient, opts, context.String("runtime")) + err = RunContainer(imageClient, runtimeClient, opts, context.String("runtime")) if err != nil { return fmt.Errorf("Running container failed: %v", err) } @@ -715,7 +739,7 @@ func ContainerStatus(client pb.RuntimeServiceClient, ID, output string, quiet bo // ListContainers sends a ListContainerRequest to the server, and parses // the returned ListContainerResponse. -func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error { +func ListContainers(runtimeClient pb.RuntimeServiceClient, imageClient pb.ImageServiceClient, opts listOptions) error { filter := &pb.ContainerFilter{} if opts.id != "" { filter.Id = opts.id @@ -758,7 +782,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error { Filter: filter, } logrus.Debugf("ListContainerRequest: %v", request) - r, err := client.ListContainers(context.Background(), request) + r, err := runtimeClient.ListContainers(context.Background(), request) logrus.Debugf("ListContainerResponse: %v", r) if err != nil { return err @@ -785,7 +809,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error { if !matchesRegex(opts.nameRegexp, c.Metadata.Name) { continue } - if match, err := matchesImage(opts.image, c.GetImage().GetImage()); err != nil { + if match, err := matchesImage(imageClient, opts.image, c.GetImage().GetImage()); err != nil { return fmt.Errorf("failed to check image match %v", err) } else if !match { continue diff --git a/cmd/crictl/exec.go b/cmd/crictl/exec.go index 18d466005c..e03d273b62 100644 --- a/cmd/crictl/exec.go +++ b/cmd/crictl/exec.go @@ -66,9 +66,11 @@ var runtimeExecCommand = cli.Command{ return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, conn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, conn) var opts = execOptions{ id: context.Args().First(), @@ -87,13 +89,12 @@ var runtimeExecCommand = cli.Command{ } return nil } - err := Exec(runtimeClient, opts) + err = Exec(runtimeClient, opts) if err != nil { return fmt.Errorf("execing command in container failed: %v", err) } return nil }, - After: closeConnection, } // ExecSync sends an ExecSyncRequest to the server, and parses diff --git a/cmd/crictl/image.go b/cmd/crictl/image.go index 2cff120d1e..46c2fda7e7 100644 --- a/cmd/crictl/image.go +++ b/cmd/crictl/image.go @@ -72,9 +72,11 @@ var pullImageCommand = cli.Command{ return cli.ShowSubcommandHelp(context) } - if err := getImageClient(context); err != nil { + imageClient, conn, err := getImageClient(context) + if err != nil { return err } + defer closeConnection(context, conn) auth, err := getAuth(context.String("creds"), context.String("auth")) if err != nil { @@ -126,9 +128,11 @@ var listImageCommand = cli.Command{ }, }, Action: func(context *cli.Context) error { - if err := getImageClient(context); err != nil { + imageClient, conn, err := getImageClient(context) + if err != nil { return err } + defer closeConnection(context, conn) r, err := ListImages(imageClient, context.Args().First()) if err != nil { @@ -222,9 +226,12 @@ var imageStatusCommand = cli.Command{ if context.NArg() == 0 { return cli.ShowSubcommandHelp(context) } - if err := getImageClient(context); err != nil { + imageClient, conn, err := getImageClient(context) + if err != nil { return err } + defer closeConnection(context, conn) + verbose := !(context.Bool("quiet")) output := context.String("output") if output == "" { // default to json output @@ -284,9 +291,12 @@ var removeImageCommand = cli.Command{ if context.NArg() == 0 { return cli.ShowSubcommandHelp(context) } - if err := getImageClient(context); err != nil { + imageClient, conn, err := getImageClient(context) + if err != nil { return err } + defer closeConnection(context, conn) + for i := 0; i < context.NArg(); i++ { id := context.Args().Get(i) @@ -323,9 +333,12 @@ var imageFsInfoCommand = cli.Command{ }, }, Action: func(context *cli.Context) error { - if err := getImageClient(context); err != nil { + imageClient, conn, err := getImageClient(context) + if err != nil { return err } + defer closeConnection(context, conn) + output := context.String("output") if output == "" { // default to json output output = "json" diff --git a/cmd/crictl/info.go b/cmd/crictl/info.go index 14b4b08118..212ac366ae 100644 --- a/cmd/crictl/info.go +++ b/cmd/crictl/info.go @@ -43,14 +43,18 @@ var runtimeStatusCommand = cli.Command{ }, }, Action: func(context *cli.Context) error { - err := Info(context, runtimeClient) + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { + return err + } + defer closeConnection(context, runtimeConn) + + err = Info(context, runtimeClient) if err != nil { return fmt.Errorf("getting status of runtime failed: %v", err) } return nil }, - Before: getRuntimeClient, - After: closeConnection, } // Info sends a StatusRequest to the server, and parses the returned StatusResponse. diff --git a/cmd/crictl/logs.go b/cmd/crictl/logs.go index e8c5c83516..e722c43ffb 100644 --- a/cmd/crictl/logs.go +++ b/cmd/crictl/logs.go @@ -94,7 +94,6 @@ var logsCommand = cli.Command{ } return logs.ReadLogs(context.Background(), logPath, status.GetId(), logOptions, runtimeService, os.Stdout, os.Stderr) }, - After: closeConnection, } // parseTimestamp parses timestamp string as golang duration, diff --git a/cmd/crictl/portforward.go b/cmd/crictl/portforward.go index b0c572abec..efce7feec6 100644 --- a/cmd/crictl/portforward.go +++ b/cmd/crictl/portforward.go @@ -42,15 +42,17 @@ var runtimePortForwardCommand = cli.Command{ return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) var opts = portforwardOptions{ id: args[0], ports: args[1:], } - err := PortForward(runtimeClient, opts) + err = PortForward(runtimeClient, opts) if err != nil { return fmt.Errorf("port forward failed: %v", err) @@ -58,7 +60,6 @@ var runtimePortForwardCommand = cli.Command{ return nil }, - After: closeConnection, } // PortForward sends an PortForwardRequest to server, and parses the returned PortForwardResponse diff --git a/cmd/crictl/sandbox.go b/cmd/crictl/sandbox.go index ea2b0ebfa8..2b6bf82774 100644 --- a/cmd/crictl/sandbox.go +++ b/cmd/crictl/sandbox.go @@ -57,9 +57,11 @@ var runPodCommand = cli.Command{ return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) podSandboxConfig, err := loadPodSandboxConfig(sandboxSpec) if err != nil { @@ -84,9 +86,11 @@ var stopPodCommand = cli.Command{ if context.NArg() == 0 { return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) for i := 0; i < context.NArg(); i++ { id := context.Args().Get(i) err := StopPodSandbox(runtimeClient, id) @@ -112,9 +116,11 @@ var removePodCommand = cli.Command{ if ctx.NArg() == 0 { return cli.ShowSubcommandHelp(ctx) } - if err := getRuntimeClient(ctx); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(ctx) + if err != nil { return err } + defer closeConnection(ctx, runtimeConn) for i := 0; i < ctx.NArg(); i++ { id := ctx.Args().Get(i) @@ -162,9 +168,11 @@ var podStatusCommand = cli.Command{ if context.NArg() == 0 { return cli.ShowSubcommandHelp(context) } - if err := getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) for i := 0; i < context.NArg(); i++ { id := context.Args().Get(i) @@ -234,9 +242,11 @@ var listPodCommand = cli.Command{ }, Action: func(context *cli.Context) error { var err error - if err = getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) opts := listOptions{ id: context.String("id"), diff --git a/cmd/crictl/stats.go b/cmd/crictl/stats.go index dd526b1205..5b6c41ed02 100644 --- a/cmd/crictl/stats.go +++ b/cmd/crictl/stats.go @@ -84,10 +84,11 @@ var statsCommand = cli.Command{ }, }, Action: func(context *cli.Context) error { - var err error - if err = getRuntimeClient(context); err != nil { + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { return err } + defer closeConnection(context, runtimeConn) opts := statsOptions{ all: context.Bool("all"), diff --git a/cmd/crictl/util.go b/cmd/crictl/util.go index 8e5e373a76..353fba37a7 100644 --- a/cmd/crictl/util.go +++ b/cmd/crictl/util.go @@ -40,10 +40,6 @@ const ( truncatedIDLen = 13 ) -var runtimeClient pb.RuntimeServiceClient -var imageClient pb.ImageServiceClient -var conn *grpc.ClientConn - type listOptions struct { // id of container or sandbox id string @@ -152,33 +148,30 @@ func openFile(path string) (*os.File, error) { return f, nil } -func getRuntimeClient(context *cli.Context) error { +func getRuntimeClient(context *cli.Context) (pb.RuntimeServiceClient, *grpc.ClientConn, error) { // Set up a connection to the server. - var err error - conn, err = getRuntimeClientConnection(context) + conn, err := getRuntimeClientConnection(context) if err != nil { - return fmt.Errorf("failed to connect: %v", err) + return nil, nil, fmt.Errorf("failed to connect: %v", err) } - runtimeClient = pb.NewRuntimeServiceClient(conn) - return nil + runtimeClient := pb.NewRuntimeServiceClient(conn) + return runtimeClient, conn, nil } -func getImageClient(context *cli.Context) error { +func getImageClient(context *cli.Context) (pb.ImageServiceClient, *grpc.ClientConn, error) { // Set up a connection to the server. - var err error - conn, err = getImageClientConnection(context) + conn, err := getImageClientConnection(context) if err != nil { - return fmt.Errorf("failed to connect: %v", err) + return nil, nil, fmt.Errorf("failed to connect: %v", err) } - imageClient = pb.NewImageServiceClient(conn) - return nil + imageClient := pb.NewImageServiceClient(conn) + return imageClient, conn, nil } -func closeConnection(context *cli.Context) error { +func closeConnection(context *cli.Context, conn *grpc.ClientConn) error { if conn == nil { return nil } - return conn.Close() } @@ -324,13 +317,10 @@ func matchesRegex(pattern, target string) bool { return matched } -func matchesImage(image, containerImage string) (bool, error) { +func matchesImage(imageClient pb.ImageServiceClient, image string, containerImage string) (bool, error) { if image == "" { return true, nil } - if imageClient == nil { - getImageClient(nil) - } r1, err := ImageStatus(imageClient, image, false) if err != nil { return false, err diff --git a/cmd/crictl/version.go b/cmd/crictl/version.go index 3c03d77fff..8d582d5de8 100644 --- a/cmd/crictl/version.go +++ b/cmd/crictl/version.go @@ -33,14 +33,17 @@ var runtimeVersionCommand = cli.Command{ Name: "version", Usage: "Display runtime version information", Action: func(context *cli.Context) error { - err := Version(runtimeClient, criClientVersion) + runtimeClient, runtimeConn, err := getRuntimeClient(context) + if err != nil { + return err + } + defer closeConnection(context, runtimeConn) + err = Version(runtimeClient, criClientVersion) if err != nil { return fmt.Errorf("getting the runtime version failed: %v", err) } return nil }, - Before: getRuntimeClient, - After: closeConnection, } // Version sends a VersionRequest to the server, and parses the returned VersionResponse.