-
Notifications
You must be signed in to change notification settings - Fork 6
/
log_receiver.go
52 lines (38 loc) · 1.17 KB
/
log_receiver.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
// 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 agent
import (
"context"
"errors"
"fmt"
"net"
"net/netip"
"sync"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/siderolabs/siderolink/pkg/logreceiver"
)
func logHandler(logger *zap.Logger) logreceiver.Handler {
return func(srcAddress netip.Addr, msg map[string]interface{}) {
logger.Info("kernel log message", zap.Stringer("src_address", srcAddress), zap.Any("msg", msg))
}
}
func logReceiver(ctx context.Context, endpoint string, eg *errgroup.Group, logger *zap.Logger) error {
lis, err := net.Listen("tcp", endpoint)
if err != nil {
return fmt.Errorf("error listening for TCP log receiver: %w", err)
}
srv := logreceiver.NewServer(logger, lis, logHandler(logger))
stopServer := sync.OnceFunc(srv.Stop)
eg.Go(func() error {
defer stopServer()
serveErr := srv.Serve()
if errors.Is(serveErr, net.ErrClosed) && ctx.Err() != nil {
return nil //nolint:nilerr
}
return serveErr
})
context.AfterFunc(ctx, stopServer)
return nil
}