Skip to content

Commit e4ff4c3

Browse files
committed
Make runtime and image client non-public variables
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]>
1 parent ebc06b0 commit e4ff4c3

11 files changed

+114
-68
lines changed

cmd/crictl/attach.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,24 @@ var runtimeAttachCommand = cli.Command{
4848
return cli.ShowSubcommandHelp(context)
4949
}
5050

51-
if err := getRuntimeClient(context); err != nil {
51+
runtimeClient, conn, err := getRuntimeClient(context)
52+
if err != nil {
5253
return err
5354
}
55+
defer closeConnection(context, conn)
5456

5557
var opts = attachOptions{
5658
id: id,
5759
tty: context.Bool("tty"),
5860
stdin: context.Bool("stdin"),
5961
}
60-
err := Attach(runtimeClient, opts)
62+
err = Attach(runtimeClient, opts)
6163
if err != nil {
6264
return fmt.Errorf("attaching running container failed: %v", err)
6365

6466
}
6567
return nil
66-
6768
},
68-
After: closeConnection,
6969
}
7070

7171
// Attach sends an AttachRequest to server, and parses the returned AttachResponse

cmd/crictl/container.go

+41-17
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,17 @@ var createContainerCommand = cli.Command{
100100
return cli.ShowSubcommandHelp(context)
101101
}
102102

103-
if err := getRuntimeClient(context); err != nil {
103+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
104+
if err != nil {
104105
return err
105106
}
106-
if err := getImageClient(context); err != nil {
107+
defer closeConnection(context, runtimeConn)
108+
109+
imageClient, imageConn, err := getImageClient(context)
110+
if err != nil {
107111
return err
108112
}
113+
defer closeConnection(context, imageConn)
109114

110115
opts := createOptions{
111116
podID: context.Args().Get(0),
@@ -137,9 +142,11 @@ var startContainerCommand = cli.Command{
137142
if context.NArg() == 0 {
138143
return cli.ShowSubcommandHelp(context)
139144
}
140-
if err := getRuntimeClient(context); err != nil {
145+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
146+
if err != nil {
141147
return err
142148
}
149+
defer closeConnection(context, runtimeConn)
143150

144151
for i := 0; i < context.NArg(); i++ {
145152
containerID := context.Args().Get(i)
@@ -186,9 +193,11 @@ var updateContainerCommand = cli.Command{
186193
if context.NArg() == 0 {
187194
return cli.ShowSubcommandHelp(context)
188195
}
189-
if err := getRuntimeClient(context); err != nil {
196+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
197+
if err != nil {
190198
return err
191199
}
200+
defer closeConnection(context, runtimeConn)
192201

193202
options := updateOptions{
194203
CPUPeriod: context.Int64("cpu-period"),
@@ -227,9 +236,11 @@ var stopContainerCommand = cli.Command{
227236
if context.NArg() == 0 {
228237
return cli.ShowSubcommandHelp(context)
229238
}
230-
if err := getRuntimeClient(context); err != nil {
239+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
240+
if err != nil {
231241
return err
232242
}
243+
defer closeConnection(context, runtimeConn)
233244

234245
for i := 0; i < context.NArg(); i++ {
235246
containerID := context.Args().Get(i)
@@ -250,9 +261,11 @@ var removeContainerCommand = cli.Command{
250261
if context.NArg() == 0 {
251262
return cli.ShowSubcommandHelp(context)
252263
}
253-
if err := getRuntimeClient(context); err != nil {
264+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
265+
if err != nil {
254266
return err
255267
}
268+
defer closeConnection(context, runtimeConn)
256269

257270
for i := 0; i < context.NArg(); i++ {
258271
containerID := context.Args().Get(i)
@@ -283,9 +296,11 @@ var containerStatusCommand = cli.Command{
283296
if context.NArg() == 0 {
284297
return cli.ShowSubcommandHelp(context)
285298
}
286-
if err := getRuntimeClient(context); err != nil {
299+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
300+
if err != nil {
287301
return err
288302
}
303+
defer closeConnection(context, runtimeConn)
289304

290305
for i := 0; i < context.NArg(); i++ {
291306
containerID := context.Args().Get(i)
@@ -363,13 +378,17 @@ var listContainersCommand = cli.Command{
363378
},
364379
},
365380
Action: func(context *cli.Context) error {
366-
var err error
367-
if err = getRuntimeClient(context); err != nil {
381+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
382+
if err != nil {
368383
return err
369384
}
370-
if err = getImageClient(context); err != nil {
385+
defer closeConnection(context, runtimeConn)
386+
387+
imageClient, imageConn, err := getImageClient(context)
388+
if err != nil {
371389
return err
372390
}
391+
defer closeConnection(context, imageConn)
373392

374393
opts := listOptions{
375394
id: context.String("id"),
@@ -390,7 +409,7 @@ var listContainersCommand = cli.Command{
390409
return err
391410
}
392411

393-
if err = ListContainers(runtimeClient, opts); err != nil {
412+
if err = ListContainers(runtimeClient, imageClient, opts); err != nil {
394413
return fmt.Errorf("listing containers failed: %v", err)
395414
}
396415
return nil
@@ -411,12 +430,17 @@ var runContainerCommand = cli.Command{
411430
return cli.ShowSubcommandHelp(context)
412431
}
413432

414-
if err := getRuntimeClient(context); err != nil {
433+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
434+
if err != nil {
415435
return err
416436
}
417-
if err := getImageClient(context); err != nil {
437+
defer closeConnection(context, runtimeConn)
438+
439+
imageClient, imageConn, err := getImageClient(context)
440+
if err != nil {
418441
return err
419442
}
443+
defer closeConnection(context, imageConn)
420444

421445
opts := runOptions{
422446
configPath: context.Args().Get(0),
@@ -428,7 +452,7 @@ var runContainerCommand = cli.Command{
428452
},
429453
}
430454

431-
err := RunContainer(imageClient, runtimeClient, opts, context.String("runtime"))
455+
err = RunContainer(imageClient, runtimeClient, opts, context.String("runtime"))
432456
if err != nil {
433457
return fmt.Errorf("Running container failed: %v", err)
434458
}
@@ -715,7 +739,7 @@ func ContainerStatus(client pb.RuntimeServiceClient, ID, output string, quiet bo
715739

716740
// ListContainers sends a ListContainerRequest to the server, and parses
717741
// the returned ListContainerResponse.
718-
func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
742+
func ListContainers(runtimeClient pb.RuntimeServiceClient, imageClient pb.ImageServiceClient, opts listOptions) error {
719743
filter := &pb.ContainerFilter{}
720744
if opts.id != "" {
721745
filter.Id = opts.id
@@ -758,7 +782,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
758782
Filter: filter,
759783
}
760784
logrus.Debugf("ListContainerRequest: %v", request)
761-
r, err := client.ListContainers(context.Background(), request)
785+
r, err := runtimeClient.ListContainers(context.Background(), request)
762786
logrus.Debugf("ListContainerResponse: %v", r)
763787
if err != nil {
764788
return err
@@ -785,7 +809,7 @@ func ListContainers(client pb.RuntimeServiceClient, opts listOptions) error {
785809
if !matchesRegex(opts.nameRegexp, c.Metadata.Name) {
786810
continue
787811
}
788-
if match, err := matchesImage(opts.image, c.GetImage().GetImage()); err != nil {
812+
if match, err := matchesImage(imageClient, opts.image, c.GetImage().GetImage()); err != nil {
789813
return fmt.Errorf("failed to check image match %v", err)
790814
} else if !match {
791815
continue

cmd/crictl/exec.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ var runtimeExecCommand = cli.Command{
6666
return cli.ShowSubcommandHelp(context)
6767
}
6868

69-
if err := getRuntimeClient(context); err != nil {
69+
runtimeClient, conn, err := getRuntimeClient(context)
70+
if err != nil {
7071
return err
7172
}
73+
defer closeConnection(context, conn)
7274

7375
var opts = execOptions{
7476
id: context.Args().First(),
@@ -87,13 +89,12 @@ var runtimeExecCommand = cli.Command{
8789
}
8890
return nil
8991
}
90-
err := Exec(runtimeClient, opts)
92+
err = Exec(runtimeClient, opts)
9193
if err != nil {
9294
return fmt.Errorf("execing command in container failed: %v", err)
9395
}
9496
return nil
9597
},
96-
After: closeConnection,
9798
}
9899

99100
// ExecSync sends an ExecSyncRequest to the server, and parses

cmd/crictl/image.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ var pullImageCommand = cli.Command{
7272
return cli.ShowSubcommandHelp(context)
7373
}
7474

75-
if err := getImageClient(context); err != nil {
75+
imageClient, conn, err := getImageClient(context)
76+
if err != nil {
7677
return err
7778
}
79+
defer closeConnection(context, conn)
7880

7981
auth, err := getAuth(context.String("creds"), context.String("auth"))
8082
if err != nil {
@@ -126,9 +128,11 @@ var listImageCommand = cli.Command{
126128
},
127129
},
128130
Action: func(context *cli.Context) error {
129-
if err := getImageClient(context); err != nil {
131+
imageClient, conn, err := getImageClient(context)
132+
if err != nil {
130133
return err
131134
}
135+
defer closeConnection(context, conn)
132136

133137
r, err := ListImages(imageClient, context.Args().First())
134138
if err != nil {
@@ -222,9 +226,12 @@ var imageStatusCommand = cli.Command{
222226
if context.NArg() == 0 {
223227
return cli.ShowSubcommandHelp(context)
224228
}
225-
if err := getImageClient(context); err != nil {
229+
imageClient, conn, err := getImageClient(context)
230+
if err != nil {
226231
return err
227232
}
233+
defer closeConnection(context, conn)
234+
228235
verbose := !(context.Bool("quiet"))
229236
output := context.String("output")
230237
if output == "" { // default to json output
@@ -284,9 +291,12 @@ var removeImageCommand = cli.Command{
284291
if context.NArg() == 0 {
285292
return cli.ShowSubcommandHelp(context)
286293
}
287-
if err := getImageClient(context); err != nil {
294+
imageClient, conn, err := getImageClient(context)
295+
if err != nil {
288296
return err
289297
}
298+
defer closeConnection(context, conn)
299+
290300
for i := 0; i < context.NArg(); i++ {
291301
id := context.Args().Get(i)
292302

@@ -323,9 +333,12 @@ var imageFsInfoCommand = cli.Command{
323333
},
324334
},
325335
Action: func(context *cli.Context) error {
326-
if err := getImageClient(context); err != nil {
336+
imageClient, conn, err := getImageClient(context)
337+
if err != nil {
327338
return err
328339
}
340+
defer closeConnection(context, conn)
341+
329342
output := context.String("output")
330343
if output == "" { // default to json output
331344
output = "json"

cmd/crictl/info.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,18 @@ var runtimeStatusCommand = cli.Command{
4343
},
4444
},
4545
Action: func(context *cli.Context) error {
46-
err := Info(context, runtimeClient)
46+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
47+
if err != nil {
48+
return err
49+
}
50+
defer closeConnection(context, runtimeConn)
51+
52+
err = Info(context, runtimeClient)
4753
if err != nil {
4854
return fmt.Errorf("getting status of runtime failed: %v", err)
4955
}
5056
return nil
5157
},
52-
Before: getRuntimeClient,
53-
After: closeConnection,
5458
}
5559

5660
// Info sends a StatusRequest to the server, and parses the returned StatusResponse.

cmd/crictl/logs.go

-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ var logsCommand = cli.Command{
9494
}
9595
return logs.ReadLogs(context.Background(), logPath, status.GetId(), logOptions, runtimeService, os.Stdout, os.Stderr)
9696
},
97-
After: closeConnection,
9897
}
9998

10099
// parseTimestamp parses timestamp string as golang duration,

cmd/crictl/portforward.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,24 @@ var runtimePortForwardCommand = cli.Command{
4242
return cli.ShowSubcommandHelp(context)
4343
}
4444

45-
if err := getRuntimeClient(context); err != nil {
45+
runtimeClient, runtimeConn, err := getRuntimeClient(context)
46+
if err != nil {
4647
return err
4748
}
49+
defer closeConnection(context, runtimeConn)
4850

4951
var opts = portforwardOptions{
5052
id: args[0],
5153
ports: args[1:],
5254
}
53-
err := PortForward(runtimeClient, opts)
55+
err = PortForward(runtimeClient, opts)
5456
if err != nil {
5557
return fmt.Errorf("port forward failed: %v", err)
5658

5759
}
5860
return nil
5961

6062
},
61-
After: closeConnection,
6263
}
6364

6465
// PortForward sends an PortForwardRequest to server, and parses the returned PortForwardResponse

0 commit comments

Comments
 (0)