Skip to content

Commit ded43fc

Browse files
authored
Merge pull request containerd#1850 from weikequ/refactor-container-inspect
Refactor container inspect
2 parents 5ec3339 + 76fb67f commit ded43fc

3 files changed

Lines changed: 118 additions & 61 deletions

File tree

cmd/nerdctl/container_inspect.go

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,10 @@
1717
package main
1818

1919
import (
20-
"context"
2120
"fmt"
22-
"time"
2321

24-
"github.com/containerd/nerdctl/pkg/clientutil"
25-
"github.com/containerd/nerdctl/pkg/containerinspector"
26-
"github.com/containerd/nerdctl/pkg/formatter"
27-
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
28-
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
22+
"github.com/containerd/nerdctl/pkg/api/types"
23+
"github.com/containerd/nerdctl/pkg/cmd/container"
2924

3025
"github.com/spf13/cobra"
3126
)
@@ -52,74 +47,33 @@ func newContainerInspectCommand() *cobra.Command {
5247
return containerInspectCommand
5348
}
5449

50+
var validModeType = map[string]bool{
51+
"native": true,
52+
"dockercompat": true,
53+
}
54+
5555
func containerInspectAction(cmd *cobra.Command, args []string) error {
5656
globalOptions, err := processRootCmdFlags(cmd)
5757
if err != nil {
5858
return err
5959
}
60-
client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
61-
if err != nil {
62-
return err
63-
}
64-
defer cancel()
65-
6660
mode, err := cmd.Flags().GetString("mode")
6761
if err != nil {
6862
return err
6963
}
70-
f := &containerInspector{
71-
mode: mode,
72-
}
73-
walker := &containerwalker.ContainerWalker{
74-
Client: client,
75-
OnFound: f.Handler,
76-
}
77-
78-
var errs []error
79-
for _, req := range args {
80-
n, err := walker.Walk(ctx, req)
81-
if err != nil {
82-
errs = append(errs, err)
83-
} else if n == 0 {
84-
errs = append(errs, fmt.Errorf("no such object: %s", req))
85-
}
86-
}
87-
if len(errs) > 0 {
88-
return fmt.Errorf("%d errors: %v", len(errs), errs)
64+
if len(mode) > 0 && !validModeType[mode] {
65+
return fmt.Errorf("%q is not a valid value for --mode", mode)
8966
}
9067
format, err := cmd.Flags().GetString("format")
9168
if err != nil {
9269
return err
9370
}
94-
return formatter.FormatSlice(format, cmd.OutOrStdout(), f.entries)
95-
}
96-
97-
type containerInspector struct {
98-
mode string
99-
entries []interface{}
100-
}
101-
102-
func (x *containerInspector) Handler(ctx context.Context, found containerwalker.Found) error {
103-
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
104-
defer cancel()
105-
106-
n, err := containerinspector.Inspect(ctx, found.Container)
107-
if err != nil {
108-
return err
109-
}
110-
switch x.mode {
111-
case "native":
112-
x.entries = append(x.entries, n)
113-
case "dockercompat":
114-
d, err := dockercompat.ContainerFromNative(n)
115-
if err != nil {
116-
return err
117-
}
118-
x.entries = append(x.entries, d)
119-
default:
120-
return fmt.Errorf("unknown mode %q", x.mode)
121-
}
122-
return nil
71+
return container.Inspect(cmd.Context(), types.ContainerInspectCommandOptions{
72+
GOptions: globalOptions,
73+
Format: format,
74+
Mode: mode,
75+
Containers: args,
76+
}, cmd.OutOrStdout())
12377
}
12478

12579
func containerInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

pkg/api/types/container_types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,15 @@ type ContainerRemoveCommandOptions struct {
3333
// Volumes removes anonymous volumes associated with the container
3434
Volumes bool
3535
}
36+
37+
// ContainerInspectCommandOptions specifies options for `nerdctl container inspect`
38+
type ContainerInspectCommandOptions struct {
39+
// GOptions is the global options
40+
GOptions GlobalCommandOptions
41+
// Format of the output
42+
Format string
43+
// Inspect mode, either dockercompat or native
44+
Mode string
45+
// Containers are the containers to be inspected
46+
Containers []string
47+
}

pkg/cmd/container/inspect.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package container
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"io"
23+
"time"
24+
25+
"github.com/containerd/nerdctl/pkg/api/types"
26+
"github.com/containerd/nerdctl/pkg/clientutil"
27+
"github.com/containerd/nerdctl/pkg/containerinspector"
28+
"github.com/containerd/nerdctl/pkg/formatter"
29+
"github.com/containerd/nerdctl/pkg/idutil/containerwalker"
30+
"github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat"
31+
)
32+
33+
func Inspect(ctx context.Context, options types.ContainerInspectCommandOptions, stdout io.Writer) error {
34+
client, ctx, cancel, err := clientutil.NewClient(ctx, options.GOptions.Namespace, options.GOptions.Address)
35+
if err != nil {
36+
return err
37+
}
38+
defer cancel()
39+
40+
f := &containerInspector{
41+
mode: options.Mode,
42+
}
43+
44+
walker := &containerwalker.ContainerWalker{
45+
Client: client,
46+
OnFound: f.Handler,
47+
}
48+
49+
var errs []error
50+
for _, req := range options.Containers {
51+
n, err := walker.Walk(ctx, req)
52+
if err != nil {
53+
errs = append(errs, err)
54+
} else if n == 0 {
55+
errs = append(errs, fmt.Errorf("no such container: %s", req))
56+
}
57+
}
58+
if len(errs) > 0 {
59+
return fmt.Errorf("%d errors: %v", len(errs), errs)
60+
}
61+
62+
return formatter.FormatSlice(options.Format, stdout, f.entries)
63+
}
64+
65+
type containerInspector struct {
66+
mode string
67+
entries []interface{}
68+
}
69+
70+
func (x *containerInspector) Handler(ctx context.Context, found containerwalker.Found) error {
71+
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
72+
defer cancel()
73+
74+
n, err := containerinspector.Inspect(ctx, found.Container)
75+
if err != nil {
76+
return err
77+
}
78+
switch x.mode {
79+
case "native":
80+
x.entries = append(x.entries, n)
81+
case "dockercompat":
82+
d, err := dockercompat.ContainerFromNative(n)
83+
if err != nil {
84+
return err
85+
}
86+
x.entries = append(x.entries, d)
87+
default:
88+
return fmt.Errorf("unknown mode %q", x.mode)
89+
}
90+
return nil
91+
}

0 commit comments

Comments
 (0)