-
Notifications
You must be signed in to change notification settings - Fork 150
/
server.go
164 lines (141 loc) · 3.88 KB
/
server.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
package daemon
import (
"fmt"
"io/ioutil"
"net"
"net/http"
_ "net/http/pprof" //import pprof for diagnose
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"strings"
"syscall"
"github.com/AliyunContainerService/terway/pkg/metric"
"github.com/AliyunContainerService/terway/pkg/tracing"
"github.com/AliyunContainerService/terway/rpc"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
// stackTriger print golang stack trace to log
func stackTriger() {
sigchain := make(chan os.Signal, 1)
go func(c chan os.Signal) {
for {
<-sigchain
var (
buf []byte
stackSize int
)
bufferLen := 16384
for stackSize == len(buf) {
buf = make([]byte, bufferLen)
stackSize = runtime.Stack(buf, true)
bufferLen *= 2
}
buf = buf[:stackSize]
log.Printf("dump stacks: %s\n", string(buf))
}
}(sigchain)
signal.Notify(sigchain, syscall.SIGUSR1)
}
// Run terway daemon
func Run(pidFilePath, socketFilePath, debugSocketListen, configFilePath, kubeconfig, master, daemonMode, logLevel string) error {
level, err := log.ParseLevel(logLevel)
if err != nil {
return errors.Wrapf(err, "error set log level: %s", logLevel)
}
log.SetLevel(level)
// Write the pidfile
if pidFilePath != "" {
if !filepath.IsAbs(pidFilePath) {
return fmt.Errorf("error writing pidfile %q: path not absolute", pidFilePath)
}
if _, err := os.Stat(path.Dir(pidFilePath)); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(path.Dir(pidFilePath), 0666); err != nil {
return fmt.Errorf("error create pid file: %+v", err)
}
}
if err := ioutil.WriteFile(pidFilePath, []byte(fmt.Sprintf("%d", os.Getpid())), 0644); err != nil {
return fmt.Errorf("error writing pidfile %q: %v", pidFilePath, err)
}
}
if err := os.MkdirAll(filepath.Dir(socketFilePath), 0700); err != nil {
return err
}
if err := syscall.Unlink(socketFilePath); err != nil && !os.IsNotExist(err) {
return err
}
mask := syscall.Umask(0777)
defer syscall.Umask(mask)
l, err := net.Listen("unix", socketFilePath)
if err != nil {
return fmt.Errorf("error listen at %s: %v", socketFilePath, err)
}
networkService, err := newNetworkService(configFilePath, kubeconfig, master, daemonMode)
if err != nil {
return err
}
grpcServer := grpc.NewServer()
rpc.RegisterTerwayBackendServer(grpcServer, networkService)
rpc.RegisterTerwayTracingServer(grpcServer, tracing.DefaultRPCServer())
stop := make(chan struct{})
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigs
log.Infof("got system signal: %v, exiting", sig)
stop <- struct{}{}
}()
stackTriger()
err = runDebugServer(debugSocketListen)
if err != nil {
return err
}
go func() {
err = grpcServer.Serve(l)
if err != nil {
log.Errorf("error start grpc server: %v", err)
stop <- struct{}{}
}
}()
<-stop
grpcServer.Stop()
return nil
}
func runDebugServer(debugSocketListen string) error {
var (
l net.Listener
err error
)
if strings.HasPrefix(debugSocketListen, "unix://") {
debugSocketListen = strings.TrimPrefix(debugSocketListen, "unix://")
if err := os.MkdirAll(filepath.Dir(debugSocketListen), 0700); err != nil {
return err
}
if err := syscall.Unlink(debugSocketListen); err != nil && !os.IsNotExist(err) {
return err
}
l, err = net.Listen("unix", debugSocketListen)
if err != nil {
return fmt.Errorf("error listen at %s: %v", debugSocketListen, err)
}
} else {
l, err = net.Listen("tcp", debugSocketListen)
if err != nil {
return fmt.Errorf("error listen at %s: %v", debugSocketListen, err)
}
}
metric.RegisterPrometheus()
http.DefaultServeMux.Handle("/metrics", promhttp.Handler())
go func() {
err := http.Serve(l, http.DefaultServeMux)
if err != nil {
log.Errorf("error start debug server: %v", err)
}
}()
return nil
}