2017-12-19 09:34:34 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-08-30 21:06:27 +00:00
|
|
|
"time"
|
2017-12-19 09:34:34 +00:00
|
|
|
|
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
2018-08-30 21:06:27 +00:00
|
|
|
apitypes "github.com/moby/buildkit/api/types"
|
2018-06-22 02:06:12 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-12-19 09:34:34 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WorkerInfo struct {
|
2018-06-22 02:06:12 +00:00
|
|
|
ID string
|
|
|
|
Labels map[string]string
|
|
|
|
Platforms []specs.Platform
|
2018-08-30 21:06:27 +00:00
|
|
|
GCPolicy []PruneInfo
|
2017-12-19 09:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) ListWorkers(ctx context.Context, opts ...ListWorkersOption) ([]*WorkerInfo, error) {
|
|
|
|
info := &ListWorkersInfo{}
|
|
|
|
for _, o := range opts {
|
2018-07-26 00:20:03 +00:00
|
|
|
o.SetListWorkersOption(info)
|
2017-12-19 09:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2018-06-22 02:06:12 +00:00
|
|
|
ID: w.ID,
|
|
|
|
Labels: w.Labels,
|
2018-06-30 01:35:39 +00:00
|
|
|
Platforms: pb.ToSpecPlatforms(w.Platforms),
|
2018-08-30 21:06:27 +00:00
|
|
|
GCPolicy: fromAPIGCPolicy(w.GCPolicy),
|
2017-12-19 09:34:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return wi, nil
|
|
|
|
}
|
|
|
|
|
2018-07-26 00:20:03 +00:00
|
|
|
type ListWorkersOption interface {
|
|
|
|
SetListWorkersOption(*ListWorkersInfo)
|
|
|
|
}
|
2017-12-19 09:34:34 +00:00
|
|
|
|
|
|
|
type ListWorkersInfo struct {
|
|
|
|
Filter []string
|
|
|
|
}
|
2018-08-30 21:06:27 +00:00
|
|
|
|
|
|
|
func fromAPIGCPolicy(in []*apitypes.GCPolicy) []PruneInfo {
|
|
|
|
out := make([]PruneInfo, 0, len(in))
|
|
|
|
for _, p := range in {
|
|
|
|
out = append(out, PruneInfo{
|
|
|
|
All: p.All,
|
|
|
|
Filter: p.Filters,
|
|
|
|
KeepDuration: time.Duration(p.KeepDuration),
|
|
|
|
KeepBytes: p.KeepBytes,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|