forked from moby/buildkit
/
workers.go
118 lines (103 loc) · 2.67 KB
/
workers.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
package debug
import (
"context"
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/containerd/containerd/platforms"
"github.com/moby/buildkit/client"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/tonistiigi/units"
"github.com/urfave/cli"
)
var WorkersCommand = cli.Command{
Name: "workers",
Usage: "list workers",
Action: listWorkers,
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "filter, f",
Usage: "containerd-style filter string slice",
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Verbose output",
},
},
}
func resolveClient(c *cli.Context) (*client.Client, error) {
return client.New(commandContext(c), c.GlobalString("addr"), client.WithFailFast())
}
func listWorkers(clicontext *cli.Context) error {
c, err := resolveClient(clicontext)
if err != nil {
return err
}
workers, err := c.ListWorkers(commandContext(clicontext), client.WithFilter(clicontext.StringSlice("filter")))
if err != nil {
return err
}
tw := tabwriter.NewWriter(os.Stdout, 1, 8, 1, '\t', 0)
if clicontext.Bool("verbose") {
printWorkersVerbose(tw, workers)
} else {
printWorkersTable(tw, workers)
}
return nil
}
func printWorkersVerbose(tw *tabwriter.Writer, winfo []*client.WorkerInfo) {
for _, wi := range winfo {
fmt.Fprintf(tw, "ID:\t%s\n", wi.ID)
fmt.Fprintf(tw, "Platforms:\t%s\n", joinPlatforms(wi.Platforms))
fmt.Fprintf(tw, "Labels:\n")
for _, k := range sortedKeys(wi.Labels) {
v := wi.Labels[k]
fmt.Fprintf(tw, "\t%s:\t%s\n", k, v)
}
for i, rule := range wi.GCPolicy {
fmt.Fprintf(tw, "GC Policy rule#%d:\n", i)
fmt.Fprintf(tw, "\tAll:\t%v\n", rule.All)
if len(rule.Filter) > 0 {
fmt.Fprintf(tw, "\tFilters:\t%s\n", strings.Join(rule.Filter, " "))
}
if rule.KeepDuration > 0 {
fmt.Fprintf(tw, "\tKeep Duration:\t%v\n", rule.KeepDuration.String())
}
if rule.KeepBytes > 0 {
fmt.Fprintf(tw, "\tKeep Bytes:\t%g\n", units.Bytes(rule.KeepBytes))
}
}
fmt.Fprintf(tw, "\n")
}
tw.Flush()
}
func printWorkersTable(tw *tabwriter.Writer, winfo []*client.WorkerInfo) {
fmt.Fprintln(tw, "ID\tPLATFORMS")
for _, wi := range winfo {
id := wi.ID
fmt.Fprintf(tw, "%s\t%s\n", id, joinPlatforms(wi.Platforms))
}
tw.Flush()
}
func sortedKeys(m map[string]string) []string {
s := make([]string, len(m))
i := 0
for k := range m {
s[i] = k
i++
}
sort.Strings(s)
return s
}
func commandContext(c *cli.Context) context.Context {
return c.App.Metadata["context"].(context.Context)
}
func joinPlatforms(p []specs.Platform) string {
str := make([]string, 0, len(p))
for _, pp := range p {
str = append(str, platforms.Format(platforms.Normalize(pp)))
}
return strings.Join(str, ",")
}