2017-11-21 08:08:36 +00:00
|
|
|
package worker
|
|
|
|
|
|
|
|
import (
|
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
|
2021-04-09 07:03:46 +00:00
|
|
|
workers []Worker
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 07:03:46 +00:00
|
|
|
// Add adds a local worker.
|
|
|
|
// The first worker becomes the default.
|
|
|
|
//
|
|
|
|
// Add is not thread-safe.
|
2017-12-15 08:06:54 +00:00
|
|
|
func (c *Controller) Add(w Worker) error {
|
2021-04-09 07:03:46 +00:00
|
|
|
c.workers = append(c.workers, w)
|
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
|
2021-04-09 07:03:46 +00:00
|
|
|
for _, w := range c.workers {
|
2017-12-19 09:34:34 +00:00
|
|
|
if filter.Match(adaptWorker(w)) {
|
|
|
|
workers = append(workers, w)
|
|
|
|
}
|
2021-04-09 07:03:46 +00:00
|
|
|
}
|
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) {
|
2021-04-09 07:03:46 +00:00
|
|
|
if len(c.workers) == 0 {
|
2018-04-13 21:13:48 +00:00
|
|
|
return nil, errors.Errorf("no default worker")
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
2021-04-09 07:03:46 +00:00
|
|
|
return c.workers[0], nil
|
2018-04-13 21:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) Get(id string) (Worker, error) {
|
2021-04-09 07:03:46 +00:00
|
|
|
for _, w := range c.workers {
|
|
|
|
if w.ID() == id {
|
|
|
|
return w, nil
|
|
|
|
}
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
2021-04-09 07:03:46 +00:00
|
|
|
return nil, errors.Errorf("worker %s not found", id)
|
2017-11-21 08:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add Get(Constraint) (*Worker, error)
|
2018-06-30 01:35:39 +00:00
|
|
|
|
2021-04-09 07:03:46 +00:00
|
|
|
// WorkerInfos returns slice of WorkerInfo.
|
|
|
|
// The first item is the default worker.
|
2018-06-30 01:35:39 +00:00
|
|
|
func (c *Controller) WorkerInfos() []client.WorkerInfo {
|
2021-04-09 07:03:46 +00:00
|
|
|
out := make([]client.WorkerInfo, 0, len(c.workers))
|
|
|
|
for _, w := range c.workers {
|
2018-06-30 01:35:39 +00:00
|
|
|
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
|
|
|
|
}
|