-
Notifications
You must be signed in to change notification settings - Fork 52
/
plugin.go
122 lines (105 loc) · 3.37 KB
/
plugin.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
package agent
import (
"context"
"fmt"
"log/slog"
healthpkg "github.com/rancher/opni/pkg/health"
"github.com/rancher/opni/pkg/logger"
httpext "github.com/rancher/opni/pkg/plugins/apis/apiextensions/http"
"github.com/rancher/opni/pkg/plugins/apis/apiextensions/stream"
"github.com/rancher/opni/pkg/plugins/apis/capability"
"github.com/rancher/opni/pkg/plugins/apis/health"
"github.com/rancher/opni/pkg/plugins/meta"
"github.com/rancher/opni/pkg/rules"
"github.com/rancher/opni/pkg/util/notifier"
"github.com/rancher/opni/plugins/metrics/apis/node"
"github.com/rancher/opni/plugins/metrics/pkg/agent/drivers"
)
type Plugin struct {
ctx context.Context
logger *slog.Logger
httpServer *HttpServer
ruleStreamer *RuleStreamer
node *MetricsNode
stopRuleStreamer context.CancelFunc
}
func NewPlugin(ctx context.Context) *Plugin {
lg := logger.NewPluginLogger().WithGroup("metrics")
ct := healthpkg.NewDefaultConditionTracker(lg)
p := &Plugin{
ctx: ctx,
logger: lg,
httpServer: NewHttpServer(ct, lg),
ruleStreamer: NewRuleStreamer(ct, lg),
node: NewMetricsNode(ct, lg),
}
for _, name := range drivers.NodeDrivers.List() {
builder, ok := drivers.NodeDrivers.Get(name)
if !ok {
continue
}
driver, err := builder(ctx)
if err != nil {
lg.With(
"driver", name,
logger.Err(err),
).Warn("failed to initialize node driver")
continue
}
p.node.AddConfigListener(driver)
p.node.AddNodeDriver(driver)
}
p.node.AddConfigListener(p)
return p
}
func (p *Plugin) ConfigureNode(nodeId string, cfg *node.MetricsCapabilityConfig) error {
lg := p.logger.With("nodeId", nodeId)
lg.Debug("metrics capability config updated")
// at this point, we know the config has been updated
currentlyRunning := (p.stopRuleStreamer != nil)
shouldRun := cfg.GetEnabled()
startRuleStreamer := func() {
ctx, ca := context.WithCancel(p.ctx)
p.stopRuleStreamer = ca
finders := []notifier.Finder[rules.RuleGroup]{}
for name, driver := range p.node.nodeDrivers {
if !cfg.Spec.RuleDiscoveryEnabled() {
continue
}
if f := driver.ConfigureRuleGroupFinder(cfg.Spec.Rules); f != nil {
lg.Info(fmt.Sprintf("prometheus rule finder configured for driver %d", name))
finders = append(finders, f)
}
}
go p.ruleStreamer.Run(ctx, cfg.GetSpec().GetRules(), notifier.NewMultiFinder(finders...))
}
switch {
case currentlyRunning && shouldRun:
p.logger.Debug("reconfiguring rule sync")
p.stopRuleStreamer()
startRuleStreamer()
case currentlyRunning && !shouldRun:
p.logger.Debug("stopping rule sync")
p.stopRuleStreamer()
p.stopRuleStreamer = nil
p.logger.Debug("disabling http server")
p.httpServer.SetEnabled(false)
case !currentlyRunning && shouldRun:
p.logger.Debug("starting rule sync")
startRuleStreamer()
p.logger.Debug("enabling http server")
p.httpServer.SetEnabled(true)
case !currentlyRunning && !shouldRun:
p.logger.Debug("rule sync is disabled")
}
return nil
}
func Scheme(ctx context.Context) meta.Scheme {
scheme := meta.NewScheme(meta.WithMode(meta.ModeAgent))
p := NewPlugin(ctx)
scheme.Add(capability.CapabilityBackendPluginID, capability.NewAgentPlugin(p.node))
scheme.Add(health.HealthPluginID, health.NewPlugin(p.node))
scheme.Add(stream.StreamAPIExtensionPluginID, stream.NewAgentPlugin(p))
scheme.Add(httpext.HTTPAPIExtensionPluginID, httpext.NewPlugin(p.httpServer))
return scheme
}