-
Notifications
You must be signed in to change notification settings - Fork 64
/
main.go
79 lines (59 loc) · 1.62 KB
/
main.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
// 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 main
import (
"context"
"errors"
"fmt"
"net"
"os"
"os/signal"
"syscall"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/siderolabs/sidero/app/sidero-controller-manager/internal/siderolink"
"github.com/siderolabs/siderolink/pkg/logreceiver"
)
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
os.Exit(1)
}
}
func run() error {
logger, err := zap.NewProduction()
if err != nil {
return fmt.Errorf("failed to initialize logger: %w", err)
}
zap.ReplaceGlobals(logger)
zap.RedirectStdLog(logger)
metalclient, kubeconfig, err := getMetalClient()
if err != nil {
return fmt.Errorf("error building runtime client: %w", err)
}
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
eg, ctx := errgroup.WithContext(ctx)
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", siderolink.LogReceiverPort))
if err != nil {
return fmt.Errorf("error listening for log endpoint: %w", err)
}
annotator := siderolink.NewAnnotator(metalclient, kubeconfig, logger)
srv := logreceiver.NewServer(logger, listener, logHandler(logger, annotator))
eg.Go(func() error {
return annotator.Run(ctx)
})
eg.Go(func() error {
return srv.Serve()
})
eg.Go(func() error {
<-ctx.Done()
srv.Stop()
return nil
})
if err := eg.Wait(); err != nil && !errors.Is(err, context.Canceled) {
return err
}
return nil
}