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