-
Notifications
You must be signed in to change notification settings - Fork 53
/
plugin.go
104 lines (88 loc) · 2.98 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
package agent
import (
"context"
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/plugins/metrics/pkg/agent/drivers"
"github.com/rancher/opni/plugins/metrics/pkg/apis/node"
"go.uber.org/zap"
)
type Plugin struct {
ctx context.Context
logger *zap.SugaredLogger
httpServer *HttpServer
ruleStreamer *RuleStreamer
node *MetricsNode
stopRuleStreamer context.CancelFunc
}
func NewPlugin(ctx context.Context) *Plugin {
lg := logger.NewPluginLogger().Named("metrics")
ct := healthpkg.NewDefaultConditionTracker(lg)
p := &Plugin{
ctx: ctx,
logger: lg,
httpServer: NewHttpServer(ct, lg),
ruleStreamer: NewRuleStreamer(ct, lg),
node: NewMetricsNode(ct, lg),
}
if d, err := drivers.NewExternalPromOperatorDriver(lg.Named("external-operator")); err != nil {
lg.With(
"driver", d.Name(),
zap.Error(err),
).Info("node driver is unavailable")
drivers.LogNodeDriverFailure(d.Name(), err)
} else {
lg.With(
"driver", d.Name(),
).Info("node driver is available")
drivers.RegisterNodeDriver(d)
p.node.AddConfigListener(drivers.NewListenerFunc(ctx, d.ConfigureNode))
p.node.SetNodeDriver(d)
}
p.node.AddConfigListener(drivers.NewListenerFunc(ctx, p.onConfigUpdated))
return p
}
func (p *Plugin) onConfigUpdated(cfg *node.MetricsCapabilityConfig) {
p.logger.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
go p.ruleStreamer.Run(ctx, cfg.GetSpec().GetRules())
}
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")
}
}
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.NewPlugin(p))
scheme.Add(httpext.HTTPAPIExtensionPluginID, httpext.NewPlugin(p.httpServer))
return scheme
}