2017-11-21 08:08:36 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
2017-12-19 09:34:34 +00:00
|
|
|
"github.com/containerd/containerd/filters"
|
2018-06-30 01:35:39 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
2017-11-21 08:08:36 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Controller holds worker instances.
|
|
|
|
// Currently, only local workers are supported.
|
|
|
|
type Controller struct {
|
|
|
|
// TODO: define worker interface and support remote ones
|
2018-04-13 21:13:48 +00:00
|
|
|
workers sync.Map
|
|
|
|
defaultID string
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds a local worker
|
2017-12-15 08:06:54 +00:00
|
|
|
func (c *Controller) Add(w Worker) error {
|
2018-04-13 21:13:48 +00:00
|
|
|
c.workers.Store(w.ID(), w)
|
|
|
|
if c.defaultID == "" {
|
|
|
|
c.defaultID = w.ID()
|
|
|
|
}
|
2017-11-21 08:08:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:34:34 +00:00
|
|
|
// List lists workers
|
|
|
|
func (c *Controller) List(filterStrings ...string) ([]Worker, error) {
|
|
|
|
filter, err := filters.ParseAll(filterStrings...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var workers []Worker
|
2018-04-13 21:13:48 +00:00
|
|
|
c.workers.Range(func(k, v interface{}) bool {
|
|
|
|
w := v.(Worker)
|
2017-12-19 09:34:34 +00:00
|
|
|
if filter.Match(adaptWorker(w)) {
|
|
|
|
workers = append(workers, w)
|
|
|
|
}
|
2018-04-13 21:13:48 +00:00
|
|
|
return true
|
|
|
|
})
|
2017-12-19 09:34:34 +00:00
|
|
|
return workers, nil
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefault returns the default local worker
|
2017-12-15 08:06:54 +00:00
|
|
|
func (c *Controller) GetDefault() (Worker, error) {
|
2018-04-13 21:13:48 +00:00
|
|
|
if c.defaultID == "" {
|
|
|
|
return nil, errors.Errorf("no default worker")
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
2018-04-13 21:13:48 +00:00
|
|
|
return c.Get(c.defaultID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) Get(id string) (Worker, error) {
|
|
|
|
v, ok := c.workers.Load(id)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("worker %s not found", id)
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
2018-04-13 21:13:48 +00:00
|
|
|
return v.(Worker), nil
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add Get(Constraint) (*Worker, error)
|
2018-06-30 01:35:39 +00:00
|
|
|
|
|
|
|
func (c *Controller) WorkerInfos() []client.WorkerInfo {
|
|
|
|
workers, err := c.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
out := make([]client.WorkerInfo, 0, len(workers))
|
|
|
|
for _, w := range workers {
|
|
|
|
out = append(out, client.WorkerInfo{
|
|
|
|
ID: w.ID(),
|
|
|
|
Labels: w.Labels(),
|
2020-02-25 23:23:34 +00:00
|
|
|
Platforms: w.Platforms(true),
|
2018-06-30 01:35:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|