forked from projectcontour/contour
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
126 lines (105 loc) · 4.02 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
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package grpc provides a gRPC implementation of the Envoy v2 xDS API.
package grpc
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/envoyproxy/go-control-plane/envoy/api/v2"
envoy_service_v2 "github.com/envoyproxy/go-control-plane/envoy/service/load_stats/v2"
"github.com/sirupsen/logrus"
)
const (
// somewhat arbitrary limit to handle many, many, EDS streams
grpcMaxConcurrentStreams = 1 << 20
)
// NewAPI returns a *grpc.Server which responds to the Envoy v2 xDS gRPC API.
func NewAPI(log logrus.FieldLogger, cacheMap map[string]Cache) *grpc.Server {
opts := []grpc.ServerOption{
// By default the Go grpc library defaults to a value of ~100 streams per
// connection. This number is likely derived from the HTTP/2 spec:
// https://http2.github.io/http2-spec/#SettingValues
// We need to raise this value because Envoy will open one EDS stream per
// CDS entry. There doesn't seem to be a penalty for increasing this value,
// so set it the limit similar to envoyproxy/go-control-plane#70.
grpc.MaxConcurrentStreams(grpcMaxConcurrentStreams),
}
g := grpc.NewServer(opts...)
s := &grpcServer{
xdsHandler{
FieldLogger: log,
resources: map[string]resource{
clusterType: &CDS{
Cache: cacheMap[clusterType],
},
endpointType: &EDS{
Cache: cacheMap[endpointType],
},
listenerType: &LDS{
Cache: cacheMap[listenerType],
},
routeType: &RDS{
Cache: cacheMap[routeType],
},
},
},
}
v2.RegisterClusterDiscoveryServiceServer(g, s)
v2.RegisterEndpointDiscoveryServiceServer(g, s)
v2.RegisterListenerDiscoveryServiceServer(g, s)
v2.RegisterRouteDiscoveryServiceServer(g, s)
return g
}
// grpcServer implements the LDS, RDS, CDS, and EDS, gRPC endpoints.
type grpcServer struct {
xdsHandler
}
// A resource provides resources formatted as []types.Any.
type resource interface {
Cache
// TypeURL returns the typeURL of messages returned from Values.
TypeURL() string
}
func (s *grpcServer) FetchClusters(_ context.Context, req *v2.DiscoveryRequest) (*v2.DiscoveryResponse, error) {
return s.fetch(req)
}
func (s *grpcServer) FetchEndpoints(_ context.Context, req *v2.DiscoveryRequest) (*v2.DiscoveryResponse, error) {
return s.fetch(req)
}
func (s *grpcServer) FetchListeners(_ context.Context, req *v2.DiscoveryRequest) (*v2.DiscoveryResponse, error) {
return s.fetch(req)
}
func (s *grpcServer) FetchRoutes(_ context.Context, req *v2.DiscoveryRequest) (*v2.DiscoveryResponse, error) {
return s.fetch(req)
}
func (s *grpcServer) StreamClusters(srv v2.ClusterDiscoveryService_StreamClustersServer) error {
return s.stream(srv)
}
func (s *grpcServer) StreamEndpoints(srv v2.EndpointDiscoveryService_StreamEndpointsServer) error {
return s.stream(srv)
}
func (s *grpcServer) StreamLoadStats(srv envoy_service_v2.LoadReportingService_StreamLoadStatsServer) error {
return status.Errorf(codes.Unimplemented, "StreamLoadStats unimplemented")
}
func (s *grpcServer) IncrementalClusters(v2.ClusterDiscoveryService_IncrementalClustersServer) error {
return status.Errorf(codes.Unimplemented, "IncrementalClusters unimplemented")
}
func (s *grpcServer) IncrementalRoutes(v2.RouteDiscoveryService_IncrementalRoutesServer) error {
return status.Errorf(codes.Unimplemented, "IncrementalRoutes unimplemented")
}
func (s *grpcServer) StreamListeners(srv v2.ListenerDiscoveryService_StreamListenersServer) error {
return s.stream(srv)
}
func (s *grpcServer) StreamRoutes(srv v2.RouteDiscoveryService_StreamRoutesServer) error {
return s.stream(srv)
}