-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
server.go
110 lines (98 loc) · 2.92 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Hubble
package server
import (
"crypto/tls"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/reflection"
observerpb "github.com/cilium/cilium/api/v1/observer"
peerpb "github.com/cilium/cilium/api/v1/peer"
recorderpb "github.com/cilium/cilium/api/v1/recorder"
"github.com/cilium/cilium/pkg/hubble/server/serveroption"
)
var (
errNoListener = errors.New("no listener configured")
errNoServerTLSConfig = errors.New("no server TLS config is set")
)
// Server is hubble's gRPC server.
type Server struct {
log logrus.FieldLogger
srv *grpc.Server
opts serveroption.Options
}
// NewServer creates a new hubble gRPC server.
func NewServer(log logrus.FieldLogger, options ...serveroption.Option) (*Server, error) {
opts := serveroption.Options{}
for _, opt := range options {
if err := opt(&opts); err != nil {
return nil, fmt.Errorf("failed to apply option: %v", err)
}
}
if opts.Listener == nil {
return nil, errNoListener
}
if opts.ServerTLSConfig == nil && !opts.Insecure {
return nil, errNoServerTLSConfig
}
s := &Server{log: log, opts: opts}
if err := s.initGRPCServer(); err != nil {
return nil, err
}
return s, nil
}
func (s *Server) newGRPCServer() (*grpc.Server, error) {
var opts []grpc.ServerOption
for _, interceptor := range s.opts.GRPCUnaryInterceptors {
opts = append(opts, grpc.UnaryInterceptor(interceptor))
}
for _, interceptor := range s.opts.GRPCStreamInterceptors {
opts = append(opts, grpc.StreamInterceptor(interceptor))
}
if s.opts.ServerTLSConfig != nil {
// NOTE: gosec is unable to resolve the constant and warns about "TLS
// MinVersion too low".
tlsConfig := s.opts.ServerTLSConfig.ServerConfig(&tls.Config{ //nolint:gosec
MinVersion: serveroption.MinTLSVersion,
})
opts = append(opts, grpc.Creds(credentials.NewTLS(tlsConfig)))
}
return grpc.NewServer(opts...), nil
}
func (s *Server) initGRPCServer() error {
srv, err := s.newGRPCServer()
if err != nil {
return err
}
if s.opts.HealthService != nil {
healthpb.RegisterHealthServer(srv, s.opts.HealthService)
}
if s.opts.ObserverService != nil {
observerpb.RegisterObserverServer(srv, s.opts.ObserverService)
}
if s.opts.PeerService != nil {
peerpb.RegisterPeerServer(srv, s.opts.PeerService)
}
if s.opts.RecorderService != nil {
recorderpb.RegisterRecorderServer(srv, s.opts.RecorderService)
}
reflection.Register(srv)
if s.opts.GRPCMetrics != nil {
s.opts.GRPCMetrics.InitializeMetrics(srv)
}
s.srv = srv
return nil
}
// Serve starts the hubble server and accepts new connections on the configured
// listener. Stop should be called to stop the server.
func (s *Server) Serve() error {
return s.srv.Serve(s.opts.Listener)
}
// Stop stops the hubble server.
func (s *Server) Stop() {
s.srv.Stop()
}