forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect.go
189 lines (166 loc) · 4.91 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
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
package service
import (
"errors"
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/docker/swarmkit/api"
"github.com/docker/swarmkit/cmd/swarmctl/common"
"github.com/docker/swarmkit/cmd/swarmctl/task"
"github.com/docker/swarmkit/protobuf/ptypes"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
)
func printServiceSummary(service *api.Service, running int) {
w := tabwriter.NewWriter(os.Stdout, 8, 8, 8, ' ', 0)
defer w.Flush()
task := service.Spec.Task
common.FprintfIfNotEmpty(w, "ID\t: %s\n", service.ID)
common.FprintfIfNotEmpty(w, "Name\t: %s\n", service.Spec.Annotations.Name)
if len(service.Spec.Annotations.Labels) > 0 {
fmt.Fprintln(w, "Labels\t")
for k, v := range service.Spec.Annotations.Labels {
fmt.Fprintf(w, " %s\t: %s\n", k, v)
}
}
common.FprintfIfNotEmpty(w, "Replicas\t: %s\n", getServiceReplicasTxt(service, running))
if service.UpdateStatus != nil {
fmt.Fprintln(w, "Update Status\t")
fmt.Fprintln(w, " State\t:", service.UpdateStatus.State)
started, err := ptypes.Timestamp(service.UpdateStatus.StartedAt)
if err == nil {
fmt.Fprintln(w, " Started\t:", humanize.Time(started))
}
if service.UpdateStatus.State == api.UpdateStatus_COMPLETED {
completed, err := ptypes.Timestamp(service.UpdateStatus.CompletedAt)
if err == nil {
fmt.Fprintln(w, " Completed\t:", humanize.Time(completed))
}
}
fmt.Fprintln(w, " Message\t:", service.UpdateStatus.Message)
}
fmt.Fprintln(w, "Template\t")
fmt.Fprintln(w, " Container\t")
ctr := service.Spec.Task.GetContainer()
common.FprintfIfNotEmpty(w, " Image\t: %s\n", ctr.Image)
common.FprintfIfNotEmpty(w, " Command\t: %q\n", strings.Join(ctr.Command, " "))
common.FprintfIfNotEmpty(w, " Args\t: [%s]\n", strings.Join(ctr.Args, ", "))
common.FprintfIfNotEmpty(w, " Env\t: [%s]\n", strings.Join(ctr.Env, ", "))
if task.Placement != nil {
common.FprintfIfNotEmpty(w, " Constraints\t: %s\n", strings.Join(task.Placement.Constraints, ", "))
}
if task.Resources != nil {
res := task.Resources
fmt.Fprintln(w, " Resources\t")
printResources := func(w io.Writer, r *api.Resources) {
if r.NanoCPUs != 0 {
fmt.Fprintf(w, " CPU\t: %g\n", float64(r.NanoCPUs)/1e9)
}
if r.MemoryBytes != 0 {
fmt.Fprintf(w, " Memory\t: %s\n", humanize.IBytes(uint64(r.MemoryBytes)))
}
}
if res.Reservations != nil {
fmt.Fprintln(w, " Reservations:\t")
printResources(w, res.Reservations)
}
if res.Limits != nil {
fmt.Fprintln(w, " Limits:\t")
printResources(w, res.Limits)
}
}
if len(service.Spec.Networks) > 0 {
fmt.Fprintf(w, " Networks:\t")
for _, n := range service.Spec.Networks {
fmt.Fprintf(w, " %s", n.Target)
}
}
if service.Endpoint != nil && len(service.Endpoint.Ports) > 0 {
fmt.Fprintln(w, "\nPorts:")
for _, port := range service.Endpoint.Ports {
fmt.Fprintf(w, " - Name\t= %s\n", port.Name)
fmt.Fprintf(w, " Protocol\t= %s\n", port.Protocol)
fmt.Fprintf(w, " Port\t= %d\n", port.TargetPort)
fmt.Fprintf(w, " SwarmPort\t= %d\n", port.PublishedPort)
}
}
if len(ctr.Mounts) > 0 {
fmt.Fprintln(w, " Mounts:")
for _, v := range ctr.Mounts {
fmt.Fprintf(w, " - target = %s\n", v.Target)
fmt.Fprintf(w, " source = %s\n", v.Source)
fmt.Fprintf(w, " readonly = %v\n", v.ReadOnly)
fmt.Fprintf(w, " type = %v\n", strings.ToLower(v.Type.String()))
}
}
if task.LogDriver != nil {
fmt.Fprintf(w, " LogDriver\t: %s\n", task.LogDriver.Name)
var keys []string
if task.LogDriver.Options != nil {
for k := range task.LogDriver.Options {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := task.LogDriver.Options[k]
if v != "" {
fmt.Fprintf(w, " %s\t: %s\n", k, v)
} else {
fmt.Fprintf(w, " %s\t\n", k)
}
}
}
}
}
var (
inspectCmd = &cobra.Command{
Use: "inspect <service ID>",
Short: "Inspect a service",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("service ID missing")
}
flags := cmd.Flags()
all, err := flags.GetBool("all")
if err != nil {
return err
}
c, err := common.Dial(cmd)
if err != nil {
return err
}
res := common.NewResolver(cmd, c)
service, err := getService(common.Context(cmd), c, args[0])
if err != nil {
return err
}
r, err := c.ListTasks(common.Context(cmd),
&api.ListTasksRequest{
Filters: &api.ListTasksRequest_Filters{
ServiceIDs: []string{service.ID},
},
})
if err != nil {
return err
}
var running int
for _, t := range r.Tasks {
if t.Status.State == api.TaskStateRunning {
running++
}
}
printServiceSummary(service, running)
if len(r.Tasks) > 0 {
fmt.Printf("\n")
task.Print(r.Tasks, all, res)
}
return nil
},
}
)
func init() {
inspectCmd.Flags().BoolP("all", "a", false, "Show all tasks (default shows just running)")
}