-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
info.go
493 lines (438 loc) · 16.5 KB
/
info.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// FIXME(thaJeztah): remove once we are a module; the go:build directive prevents go from downgrading language version to go1.16:
//go:build go1.19
package system
import (
"context"
"fmt"
"io"
"regexp"
"sort"
"strings"
"github.com/docker/cli/cli"
pluginmanager "github.com/docker/cli/cli-plugins/manager"
"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/completion"
"github.com/docker/cli/cli/command/formatter"
"github.com/docker/cli/cli/debug"
flagsHelper "github.com/docker/cli/cli/flags"
"github.com/docker/cli/templates"
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/api/types/system"
"github.com/docker/docker/registry"
"github.com/docker/go-units"
"github.com/spf13/cobra"
)
type infoOptions struct {
format string
}
type clientInfo struct {
Debug bool
clientVersion
Plugins []pluginmanager.Plugin
Warnings []string
}
type dockerInfo struct {
// This field should/could be ServerInfo but is anonymous to
// preserve backwards compatibility in the JSON rendering
// which has ServerInfo immediately within the top-level
// object.
*system.Info `json:",omitempty"`
ServerErrors []string `json:",omitempty"`
UserName string `json:"-"`
ClientInfo *clientInfo `json:",omitempty"`
ClientErrors []string `json:",omitempty"`
}
func (i *dockerInfo) clientPlatform() string {
if i.ClientInfo != nil && i.ClientInfo.Platform != nil {
return i.ClientInfo.Platform.Name
}
return ""
}
// NewInfoCommand creates a new cobra.Command for `docker info`
func NewInfoCommand(dockerCli command.Cli) *cobra.Command {
var opts infoOptions
cmd := &cobra.Command{
Use: "info [OPTIONS]",
Short: "Display system-wide information",
Args: cli.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runInfo(cmd.Context(), cmd, dockerCli, &opts)
},
Annotations: map[string]string{
"category-top": "12",
"aliases": "docker system info, docker info",
},
ValidArgsFunction: completion.NoComplete,
}
cmd.Flags().StringVarP(&opts.format, "format", "f", "", flagsHelper.InspectFormatHelp)
return cmd
}
func runInfo(ctx context.Context, cmd *cobra.Command, dockerCli command.Cli, opts *infoOptions) error {
info := dockerInfo{
ClientInfo: &clientInfo{
// Don't pass a dockerCLI to newClientVersion(), because we currently
// don't include negotiated API version, and want to avoid making an
// API connection when only printing the Client section.
clientVersion: newClientVersion(dockerCli.CurrentContext(), nil),
Debug: debug.IsEnabled(),
},
Info: &system.Info{},
}
if plugins, err := pluginmanager.ListPlugins(dockerCli, cmd.Root()); err == nil {
info.ClientInfo.Plugins = plugins
} else {
info.ClientErrors = append(info.ClientErrors, err.Error())
}
if needsServerInfo(opts.format, info) {
if dinfo, err := dockerCli.Client().Info(ctx); err == nil {
info.Info = &dinfo
} else {
info.ServerErrors = append(info.ServerErrors, err.Error())
if opts.format == "" {
// reset the server info to prevent printing "empty" Server info
// and warnings, but don't reset it if a custom format was specified
// to prevent errors from Go's template parsing during format.
info.Info = nil
} else {
// if a format is provided, print the error, as it may be hidden
// otherwise if the template doesn't include the ServerErrors field.
fprintln(dockerCli.Err(), err)
}
}
}
if opts.format == "" {
info.UserName = dockerCli.ConfigFile().AuthConfigs[registry.IndexServer].Username
info.ClientInfo.APIVersion = dockerCli.CurrentVersion()
return prettyPrintInfo(dockerCli, info)
}
return formatInfo(dockerCli.Out(), info, opts.format)
}
// placeHolders does a rudimentary match for possible placeholders in a
// template, matching a '.', followed by an letter (a-z/A-Z).
var placeHolders = regexp.MustCompile(`\.[a-zA-Z]`)
// needsServerInfo detects if the given template uses any server information.
// If only client-side information is used in the template, we can skip
// connecting to the daemon. This allows (e.g.) to only get cli-plugin
// information, without also making a (potentially expensive) API call.
func needsServerInfo(template string, info dockerInfo) bool {
if len(template) == 0 || placeHolders.FindString(template) == "" {
// The template is empty, or does not contain formatting fields
// (e.g. `table` or `raw` or `{{ json .}}`). Assume we need server-side
// information to render it.
return true
}
// A template is provided and has at least one field set.
tmpl, err := templates.NewParse("", template)
if err != nil {
// ignore parsing errors here, and let regular code handle them
return true
}
type sparseInfo struct {
ClientInfo *clientInfo `json:",omitempty"`
ClientErrors []string `json:",omitempty"`
}
// This constructs an "info" object that only has the client-side fields.
err = tmpl.Execute(io.Discard, sparseInfo{
ClientInfo: info.ClientInfo,
ClientErrors: info.ClientErrors,
})
// If executing the template failed, it means the template needs
// server-side information as well. If it succeeded without server-side
// information, we don't need to make API calls to collect that information.
return err != nil
}
func prettyPrintInfo(streams command.Streams, info dockerInfo) error {
// Only append the platform info if it's not empty, to prevent printing a trailing space.
if p := info.clientPlatform(); p != "" {
fprintln(streams.Out(), "Client:", p)
} else {
fprintln(streams.Out(), "Client:")
}
if info.ClientInfo != nil {
prettyPrintClientInfo(streams, *info.ClientInfo)
}
for _, err := range info.ClientErrors {
fprintln(streams.Err(), "ERROR:", err)
}
fprintln(streams.Out())
fprintln(streams.Out(), "Server:")
if info.Info != nil {
for _, err := range prettyPrintServerInfo(streams, &info) {
info.ServerErrors = append(info.ServerErrors, err.Error())
}
}
for _, err := range info.ServerErrors {
fprintln(streams.Err(), "ERROR:", err)
}
if len(info.ServerErrors) > 0 || len(info.ClientErrors) > 0 {
return fmt.Errorf("errors pretty printing info")
}
return nil
}
func prettyPrintClientInfo(streams command.Streams, info clientInfo) {
fprintlnNonEmpty(streams.Out(), " Version: ", info.Version)
fprintln(streams.Out(), " Context: ", info.Context)
fprintln(streams.Out(), " Debug Mode:", info.Debug)
if len(info.Plugins) > 0 {
fprintln(streams.Out(), " Plugins:")
for _, p := range info.Plugins {
if p.Err == nil {
fprintf(streams.Out(), " %s: %s (%s)\n", p.Name, p.ShortDescription, p.Vendor)
fprintlnNonEmpty(streams.Out(), " Version: ", p.Version)
fprintlnNonEmpty(streams.Out(), " Path: ", p.Path)
} else {
info.Warnings = append(info.Warnings, fmt.Sprintf("WARNING: Plugin %q is not valid: %s", p.Path, p.Err))
}
}
}
if len(info.Warnings) > 0 {
fprintln(streams.Err(), strings.Join(info.Warnings, "\n"))
}
}
//nolint:gocyclo
func prettyPrintServerInfo(streams command.Streams, info *dockerInfo) []error {
var errs []error
output := streams.Out()
fprintln(output, " Containers:", info.Containers)
fprintln(output, " Running:", info.ContainersRunning)
fprintln(output, " Paused:", info.ContainersPaused)
fprintln(output, " Stopped:", info.ContainersStopped)
fprintln(output, " Images:", info.Images)
fprintlnNonEmpty(output, " Server Version:", info.ServerVersion)
fprintlnNonEmpty(output, " Storage Driver:", info.Driver)
if info.DriverStatus != nil {
for _, pair := range info.DriverStatus {
fprintf(output, " %s: %s\n", pair[0], pair[1])
}
}
if info.SystemStatus != nil {
for _, pair := range info.SystemStatus {
fprintf(output, " %s: %s\n", pair[0], pair[1])
}
}
fprintlnNonEmpty(output, " Logging Driver:", info.LoggingDriver)
fprintlnNonEmpty(output, " Cgroup Driver:", info.CgroupDriver)
fprintlnNonEmpty(output, " Cgroup Version:", info.CgroupVersion)
fprintln(output, " Plugins:")
fprintln(output, " Volume:", strings.Join(info.Plugins.Volume, " "))
fprintln(output, " Network:", strings.Join(info.Plugins.Network, " "))
if len(info.Plugins.Authorization) != 0 {
fprintln(output, " Authorization:", strings.Join(info.Plugins.Authorization, " "))
}
fprintln(output, " Log:", strings.Join(info.Plugins.Log, " "))
if len(info.CDISpecDirs) > 0 {
fprintln(output, " CDI spec directories:")
for _, dir := range info.CDISpecDirs {
fprintf(output, " %s\n", dir)
}
}
fprintln(output, " Swarm:", info.Swarm.LocalNodeState)
printSwarmInfo(output, *info.Info)
if len(info.Runtimes) > 0 {
names := make([]string, 0, len(info.Runtimes))
for name := range info.Runtimes {
names = append(names, name)
}
fprintln(output, " Runtimes:", strings.Join(names, " "))
fprintln(output, " Default Runtime:", info.DefaultRuntime)
}
if info.OSType == "linux" {
fprintln(output, " Init Binary:", info.InitBinary)
for _, ci := range []struct {
Name string
Commit system.Commit
}{
{"containerd", info.ContainerdCommit},
{"runc", info.RuncCommit},
{"init", info.InitCommit},
} {
fprintf(output, " %s version: %s", ci.Name, ci.Commit.ID)
if ci.Commit.ID != ci.Commit.Expected {
fprintf(output, " (expected: %s)", ci.Commit.Expected)
}
fprintln(output)
}
if len(info.SecurityOptions) != 0 {
if kvs, err := system.DecodeSecurityOptions(info.SecurityOptions); err != nil {
errs = append(errs, err)
} else {
fprintln(output, " Security Options:")
for _, so := range kvs {
fprintln(output, " "+so.Name)
for _, o := range so.Options {
if o.Key == "profile" {
fprintln(output, " Profile:", o.Value)
}
}
}
}
}
}
// Isolation only has meaning on a Windows daemon.
if info.OSType == "windows" {
fprintln(output, " Default Isolation:", info.Isolation)
}
fprintlnNonEmpty(output, " Kernel Version:", info.KernelVersion)
fprintlnNonEmpty(output, " Operating System:", info.OperatingSystem)
fprintlnNonEmpty(output, " OSType:", info.OSType)
fprintlnNonEmpty(output, " Architecture:", info.Architecture)
fprintln(output, " CPUs:", info.NCPU)
fprintln(output, " Total Memory:", units.BytesSize(float64(info.MemTotal)))
fprintlnNonEmpty(output, " Name:", info.Name)
fprintlnNonEmpty(output, " ID:", info.ID)
fprintln(output, " Docker Root Dir:", info.DockerRootDir)
fprintln(output, " Debug Mode:", info.Debug)
// The daemon collects this information regardless if "debug" is
// enabled. Print the debugging information if either the daemon,
// or the client has debug enabled. We should probably improve this
// logic and print any of these if set (but some special rules are
// needed for file-descriptors, which may use "-1".
if info.Debug || debug.IsEnabled() {
fprintln(output, " File Descriptors:", info.NFd)
fprintln(output, " Goroutines:", info.NGoroutines)
fprintln(output, " System Time:", info.SystemTime)
fprintln(output, " EventsListeners:", info.NEventsListener)
}
fprintlnNonEmpty(output, " HTTP Proxy:", info.HTTPProxy)
fprintlnNonEmpty(output, " HTTPS Proxy:", info.HTTPSProxy)
fprintlnNonEmpty(output, " No Proxy:", info.NoProxy)
fprintlnNonEmpty(output, " Username:", info.UserName)
if len(info.Labels) > 0 {
fprintln(output, " Labels:")
for _, lbl := range info.Labels {
fprintln(output, " "+lbl)
}
}
fprintln(output, " Experimental:", info.ExperimentalBuild)
if info.RegistryConfig != nil && (len(info.RegistryConfig.InsecureRegistryCIDRs) > 0 || len(info.RegistryConfig.IndexConfigs) > 0) {
fprintln(output, " Insecure Registries:")
for _, registryConfig := range info.RegistryConfig.IndexConfigs {
if !registryConfig.Secure {
fprintln(output, " "+registryConfig.Name)
}
}
for _, registryConfig := range info.RegistryConfig.InsecureRegistryCIDRs {
mask, _ := registryConfig.Mask.Size()
fprintf(output, " %s/%d\n", registryConfig.IP.String(), mask)
}
}
if info.RegistryConfig != nil && len(info.RegistryConfig.Mirrors) > 0 {
fprintln(output, " Registry Mirrors:")
for _, mirror := range info.RegistryConfig.Mirrors {
fprintln(output, " "+mirror)
}
}
fprintln(output, " Live Restore Enabled:", info.LiveRestoreEnabled)
if info.ProductLicense != "" {
fprintln(output, " Product License:", info.ProductLicense)
}
if info.DefaultAddressPools != nil && len(info.DefaultAddressPools) > 0 {
fprintln(output, " Default Address Pools:")
for _, pool := range info.DefaultAddressPools {
fprintf(output, " Base: %s, Size: %d\n", pool.Base, pool.Size)
}
}
fprintln(output)
for _, w := range info.Warnings {
fprintln(streams.Err(), w)
}
return errs
}
//nolint:gocyclo
func printSwarmInfo(output io.Writer, info system.Info) {
if info.Swarm.LocalNodeState == swarm.LocalNodeStateInactive || info.Swarm.LocalNodeState == swarm.LocalNodeStateLocked {
return
}
fprintln(output, " NodeID:", info.Swarm.NodeID)
if info.Swarm.Error != "" {
fprintln(output, " Error:", info.Swarm.Error)
}
fprintln(output, " Is Manager:", info.Swarm.ControlAvailable)
if info.Swarm.Cluster != nil && info.Swarm.ControlAvailable && info.Swarm.Error == "" && info.Swarm.LocalNodeState != swarm.LocalNodeStateError {
fprintln(output, " ClusterID:", info.Swarm.Cluster.ID)
fprintln(output, " Managers:", info.Swarm.Managers)
fprintln(output, " Nodes:", info.Swarm.Nodes)
var strAddrPool strings.Builder
if info.Swarm.Cluster.DefaultAddrPool != nil {
for _, p := range info.Swarm.Cluster.DefaultAddrPool {
strAddrPool.WriteString(p + " ")
}
fprintln(output, " Default Address Pool:", strAddrPool.String())
fprintln(output, " SubnetSize:", info.Swarm.Cluster.SubnetSize)
}
if info.Swarm.Cluster.DataPathPort > 0 {
fprintln(output, " Data Path Port:", info.Swarm.Cluster.DataPathPort)
}
fprintln(output, " Orchestration:")
taskHistoryRetentionLimit := int64(0)
if info.Swarm.Cluster.Spec.Orchestration.TaskHistoryRetentionLimit != nil {
taskHistoryRetentionLimit = *info.Swarm.Cluster.Spec.Orchestration.TaskHistoryRetentionLimit
}
fprintln(output, " Task History Retention Limit:", taskHistoryRetentionLimit)
fprintln(output, " Raft:")
fprintln(output, " Snapshot Interval:", info.Swarm.Cluster.Spec.Raft.SnapshotInterval)
if info.Swarm.Cluster.Spec.Raft.KeepOldSnapshots != nil {
fprintf(output, " Number of Old Snapshots to Retain: %d\n", *info.Swarm.Cluster.Spec.Raft.KeepOldSnapshots)
}
fprintln(output, " Heartbeat Tick:", info.Swarm.Cluster.Spec.Raft.HeartbeatTick)
fprintln(output, " Election Tick:", info.Swarm.Cluster.Spec.Raft.ElectionTick)
fprintln(output, " Dispatcher:")
fprintln(output, " Heartbeat Period:", units.HumanDuration(info.Swarm.Cluster.Spec.Dispatcher.HeartbeatPeriod))
fprintln(output, " CA Configuration:")
fprintln(output, " Expiry Duration:", units.HumanDuration(info.Swarm.Cluster.Spec.CAConfig.NodeCertExpiry))
fprintln(output, " Force Rotate:", info.Swarm.Cluster.Spec.CAConfig.ForceRotate)
if caCert := strings.TrimSpace(info.Swarm.Cluster.Spec.CAConfig.SigningCACert); caCert != "" {
fprintf(output, " Signing CA Certificate: \n%s\n\n", caCert)
}
if len(info.Swarm.Cluster.Spec.CAConfig.ExternalCAs) > 0 {
fprintln(output, " External CAs:")
for _, entry := range info.Swarm.Cluster.Spec.CAConfig.ExternalCAs {
fprintf(output, " %s: %s\n", entry.Protocol, entry.URL)
}
}
fprintln(output, " Autolock Managers:", info.Swarm.Cluster.Spec.EncryptionConfig.AutoLockManagers)
fprintln(output, " Root Rotation In Progress:", info.Swarm.Cluster.RootRotationInProgress)
}
fprintln(output, " Node Address:", info.Swarm.NodeAddr)
if len(info.Swarm.RemoteManagers) > 0 {
managers := []string{}
for _, entry := range info.Swarm.RemoteManagers {
managers = append(managers, entry.Addr)
}
sort.Strings(managers)
fprintln(output, " Manager Addresses:")
for _, entry := range managers {
fprintf(output, " %s\n", entry)
}
}
}
func formatInfo(output io.Writer, info dockerInfo, format string) error {
if format == formatter.JSONFormatKey {
format = formatter.JSONFormat
}
// Ensure slice/array fields render as `[]` not `null`
if info.ClientInfo != nil && info.ClientInfo.Plugins == nil {
info.ClientInfo.Plugins = make([]pluginmanager.Plugin, 0)
}
tmpl, err := templates.Parse(format)
if err != nil {
return cli.StatusError{
StatusCode: 64,
Status: "template parsing error: " + err.Error(),
}
}
err = tmpl.Execute(output, info)
fprintln(output)
return err
}
func fprintf(w io.Writer, format string, a ...any) {
_, _ = fmt.Fprintf(w, format, a...)
}
func fprintln(w io.Writer, a ...any) {
_, _ = fmt.Fprintln(w, a...)
}
func fprintlnNonEmpty(w io.Writer, label, value string) {
if value != "" {
_, _ = fmt.Fprintln(w, label, value)
}
}