-
Notifications
You must be signed in to change notification settings - Fork 53
/
node_driver.go
87 lines (71 loc) · 1.62 KB
/
node_driver.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
78
79
80
81
82
83
84
85
86
87
package drivers
import (
"context"
"errors"
"fmt"
"sync"
"github.com/rancher/opni/plugins/metrics/pkg/apis/node"
"github.com/samber/lo"
)
type MetricsNodeDriver interface {
Name() string
ConfigureNode(*node.MetricsCapabilityConfig)
}
var (
lock = &sync.Mutex{}
nodeDrivers map[string]MetricsNodeDriver
failedNodeDrivers map[string]string
)
func init() {
ResetNodeDrivers()
}
func RegisterNodeDriver(driver MetricsNodeDriver) {
lock.Lock()
defer lock.Unlock()
if _, ok := nodeDrivers[driver.Name()]; ok {
panic("driver already exists: " + driver.Name())
}
nodeDrivers[driver.Name()] = driver
}
func LogNodeDriverFailure(name string, err error) {
lock.Lock()
defer lock.Unlock()
failedNodeDrivers[name] = err.Error()
}
func GetNodeDriver(name string) (MetricsNodeDriver, error) {
lock.Lock()
defer lock.Unlock()
driver, ok := nodeDrivers[name]
if !ok {
if failureMsg, ok := failedNodeDrivers[name]; ok {
return nil, errors.New(failureMsg)
}
return nil, fmt.Errorf("driver not found")
}
return driver, nil
}
func ListNodeDrivers() []string {
lock.Lock()
defer lock.Unlock()
return lo.Keys(nodeDrivers)
}
func ResetNodeDrivers() {
lock.Lock()
defer lock.Unlock()
nodeDrivers = make(map[string]MetricsNodeDriver)
failedNodeDrivers = make(map[string]string)
}
func NewListenerFunc(ctx context.Context, fn func(cfg *node.MetricsCapabilityConfig)) chan<- *node.MetricsCapabilityConfig {
listenerC := make(chan *node.MetricsCapabilityConfig, 1)
go func() {
for {
select {
case <-ctx.Done():
return
case cfg := <-listenerC:
fn(cfg)
}
}
}()
return listenerC
}