forked from moby/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkercontroller.go
77 lines (68 loc) · 1.63 KB
/
workercontroller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package worker
import (
"sync"
"github.com/containerd/containerd/filters"
"github.com/moby/buildkit/client"
"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
workers sync.Map
defaultID string
}
// Add adds a local worker
func (c *Controller) Add(w Worker) error {
c.workers.Store(w.ID(), w)
if c.defaultID == "" {
c.defaultID = w.ID()
}
return nil
}
// 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
c.workers.Range(func(k, v interface{}) bool {
w := v.(Worker)
if filter.Match(adaptWorker(w)) {
workers = append(workers, w)
}
return true
})
return workers, nil
}
// GetDefault returns the default local worker
func (c *Controller) GetDefault() (Worker, error) {
if c.defaultID == "" {
return nil, errors.Errorf("no default worker")
}
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)
}
return v.(Worker), nil
}
// TODO: add Get(Constraint) (*Worker, error)
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(),
Platforms: w.Platforms(true),
})
}
return out
}