-
Notifications
You must be signed in to change notification settings - Fork 458
/
kmsg_log.go
274 lines (225 loc) · 6.92 KB
/
kmsg_log.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// 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"
"errors"
"fmt"
"net/url"
"time"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/optional"
"github.com/siderolabs/gen/xslices"
"github.com/siderolabs/go-kmsg"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
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/internal/app/machined/pkg/runtime/logging"
"github.com/siderolabs/talos/pkg/machinery/config/config"
"github.com/siderolabs/talos/pkg/machinery/constants"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
)
const (
drainTimeout = 100 * time.Millisecond
logSendTimeout = 5 * time.Second
logRetryTimeout = 1 * time.Second
logCloseTimeout = 5 * time.Second
)
// KmsgLogDeliveryController watches events and forwards them to the events sink server
// if it's configured.
type KmsgLogDeliveryController struct {
Drainer *machinedruntime.Drainer
drainSub *machinedruntime.DrainSubscription
}
// Name implements controller.Controller interface.
func (ctrl *KmsgLogDeliveryController) Name() string {
return "runtime.KmsgLogDeliveryController"
}
// Inputs implements controller.Controller interface.
func (ctrl *KmsgLogDeliveryController) Inputs() []controller.Input {
return nil
}
// Outputs implements controller.Controller interface.
func (ctrl *KmsgLogDeliveryController) Outputs() []controller.Output {
return nil
}
// Run implements controller.Controller interface.
func (ctrl *KmsgLogDeliveryController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
if err := networkutils.WaitForNetworkReady(ctx, r,
func(status *network.StatusSpec) bool {
return status.AddressReady
},
[]controller.Input{
{
Namespace: runtime.NamespaceName,
Type: runtime.KmsgLogConfigType,
ID: optional.Some(runtime.KmsgLogConfigID),
Kind: controller.InputWeak,
},
},
); err != nil {
return fmt.Errorf("error waiting for network: %w", err)
}
// initilalize kmsg reader early, so that we don't lose position on config changes
reader, err := kmsg.NewReader(kmsg.Follow())
if err != nil {
return fmt.Errorf("error reading kernel messages: %w", err)
}
defer reader.Close() //nolint:errcheck
kmsgCh := reader.Scan(ctx)
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
}
cfg, err := safe.ReaderGetByID[*runtime.KmsgLogConfig](ctx, r, runtime.KmsgLogConfigID)
if err != nil && !state.IsNotFoundError(err) {
return fmt.Errorf("error getting configuration: %w", err)
}
if cfg == nil {
// no config, wait for the next event
continue
}
if err = ctrl.deliverLogs(ctx, r, logger, kmsgCh, cfg.TypedSpec().Destinations); err != nil {
return fmt.Errorf("error delivering logs: %w", err)
}
r.ResetRestartBackoff()
}
}
type logConfig struct {
endpoint *url.URL
}
func (c logConfig) Format() string {
return constants.LoggingFormatJSONLines
}
func (c logConfig) Endpoint() *url.URL {
return c.endpoint
}
func (c logConfig) ExtraTags() map[string]string {
return nil
}
//nolint:gocyclo
func (ctrl *KmsgLogDeliveryController) deliverLogs(ctx context.Context, r controller.Runtime, logger *zap.Logger, kmsgCh <-chan kmsg.Packet, destURLs []*url.URL) error {
if ctrl.drainSub == nil {
ctrl.drainSub = ctrl.Drainer.Subscribe()
}
// initialize all log senders
destLogConfigs := xslices.Map(destURLs, func(u *url.URL) config.LoggingDestination {
return logConfig{endpoint: u}
})
senders := xslices.Map(destLogConfigs, logging.NewJSONLines)
defer func() {
closeCtx, closeCtxCancel := context.WithTimeout(context.Background(), logCloseTimeout)
defer closeCtxCancel()
for _, sender := range senders {
if err := sender.Close(closeCtx); err != nil {
logger.Error("error closing log sender", zap.Error(err))
}
}
}()
var (
drainTimer *time.Timer
drainTimerCh <-chan time.Time
)
for {
var msg kmsg.Packet
select {
case <-ctx.Done():
ctrl.drainSub.Cancel()
return nil
case <-r.EventCh():
// config changed, restart the loop
return nil
case <-ctrl.drainSub.EventCh():
// drain started, assume that ksmg is drained if there're no new messages in drainTimeout
drainTimer = time.NewTimer(drainTimeout)
drainTimerCh = drainTimer.C
continue
case <-drainTimerCh:
ctrl.drainSub.Cancel()
return nil
case msg = <-kmsgCh:
if drainTimer != nil {
// if draining, reset the timer as there's a new message
if !drainTimer.Stop() {
<-drainTimer.C
}
drainTimer.Reset(drainTimeout)
}
}
if msg.Err != nil {
return fmt.Errorf("error receiving kernel logs: %w", msg.Err)
}
event := machinedruntime.LogEvent{
Msg: msg.Message.Message,
Time: msg.Message.Timestamp,
Level: kmsgPriorityToLevel(msg.Message.Priority),
Fields: map[string]interface{}{
"facility": msg.Message.Facility.String(),
"seq": msg.Message.SequenceNumber,
"clock": msg.Message.Clock,
"priority": msg.Message.Priority.String(),
},
}
if err := ctrl.resend(ctx, r, logger, senders, &event); err != nil {
return fmt.Errorf("error sending log event: %w", err)
}
}
}
//nolint:gocyclo
func (ctrl *KmsgLogDeliveryController) resend(ctx context.Context, r controller.Runtime, logger *zap.Logger, senders []machinedruntime.LogSender, e *machinedruntime.LogEvent) error {
for {
sendCtx, sendCancel := context.WithTimeout(ctx, logSendTimeout)
sendErrors := make(chan error, len(senders))
for _, sender := range senders {
go func() {
sendErrors <- sender.Send(sendCtx, e)
}()
}
var dontRetry bool
for range senders {
err := <-sendErrors
// don't retry if at least one sender succeed to avoid implementing per-sender queue, etc
if err == nil {
dontRetry = true
continue
}
logger.Debug("error sending log event", zap.Error(err))
if errors.Is(err, machinedruntime.ErrDontRetry) || errors.Is(err, context.Canceled) {
dontRetry = true
}
}
sendCancel()
if dontRetry {
return nil
}
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
// config changed, restart the loop
return errors.New("config changed")
case <-time.After(logRetryTimeout):
}
}
}
func kmsgPriorityToLevel(pri kmsg.Priority) zapcore.Level {
switch pri {
case kmsg.Alert, kmsg.Crit, kmsg.Emerg, kmsg.Err:
return zapcore.ErrorLevel
case kmsg.Debug:
return zapcore.DebugLevel
case kmsg.Info, kmsg.Notice:
return zapcore.InfoLevel
case kmsg.Warning:
return zapcore.WarnLevel
default:
return zapcore.ErrorLevel
}
}