-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
grpc.go
161 lines (128 loc) · 7.39 KB
/
grpc.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package envoy
import (
"context"
"errors"
"net"
cilium "github.com/cilium/proxy/go/cilium/api"
envoy_service_cluster "github.com/cilium/proxy/go/envoy/service/cluster/v3"
envoy_service_discovery "github.com/cilium/proxy/go/envoy/service/discovery/v3"
envoy_service_endpoint "github.com/cilium/proxy/go/envoy/service/endpoint/v3"
envoy_service_listener "github.com/cilium/proxy/go/envoy/service/listener/v3"
envoy_service_route "github.com/cilium/proxy/go/envoy/service/route/v3"
envoy_service_secret "github.com/cilium/proxy/go/envoy/service/secret/v3"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"github.com/cilium/cilium/pkg/envoy/xds"
)
var (
// ErrNotImplemented is the error returned by gRPC methods that are not
// implemented by Cilium.
ErrNotImplemented = errors.New("not implemented")
)
// startXDSGRPCServer starts a gRPC server to serve xDS APIs using the given
// resource watcher and network listener.
// Returns a function that stops the GRPC server when called.
func startXDSGRPCServer(listener net.Listener, config map[string]*xds.ResourceTypeConfiguration) context.CancelFunc {
grpcServer := grpc.NewServer()
xdsServer := xds.NewServer(config)
dsServer := (*xdsGRPCServer)(xdsServer)
// TODO: https://github.com/cilium/cilium/issues/5051
// Implement IncrementalAggregatedResources to support Incremental xDS.
//envoy_service_discovery_v3.RegisterAggregatedDiscoveryServiceServer(grpcServer, dsServer)
envoy_service_secret.RegisterSecretDiscoveryServiceServer(grpcServer, dsServer)
envoy_service_endpoint.RegisterEndpointDiscoveryServiceServer(grpcServer, dsServer)
envoy_service_cluster.RegisterClusterDiscoveryServiceServer(grpcServer, dsServer)
envoy_service_route.RegisterRouteDiscoveryServiceServer(grpcServer, dsServer)
envoy_service_listener.RegisterListenerDiscoveryServiceServer(grpcServer, dsServer)
cilium.RegisterNetworkPolicyDiscoveryServiceServer(grpcServer, dsServer)
cilium.RegisterNetworkPolicyHostsDiscoveryServiceServer(grpcServer, dsServer)
reflection.Register(grpcServer)
go func() {
log.Infof("Envoy: Starting xDS gRPC server listening on %s", listener.Addr())
if err := grpcServer.Serve(listener); err != nil && !errors.Is(err, net.ErrClosed) {
log.WithError(err).Fatal("Envoy: Failed to serve xDS gRPC API")
}
}()
return grpcServer.Stop
}
// xdsGRPCServer handles gRPC streaming discovery requests for the
// resource types supported by Cilium.
type xdsGRPCServer xds.Server
// TODO: https://github.com/cilium/cilium/issues/5051
// Implement IncrementalAggregatedResources also to support Incremental xDS.
//func (s *xdsGRPCServer) StreamAggregatedResources(stream envoy_service_discovery_v3.AggregatedDiscoveryService_StreamAggregatedResourcesServer) error {
// return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, xds.AnyTypeURL)
//}
func (s *xdsGRPCServer) DeltaListeners(stream envoy_service_listener.ListenerDiscoveryService_DeltaListenersServer) error {
return ErrNotImplemented
}
func (s *xdsGRPCServer) StreamListeners(stream envoy_service_listener.ListenerDiscoveryService_StreamListenersServer) error {
return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, ListenerTypeURL)
}
func (s *xdsGRPCServer) FetchListeners(ctx context.Context, req *envoy_service_discovery.DiscoveryRequest) (*envoy_service_discovery.DiscoveryResponse, error) {
// The Fetch methods are only called via the REST API, which is not
// implemented in Cilium. Only the Stream methods are called over gRPC.
return nil, ErrNotImplemented
}
func (s *xdsGRPCServer) DeltaRoutes(stream envoy_service_route.RouteDiscoveryService_DeltaRoutesServer) error {
return ErrNotImplemented
}
func (s *xdsGRPCServer) StreamRoutes(stream envoy_service_route.RouteDiscoveryService_StreamRoutesServer) error {
return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, RouteTypeURL)
}
func (s *xdsGRPCServer) FetchRoutes(ctx context.Context, req *envoy_service_discovery.DiscoveryRequest) (*envoy_service_discovery.DiscoveryResponse, error) {
// The Fetch methods are only called via the REST API, which is not
// implemented in Cilium. Only the Stream methods are called over gRPC.
return nil, ErrNotImplemented
}
func (s *xdsGRPCServer) DeltaClusters(stream envoy_service_cluster.ClusterDiscoveryService_DeltaClustersServer) error {
return ErrNotImplemented
}
func (s *xdsGRPCServer) StreamClusters(stream envoy_service_cluster.ClusterDiscoveryService_StreamClustersServer) error {
return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, ClusterTypeURL)
}
func (s *xdsGRPCServer) FetchClusters(ctx context.Context, req *envoy_service_discovery.DiscoveryRequest) (*envoy_service_discovery.DiscoveryResponse, error) {
// The Fetch methods are only called via the REST API, which is not
// implemented in Cilium. Only the Stream methods are called over gRPC.
return nil, ErrNotImplemented
}
func (s *xdsGRPCServer) DeltaEndpoints(stream envoy_service_endpoint.EndpointDiscoveryService_DeltaEndpointsServer) error {
return ErrNotImplemented
}
func (s *xdsGRPCServer) StreamEndpoints(stream envoy_service_endpoint.EndpointDiscoveryService_StreamEndpointsServer) error {
return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, EndpointTypeURL)
}
func (s *xdsGRPCServer) FetchEndpoints(ctx context.Context, req *envoy_service_discovery.DiscoveryRequest) (*envoy_service_discovery.DiscoveryResponse, error) {
// The Fetch methods are only called via the REST API, which is not
// implemented in Cilium. Only the Stream methods are called over gRPC.
return nil, ErrNotImplemented
}
func (s *xdsGRPCServer) DeltaSecrets(stream envoy_service_secret.SecretDiscoveryService_DeltaSecretsServer) error {
return ErrNotImplemented
}
func (s *xdsGRPCServer) StreamSecrets(stream envoy_service_secret.SecretDiscoveryService_StreamSecretsServer) error {
return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, SecretTypeURL)
}
func (s *xdsGRPCServer) FetchSecrets(ctx context.Context, req *envoy_service_discovery.DiscoveryRequest) (*envoy_service_discovery.DiscoveryResponse, error) {
// The Fetch methods are only called via the REST API, which is not
// implemented in Cilium. Only the Stream methods are called over gRPC.
return nil, ErrNotImplemented
}
func (s *xdsGRPCServer) StreamNetworkPolicies(stream cilium.NetworkPolicyDiscoveryService_StreamNetworkPoliciesServer) error {
return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, NetworkPolicyTypeURL)
}
func (s *xdsGRPCServer) FetchNetworkPolicies(ctx context.Context, req *envoy_service_discovery.DiscoveryRequest) (*envoy_service_discovery.DiscoveryResponse, error) {
// The Fetch methods are only called via the REST API, which is not
// implemented in Cilium. Only the Stream methods are called over gRPC.
return nil, ErrNotImplemented
}
func (s *xdsGRPCServer) StreamNetworkPolicyHosts(stream cilium.NetworkPolicyHostsDiscoveryService_StreamNetworkPolicyHostsServer) error {
return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, NetworkPolicyHostsTypeURL)
}
func (s *xdsGRPCServer) FetchNetworkPolicyHosts(ctx context.Context, req *envoy_service_discovery.DiscoveryRequest) (*envoy_service_discovery.DiscoveryResponse, error) {
// The Fetch methods are only called via the REST API, which is not
// implemented in Cilium. Only the Stream methods are called over gRPC.
return nil, ErrNotImplemented
}