2018-06-09 00:39:07 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-09-06 18:50:41 +00:00
|
|
|
"time"
|
2018-06-09 00:39:07 +00:00
|
|
|
|
|
|
|
controlapi "github.com/moby/buildkit/api/services/control"
|
2018-09-06 18:50:41 +00:00
|
|
|
apitypes "github.com/moby/buildkit/api/types"
|
2018-06-26 17:59:57 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2018-06-09 00:39:07 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2018-09-21 20:43:26 +00:00
|
|
|
// WorkerInfo contains information about a worker
|
2018-06-09 00:39:07 +00:00
|
|
|
type WorkerInfo struct {
|
2018-06-26 17:59:57 +00:00
|
|
|
ID string
|
|
|
|
Labels map[string]string
|
|
|
|
Platforms []specs.Platform
|
2018-09-06 18:50:41 +00:00
|
|
|
GCPolicy []PruneInfo
|
2018-06-09 00:39:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 20:43:26 +00:00
|
|
|
// ListWorkers lists all active workers
|
2018-06-09 00:39:07 +00:00
|
|
|
func (c *Client) ListWorkers(ctx context.Context, opts ...ListWorkersOption) ([]*WorkerInfo, error) {
|
|
|
|
info := &ListWorkersInfo{}
|
|
|
|
for _, o := range opts {
|
2018-08-02 18:09:51 +00:00
|
|
|
o.SetListWorkersOption(info)
|
2018-06-09 00:39:07 +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-26 17:59:57 +00:00
|
|
|
ID: w.ID,
|
|
|
|
Labels: w.Labels,
|
2018-07-11 22:05:57 +00:00
|
|
|
Platforms: pb.ToSpecPlatforms(w.Platforms),
|
2018-09-06 18:50:41 +00:00
|
|
|
GCPolicy: fromAPIGCPolicy(w.GCPolicy),
|
2018-06-09 00:39:07 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return wi, nil
|
|
|
|
}
|
|
|
|
|
2018-09-21 20:43:26 +00:00
|
|
|
// ListWorkersOption is an option for a worker list query
|
2018-08-02 18:09:51 +00:00
|
|
|
type ListWorkersOption interface {
|
|
|
|
SetListWorkersOption(*ListWorkersInfo)
|
|
|
|
}
|
2018-06-09 00:39:07 +00:00
|
|
|
|
2018-09-21 20:43:26 +00:00
|
|
|
// ListWorkersInfo is a payload for worker list query
|
2018-06-09 00:39:07 +00:00
|
|
|
type ListWorkersInfo struct {
|
|
|
|
Filter []string
|
|
|
|
}
|
2018-09-06 18:50:41 +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
|
|
|
|
}
|