-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Akihiro Suda <[email protected]>
- Loading branch information
1 parent
621596d
commit 8456e32
Showing
18 changed files
with
1,102 additions
and
176 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
|
||
controlapi "github.com/moby/buildkit/api/services/control" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
type WorkerInfo struct { | ||
ID string | ||
Labels map[string]string | ||
} | ||
|
||
func (c *Client) ListWorkers(ctx context.Context, opts ...ListWorkersOption) ([]*WorkerInfo, error) { | ||
info := &ListWorkersInfo{} | ||
for _, o := range opts { | ||
o(info) | ||
} | ||
|
||
req := &controlapi.ListWorkersRequest{Filter: info.Filter} | ||
resp, err := c.controlClient().ListWorkers(ctx, req) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to list workers") | ||
} | ||
|
||
var wi []*WorkerInfo | ||
|
||
for _, w := range resp.Record { | ||
wi = append(wi, &WorkerInfo{ | ||
ID: w.ID, | ||
Labels: w.Labels, | ||
}) | ||
} | ||
|
||
return wi, nil | ||
} | ||
|
||
type ListWorkersOption func(*ListWorkersInfo) | ||
|
||
type ListWorkersInfo struct { | ||
Filter []string | ||
} | ||
|
||
func WithWorkerFilter(f []string) ListWorkersOption { | ||
return func(wi *ListWorkersInfo) { | ||
wi.Filter = f | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package debug | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"sort" | ||
"text/tabwriter" | ||
|
||
"github.com/moby/buildkit/client" | ||
"github.com/moby/buildkit/util/appcontext" | ||
"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(c.GlobalString("addr"), client.WithBlock()) | ||
} | ||
|
||
func listWorkers(clicontext *cli.Context) error { | ||
c, err := resolveClient(clicontext) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
workers, err := c.ListWorkers(appcontext.Context(), client.WithWorkerFilter(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, "Labels:\n") | ||
for _, kv := range sortMap(wi.Labels) { | ||
fmt.Fprintf(tw, "\t%s:\t%s\n", kv[0], kv[1]) | ||
} | ||
fmt.Fprintf(tw, "\n") | ||
} | ||
|
||
tw.Flush() | ||
} | ||
|
||
func printWorkersTable(tw *tabwriter.Writer, winfo []*client.WorkerInfo) { | ||
fmt.Fprintln(tw, "ID") | ||
|
||
for _, wi := range winfo { | ||
id := wi.ID | ||
fmt.Fprintf(tw, "%s\n", id) | ||
} | ||
|
||
tw.Flush() | ||
} | ||
|
||
func sortMap(m map[string]string) [][2]string { | ||
var s [][2]string | ||
for k, v := range m { | ||
s = append(s, [2]string{k, v}) | ||
} | ||
sort.Slice(s, func(i, j int) bool { | ||
return s[i][0] < s[j][0] | ||
}) | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.