-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
147 lines (126 loc) · 3.83 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
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
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/krateoplatformops/eventrouter/internal/env"
httputil "github.com/krateoplatformops/eventrouter/internal/helpers/http"
"github.com/krateoplatformops/eventrouter/internal/helpers/queue"
"github.com/krateoplatformops/eventrouter/internal/router"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
)
const (
serviceName = "EventRouter"
)
var (
Version string
Build string
)
func main() {
// Flags
kubeconfig := flag.String(clientcmd.RecommendedConfigPathFlag, "", "absolute path to the kubeconfig file")
debug := flag.Bool("debug",
env.Bool("EVENT_ROUTER_DEBUG", false), "dump verbose output")
insecure := flag.Bool("insecure", env.Bool("EVENT_ROUTER_INSECURE", false),
"allow insecure server connections when using SSL")
resyncInterval := flag.Duration("resync-interval",
env.Duration("EVENT_ROUTER_RESYNC_INTERVAL", time.Minute*3), "resync interval")
throttlePeriod := flag.Duration("throttle-period",
env.Duration("EVENT_ROUTER_THROTTLE_PERIOD", 0), "throttle period")
namespace := flag.String("namespace",
env.String("EVENT_ROUTER_NAMESPACE", ""), "namespace to list and watch")
queueMaxCapacity := flag.Int("queue-max-capacity",
env.Int("EVENT_ROUTER_QUEUE_MAX_CAPACITY", 10), "notification queue buffer size")
queueWorkerThreads := flag.Int("queue-worker-threads",
env.Int("EVENT_ROUTER_QUEUE_WORKER_THREADS", 50), "number of worker threads in the notification queue")
flag.Usage = func() {
fmt.Fprintln(flag.CommandLine.Output(), "Flags:")
flag.PrintDefaults()
}
klog.InitFlags(nil)
flag.Parse()
// Kubernetes configuration
var cfg *rest.Config
var err error
if len(*kubeconfig) > 0 {
cfg, err = clientcmd.BuildConfigFromFlags("", *kubeconfig)
} else {
cfg, err = rest.InClusterConfig()
}
if err != nil {
klog.Fatalf("unable to init kubeconfig: %s", err.Error())
}
if klog.V(4).Enabled() {
cfg.WrapTransport = func(rt http.RoundTripper) http.RoundTripper {
return &httputil.Tracer{RoundTripper: rt}
}
}
// creates the clientset from kubeconfig
clientSet, err := kubernetes.NewForConfig(cfg)
if err != nil {
klog.Fatalf("unable to create kubernetes clientset: %s", err.Error())
}
// setup notification worker queue
q := queue.NewQueue(*queueMaxCapacity, *queueWorkerThreads)
q.Run()
defer q.Terminate()
handler, err := router.NewPusher(router.PusherOpts{
RESTConfig: cfg,
Queue: q,
Verbose: *debug,
Insecure: *insecure,
})
if err != nil {
klog.Fatalf("unable to create the event notifier: %s", err.Error())
}
eventRouter := router.NewEventRouter(router.EventRouterOpts{
RESTClient: clientSet.CoreV1().RESTClient(),
Handler: handler,
Namespace: *namespace,
ThrottlePeriod: *throttlePeriod,
})
stop := sigHandler()
// Startup the EventRouter
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
klog.InfoS(fmt.Sprintf("Starting %s", serviceName),
"debug", *debug,
"resyncInterval", *resyncInterval,
"throttlePeriod", *throttlePeriod,
"namespace", *namespace,
"queueMaxCapacity", *queueMaxCapacity,
"queueWorkerThreads", *queueWorkerThreads)
eventRouter.Run(stop)
}()
wg.Wait()
klog.Infof("%s done", serviceName)
os.Exit(1)
}
// setup a signal hander to gracefully exit
func sigHandler() <-chan struct{} {
stop := make(chan struct{})
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c,
syscall.SIGINT, // Ctrl+C
syscall.SIGTERM, // Termination Request
syscall.SIGSEGV, // FullDerp
syscall.SIGABRT, // Abnormal termination
syscall.SIGILL, // illegal instruction
syscall.SIGFPE) // floating point - this is why we can't have nice things
sig := <-c
klog.Infof("Signal (%v) detected, shutting down", sig)
close(stop)
}()
return stop
}