forked from AliyunContainerService/pouch
/
inspect.go
127 lines (108 loc) · 3.1 KB
/
inspect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"context"
"os"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/cli/inspect"
"github.com/spf13/cobra"
)
// inspectDescription is used to describe inspect command in detail and auto generate command doc.
var inspectDescription = "Return detailed information on Pouch container"
// InspectCommand is used to implement 'inspect' command.
type InspectCommand struct {
baseCommand
format string
}
// Init initializes InspectCommand command.
func (p *InspectCommand) Init(c *Cli) {
p.cli = c
p.cmd = &cobra.Command{
Use: "inspect [OPTIONS] CONTAINER",
Short: "Get the detailed information of container",
Long: inspectDescription,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return p.runInspect(args)
},
Example: inspectExample(),
}
p.addFlags()
}
// addFlags adds flags for specific command.
func (p *InspectCommand) addFlags() {
p.cmd.Flags().StringVarP(&p.format, "format", "f", "", "Format the output using the given go template")
}
// runInspect is the entry of InspectCommand command.
func (p *InspectCommand) runInspect(args []string) error {
ctx := context.Background()
apiClient := p.cli.Client()
getRefFunc := func(ref string) (interface{}, error) {
res, err := apiClient.ContainerGet(ctx, ref)
if err != nil {
return nil, err
}
return convContainerJSONToInspectContainerJSON(res), nil
}
return inspect.Inspect(os.Stdout, args, p.format, getRefFunc)
}
// inspectExample shows examples in inspect command, and is used in auto-generated cli docs.
func inspectExample() string {
return `$ pouch inspect 08e
[
{
"Id": "08ee444faa3c6634ecdecea26de46e8a6a16efefd9afb72eb3457320b333fc60",
"Created": "2017-12-04 14:48:59",
"Path": "",
"Args": null,
"State": {
"StartedAt": "0001-01-01T00:00:00Z",
"Status": 0,
"FinishedAt": "0001-01-01T00:00:00Z",
"Pid": 25006,
"ExitCode": 0,
"Error": ""
},
"Image": "registry.docker-cn.com/library/centos:latest",
"ResolvConfPath": "",
"HostnamePath": "",
"HostsPath": "",
"LogPath": "",
"Name": "08ee44",
"RestartCount": 0,
"Driver": "",
"MountLabel": "",
"ProcessLabel": "",
"AppArmorProfile": "",
"ExecIDs": null,
"HostConfig": null,
"HostRootPath": ""
}
]`
}
type inspectContainerJSON struct {
*types.ContainerJSON
Config *inspectContainerConfig `json:"Config,omitempty"`
}
type inspectContainerConfig struct {
*types.ContainerConfig
QuotaID string `json:"QuotaId,omitempty"`
}
// convContainerJSONToInspectContainerJSON converts ContainerJSON into inspectContainerJSON.
//
// NOTE: It is used to align with existing system and we should remove it after
// upgrade existing system.
func convContainerJSONToInspectContainerJSON(from *types.ContainerJSON) *inspectContainerJSON {
iCfg := &inspectContainerConfig{
ContainerConfig: from.Config,
QuotaID: "",
}
if iCfg.ContainerConfig != nil {
iCfg.QuotaID = iCfg.ContainerConfig.QuotaID
// NOTE: make the QuotaID empty and use QuotaId
iCfg.ContainerConfig.QuotaID = ""
}
return &inspectContainerJSON{
ContainerJSON: from,
Config: iCfg,
}
}