-
Notifications
You must be signed in to change notification settings - Fork 53
/
cluster_driver.go
95 lines (78 loc) · 2.28 KB
/
cluster_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
88
89
90
91
92
93
94
95
package drivers
import (
"context"
"errors"
"fmt"
"sync"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/plugins/topology/pkg/apis/orchestrator"
"google.golang.org/protobuf/types/known/emptypb"
)
type ClusterDriver interface {
orchestrator.TopologyOrchestratorServer
// Unique name of the driver
Name() string
// ShouldDisableNode is called during node sync for nodes which otherwise
// have this capability enabled. If this function returns an error, the
// node will be set to disabled instead, and the error will be logged.
ShouldDisableNode(*corev1.Reference) error
}
var (
lock = &sync.Mutex{}
clusterDrivers map[string]ClusterDriver
persistentDrivers []func() ClusterDriver
failedClusterDrivers map[string]string
)
func init() {
ResetClusterDrivers()
}
func RegisterClusterDriver(driver ClusterDriver) {
lock.Lock()
defer lock.Unlock()
if _, ok := clusterDrivers[driver.Name()]; ok {
panic("driver already exists: " + driver.Name())
}
clusterDrivers[driver.Name()] = driver
}
func ResetClusterDrivers() {
lock.Lock()
clusterDrivers = make(map[string]ClusterDriver)
failedClusterDrivers = make(map[string]string)
lock.Unlock()
for _, driverFunc := range persistentDrivers {
RegisterClusterDriver(driverFunc())
}
}
func GetClusterDriver(name string) (ClusterDriver, error) {
lock.Lock()
defer lock.Unlock()
driver, ok := clusterDrivers[name]
if !ok {
if failureMsg, ok := failedClusterDrivers[name]; ok {
return nil, errors.New(failureMsg)
}
return nil, fmt.Errorf("driver not found")
}
return driver, nil
}
func LogClusterDriverFailure(name string, err error) {
lock.Lock()
defer lock.Unlock()
failedClusterDrivers[name] = err.Error()
}
type NoopClusterDriver struct {
orchestrator.UnsafeTopologyOrchestratorServer
}
var _ orchestrator.TopologyOrchestratorServer = &NoopClusterDriver{}
func (d *NoopClusterDriver) Name() string {
return "noop"
}
func (d *NoopClusterDriver) ShouldDisableNode(*corev1.Reference) error {
// the noop driver will never forcefully disable a node
return nil
}
func (d *NoopClusterDriver) GetClusterStatus(_ context.Context, _ *emptypb.Empty) (*orchestrator.InstallStatus, error) {
return &orchestrator.InstallStatus{
State: orchestrator.InstallState_Installed,
}, nil
}