-
Notifications
You must be signed in to change notification settings - Fork 458
/
events_sink.go
208 lines (176 loc) · 5.47 KB
/
events_sink.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package runtime
import (
"context"
"fmt"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/rs/xid"
"github.com/siderolabs/gen/channel"
"github.com/siderolabs/gen/optional"
"github.com/siderolabs/siderolink/api/events"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/anypb"
networkutils "github.com/siderolabs/talos/internal/app/machined/pkg/controllers/network/utils"
machinedruntime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
)
// EventsSinkController watches events and forwards them to the events sink server
// if it's configured.
type EventsSinkController struct {
V1Alpha1Events machinedruntime.Watcher
Drainer *machinedruntime.Drainer
drainSub *machinedruntime.DrainSubscription
eventID xid.ID
}
// Name implements controller.Controller interface.
func (ctrl *EventsSinkController) Name() string {
return "v1alpha1.EventsSinkController"
}
// Inputs implements controller.Controller interface.
func (ctrl *EventsSinkController) Inputs() []controller.Input {
return nil
}
// Outputs implements controller.Controller interface.
func (ctrl *EventsSinkController) Outputs() []controller.Output {
return nil
}
// Run implements controller.Controller interface.
//
//nolint:gocyclo,cyclop
func (ctrl *EventsSinkController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
if ctrl.drainSub == nil {
ctrl.drainSub = ctrl.Drainer.Subscribe()
}
defer func() {
if ctrl.drainSub != nil {
ctrl.drainSub.Cancel()
}
}()
if err := networkutils.WaitForNetworkReady(ctx, r,
func(status *network.StatusSpec) bool {
return status.AddressReady
},
[]controller.Input{
{
Namespace: runtime.NamespaceName,
Type: runtime.EventSinkConfigType,
ID: optional.Some(runtime.EventSinkConfigID),
Kind: controller.InputWeak,
},
},
); err != nil {
return fmt.Errorf("error waiting for network: %w", err)
}
var (
conn *grpc.ClientConn
client events.EventSinkServiceClient
watchCh, consumeWatchCh chan machinedruntime.EventInfo
backlog int
draining bool
)
defer func() {
if conn != nil {
conn.Close() //nolint:errcheck
}
}()
for {
select {
case <-ctx.Done():
return nil
case <-ctrl.drainSub.EventCh():
// drain started, return immediately if there's no backlog
draining = true
if backlog == 0 {
return nil
}
case event := <-consumeWatchCh:
// if consumeWatchCh is not nil, client connection was established
backlog = event.Backlog
data, err := anypb.New(event.Payload)
if err != nil {
return err
}
req := &events.EventRequest{
Id: event.ID.String(),
Data: data,
ActorId: event.ActorID,
}
_, err = client.Publish(ctx, req)
if err != nil {
return fmt.Errorf("error publishing event: %w", err)
}
// adjust last consumed event
ctrl.eventID = event.ID
// if draining and backlog is 0, return immediately
if draining && backlog == 0 {
return nil
}
case <-r.EventCh():
// configuration changed, re-establish connection
cfg, err := safe.ReaderGetByID[*runtime.EventSinkConfig](ctx, r, runtime.EventSinkConfigID)
if err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("error getting event sink config: %w", err)
}
if conn != nil {
logger.Debug("closing connection to event sink")
conn.Close() //nolint:errcheck
conn = nil
client = nil
consumeWatchCh = nil // stop consuming events
backlog = 0
}
if cfg == nil {
// no config, no event streaming
continue
}
// establish connection
logger.Debug("establishing connection to event sink", zap.String("endpoint", cfg.TypedSpec().Endpoint))
conn, err = grpc.Dial(
cfg.TypedSpec().Endpoint,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithSharedWriteBuffer(true),
)
if err != nil {
return fmt.Errorf("error establishing connection to event sink: %w", err)
}
client = events.NewEventSinkServiceClient(conn)
// start watching events if we haven't already done so
//
// watch is only established with the first live connection to make sure we don't miss any events
if watchCh == nil {
watchCh = make(chan machinedruntime.EventInfo)
opts := []machinedruntime.WatchOptionFunc{}
if ctrl.eventID.IsNil() {
opts = append(opts, machinedruntime.WithTailEvents(-1))
} else {
opts = append(opts, machinedruntime.WithTailID(ctrl.eventID))
}
// Watch returns immediately, setting up a goroutine which will copy events to `watchCh`
if err = ctrl.V1Alpha1Events.Watch(func(eventCh <-chan machinedruntime.EventInfo) {
for {
select {
case <-ctx.Done():
return
case event := <-eventCh:
if !channel.SendWithContext(ctx, watchCh, event) {
return
}
}
}
}, opts...); err != nil {
return err
}
}
consumeWatchCh = watchCh
}
}
}