-
Notifications
You must be signed in to change notification settings - Fork 52
/
node.go
154 lines (125 loc) · 3.78 KB
/
node.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
package agent
import (
"context"
"fmt"
"strings"
"sync"
"slices"
capabilityv1 "github.com/rancher/opni/pkg/apis/capability/v1"
controlv1 "github.com/rancher/opni/pkg/apis/control/v1"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
"github.com/rancher/opni/pkg/capabilities/wellknown"
"github.com/rancher/opni/pkg/health"
"github.com/rancher/opni/pkg/util"
"github.com/rancher/opni/plugins/topology/apis/node"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
"log/slog"
)
type TopologyNode struct {
capabilityv1.UnsafeNodeServer
controlv1.UnsafeHealthServer
logger *slog.Logger
clientMu sync.RWMutex
client node.NodeTopologyCapabilityClient
configMu sync.RWMutex
config *node.TopologyCapabilityConfig
listeners []chan<- *node.TopologyCapabilityConfig
conditions health.ConditionTracker
}
var _ capabilityv1.NodeServer = &TopologyNode{}
var _ controlv1.HealthServer = &TopologyNode{}
func NewTopologyNode(ct health.ConditionTracker, lg *slog.Logger) *TopologyNode {
return &TopologyNode{
logger: lg,
conditions: ct,
config: &node.TopologyCapabilityConfig{
Enabled: false,
},
}
}
func (t *TopologyNode) SetClient(client node.NodeTopologyCapabilityClient) {
t.clientMu.Lock()
defer t.clientMu.Unlock()
t.client = client
go t.doSync(context.Background())
}
func (t *TopologyNode) AddConfigListener(ch chan<- *node.TopologyCapabilityConfig) {
t.listeners = append(t.listeners, ch)
}
func (t *TopologyNode) doSync(ctx context.Context) {
t.logger.Debug("syncing topology node")
t.clientMu.RLock()
defer t.clientMu.RUnlock()
if t.client == nil {
t.conditions.Set(health.CondConfigSync, health.StatusPending, "no client. skipping sync")
return
}
t.configMu.RLock()
syncResp, err := t.client.Sync(ctx, &node.SyncRequest{
CurrentConfig: util.ProtoClone(t.config),
})
t.configMu.RUnlock()
if err != nil {
err := fmt.Errorf("error syncing topology node: %w", err)
t.conditions.Set(health.CondConfigSync, health.StatusFailure, err.Error())
return
}
switch syncResp.ConfigStatus {
case node.ConfigStatus_UpToDate:
t.logger.Info("topology node is up to date")
case node.ConfigStatus_NeedsUpdate:
t.logger.Info("topology node needs update")
t.updateConfig(syncResp.UpdatedConfig)
}
t.conditions.Clear(health.CondConfigSync)
}
// Implements capabilityv1.NodeServer
func (t *TopologyNode) SyncNow(_ context.Context, req *capabilityv1.Filter) (*emptypb.Empty, error) {
if len(req.GetCapabilityNames()) > 0 {
if !slices.Contains(req.CapabilityNames, wellknown.CapabilityTopology) {
t.logger.Debug("ignoring sync request due to capability filter")
return &emptypb.Empty{}, nil
}
}
t.logger.Debug("received sync request")
t.clientMu.RLock()
defer t.clientMu.RUnlock()
if t.client == nil {
return nil, status.Error(codes.Unavailable, "not connected to node server")
}
defer func() {
go t.doSync(context.Background())
}()
return &emptypb.Empty{}, nil
}
// Implement controlv1.HealthServer
func (t *TopologyNode) GetHealth(_ context.Context, _ *emptypb.Empty) (*corev1.Health, error) {
t.configMu.RLock()
defer t.configMu.RUnlock()
conditions := t.conditions.List()
if t.config != nil {
if !t.config.Enabled && len(t.config.Conditions) > 0 {
conditions = append(conditions, fmt.Sprintf("Disabled: %s", strings.Join(t.config.Conditions, ", ")))
}
}
return &corev1.Health{
Ready: len(conditions) == 0,
Conditions: conditions,
}, nil
}
func (t *TopologyNode) updateConfig(config *node.TopologyCapabilityConfig) {
t.configMu.Lock()
defer t.configMu.Unlock()
t.config = config
for _, ch := range t.listeners {
clone := util.ProtoClone(config)
select {
case ch <- clone:
default:
t.logger.Warn("slow config update listener detected")
ch <- clone
}
}
}