Skip to content

Commit

Permalink
Make runtime and image client non-global variables
Browse files Browse the repository at this point in the history
This commits removed the three global variables:

```
var runtimeClient pb.RuntimeServiceClient
var imageClient pb.ImageServiceClient
var conn *grpc.ClientConn
```

and scopes them to their actual used functions. This avoids usage of the
variables if they are uninitialized and provides a more clear control
flow.

All related functions have been adapted to work with the new functions.

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Aug 8, 2019
1 parent ebc06b0 commit f55f3fd
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 68 deletions.
8 changes: 4 additions & 4 deletions cmd/crictl/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 41 additions & 17 deletions cmd/crictl/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"),
Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions cmd/crictl/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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
Expand Down
23 changes: 18 additions & 5 deletions cmd/crictl/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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"
Expand Down
10 changes: 7 additions & 3 deletions cmd/crictl/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion cmd/crictl/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions cmd/crictl/portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,24 @@ 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)

}
return nil

},
After: closeConnection,
}

// PortForward sends an PortForwardRequest to server, and parses the returned PortForwardResponse
Expand Down
Loading

0 comments on commit f55f3fd

Please sign in to comment.