-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
146 lines (124 loc) · 2.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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"net/http"
_ "net/http/pprof"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/paralus/relay/pkg/agent"
"github.com/paralus/relay/pkg/relay"
"github.com/paralus/relay/pkg/relaylogger"
"github.com/paralus/relay/pkg/tail"
"github.com/paralus/relay/pkg/utils"
)
var (
log *relaylogger.RelayLog
)
func exit(cancel context.CancelFunc) {
cancel()
os.Exit(0)
}
func terminate(cancel context.CancelFunc) {
cancel()
os.Exit(1)
}
// os signal handler
func signalHandler(sig os.Signal, cancel context.CancelFunc) {
if sig == syscall.SIGINT || sig == syscall.SIGKILL || sig == syscall.SIGTERM || sig == syscall.SIGQUIT {
log.Error(
nil,
"Received",
"signal", sig,
)
exit(cancel)
return
}
log.Info(
"Received",
"signal", sig,
)
}
func main() {
flag.String("mode", "server", "mode of relay, accepeted values server/client")
flag.Int("log-level", 1, "log leve, accepted values 0-3")
flag.Bool("profile", false, "enable profiling")
flag.Int("profile-port", 13000, "profiler http port")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
mode := viper.GetString("mode")
logLevel := viper.GetInt("log-level")
profile := viper.GetBool("profile")
profilePort := viper.GetInt("profile-port")
utils.GenUUID()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log = relaylogger.NewLogger(logLevel).WithName("Relay")
signalChan := make(chan os.Signal, 2)
signal.Notify(signalChan,
os.Interrupt,
syscall.SIGINT,
syscall.SIGHUP,
syscall.SIGKILL,
syscall.SIGTERM,
syscall.SIGQUIT,
)
log.Info(
"Relay",
"mode", mode,
"loglevel", logLevel,
)
utils.LogLevel = logLevel
if profile {
go func() {
log.Info("profiling enabled", "profilePort", profilePort)
err := http.ListenAndServe(fmt.Sprintf(":%d", profilePort), nil)
if err != nil {
panic(err)
}
}()
}
restart:
switch mode {
case "client":
log.Info("Starting relay agent client")
utils.Mode = utils.RELAYAGENT
go agent.RunRelayKubeCTLAgent(ctx, logLevel)
case "cdclient":
log.Info("Starting relay agent client")
utils.Mode = utils.CDRELAYAGENT
go agent.RunRelayCDAgent(ctx, logLevel)
case "tail":
log.Info("Starting relay log tail")
go tail.RunRelayTail(ctx)
case "cdrelay":
utils.Mode = utils.CDRELAY
log.Info("Starting cdrelay server")
go relay.RunCDRelayServer(ctx, logLevel)
default:
// default server
utils.Mode = utils.RELAY
log.Info("Starting relay server")
go relay.RunRelayServer(ctx, logLevel)
}
for {
select {
case <-utils.ExitChan:
log.Info(
"got exit",
)
time.Sleep(time.Second * 5)
goto restart
case <-utils.TerminateChan:
terminate(cancel)
case sig := <-signalChan:
signalHandler(sig, cancel)
}
}
}