-
Notifications
You must be signed in to change notification settings - Fork 52
/
ops.go
185 lines (160 loc) · 5.52 KB
/
ops.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package ops
import (
"context"
"fmt"
"net"
"sync"
"time"
"github.com/rancher/opni/pkg/alerting/shared"
"github.com/rancher/opni/pkg/alerting/storage"
"github.com/rancher/opni/pkg/logger"
"github.com/rancher/opni/pkg/util/future"
"github.com/rancher/opni/plugins/alerting/pkg/alerting/drivers"
"github.com/rancher/opni/plugins/alerting/pkg/apis/alertops"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/emptypb"
)
// Manages all dynamic backend configurations
// that must interact with & modify the runtime cluster
type AlertingOpsNode struct {
alertops.UnsafeAlertingAdminServer
alertops.UnsafeConfigReconcilerServer
ctx context.Context
AlertingOpsNodeOptions
syncPusher chan *alertops.SyncRequest
syncMu *sync.RWMutex
clusterNotifier chan shared.AlertingClusterOptions
*shared.AlertingClusterOptions
ClusterDriver future.Future[drivers.ClusterDriver]
storageClientSet future.Future[storage.AlertingClientSet]
}
var _ alertops.AlertingAdminServer = (*AlertingOpsNode)(nil)
var _ alertops.ConfigReconcilerServer = (*AlertingOpsNode)(nil)
type AlertingOpsNodeOptions struct {
driverTimeout time.Duration
storageTimeout time.Duration
logger *zap.SugaredLogger
}
type AlertingOpsNodeOption func(*AlertingOpsNodeOptions)
func (a *AlertingOpsNodeOptions) apply(opts ...AlertingOpsNodeOption) {
for _, opt := range opts {
opt(a)
}
}
var _ alertops.AlertingAdminServer = (*AlertingOpsNode)(nil)
func NewAlertingOpsNode(
ctx context.Context,
clusterDriver future.Future[drivers.ClusterDriver],
storageClientSet future.Future[storage.AlertingClientSet],
opts ...AlertingOpsNodeOption) *AlertingOpsNode {
options := AlertingOpsNodeOptions{
driverTimeout: 1 * time.Second,
storageTimeout: 1 * time.Second,
}
options.apply(opts...)
if options.logger == nil {
options.logger = logger.NewPluginLogger().Named("alerting-ops")
}
a := &AlertingOpsNode{
ctx: ctx,
syncMu: &sync.RWMutex{},
AlertingOpsNodeOptions: options,
ClusterDriver: clusterDriver,
storageClientSet: storageClientSet,
syncPusher: make(chan *alertops.SyncRequest),
}
go a.runPeriodicSync(ctx)
return a
}
func (a *AlertingOpsNode) GetClusterConfiguration(ctx context.Context, _ *emptypb.Empty) (*alertops.ClusterConfiguration, error) {
ctxTimeout, cancel := context.WithTimeout(ctx, a.driverTimeout)
defer cancel()
driver, err := a.ClusterDriver.GetContext(ctxTimeout)
if err != nil {
return nil, err
}
return driver.GetClusterConfiguration(ctx, &emptypb.Empty{})
}
func (a *AlertingOpsNode) ConfigureCluster(ctx context.Context, conf *alertops.ClusterConfiguration) (*emptypb.Empty, error) {
ctxTimeout, cancel := context.WithTimeout(ctx, a.driverTimeout)
defer cancel()
driver, err := a.ClusterDriver.GetContext(ctxTimeout)
if err != nil {
return nil, err
}
return driver.ConfigureCluster(ctx, conf)
}
func (a *AlertingOpsNode) GetClusterStatus(ctx context.Context, _ *emptypb.Empty) (*alertops.InstallStatus, error) {
ctxTimeout, cancel := context.WithTimeout(ctx, a.driverTimeout)
defer cancel()
driver, err := a.ClusterDriver.GetContext(ctxTimeout)
if err != nil {
return nil, err
}
return driver.GetClusterStatus(ctx, &emptypb.Empty{})
}
func (a *AlertingOpsNode) UninstallCluster(ctx context.Context, req *alertops.UninstallRequest) (*emptypb.Empty, error) {
ctxTimeout, cancel := context.WithTimeout(ctx, a.driverTimeout)
defer cancel()
driver, err := a.ClusterDriver.GetContext(ctxTimeout)
if err != nil {
return nil, err
}
if req.DeleteData {
go func() {
err := a.storageClientSet.Get().Purge(context.Background())
if err != nil {
a.logger.Warnw("failed to purge data", zap.Error(err))
}
}()
}
return driver.UninstallCluster(ctx, req)
}
func (a *AlertingOpsNode) InstallCluster(ctx context.Context, _ *emptypb.Empty) (*emptypb.Empty, error) {
ctxTimeout, cancel := context.WithTimeout(ctx, a.driverTimeout)
defer cancel()
driver, err := a.ClusterDriver.GetContext(ctxTimeout)
if err != nil {
return nil, err
}
return driver.InstallCluster(ctx, &emptypb.Empty{})
}
func (a *AlertingOpsNode) GetRuntimeOptions(ctx context.Context) (shared.AlertingClusterOptions, error) {
ctxTimeout, cancel := context.WithTimeout(ctx, a.driverTimeout)
defer cancel()
driver, err := a.ClusterDriver.GetContext(ctxTimeout)
if err != nil {
return shared.AlertingClusterOptions{}, err
}
return driver.GetRuntimeOptions(), nil
}
func (a *AlertingOpsNode) GetAvailableEndpoint(ctx context.Context, options *shared.AlertingClusterOptions) (string, error) {
var availableEndpoint string
status, err := a.GetClusterConfiguration(ctx, &emptypb.Empty{})
if err != nil {
return "", err
}
if status.NumReplicas == 1 { // exactly one that is the controller
availableEndpoint = options.GetControllerEndpoint()
} else {
availableEndpoint = options.GetWorkerEndpoint()
}
return availableEndpoint, nil
}
func (a *AlertingOpsNode) GetAvailableCacheEndpoint(ctx context.Context, options *shared.AlertingClusterOptions) (string, error) {
var availableEndpoint string
status, err := a.GetClusterConfiguration(ctx, &emptypb.Empty{})
if err != nil {
return "", err
}
if status.NumReplicas == 1 { // exactly one that is the controller
availableEndpoint = options.GetControllerEndpoint()
} else {
availableEndpoint = options.GetWorkerEndpoint()
}
host, _, err := net.SplitHostPort(availableEndpoint)
if err != nil {
return fmt.Sprintf("%s:%d", availableEndpoint, options.OpniPort), nil
}
return fmt.Sprintf("%s:%d", host, options.OpniPort), nil
}