forked from cilium/cilium
/
grpc.go
113 lines (93 loc) · 4.71 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
// Copyright 2018 Authors of Cilium
//
// Licensed under the Apache License, Version 2.0 (the "License");
// 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 envoy
import (
"context"
"errors"
"net"
"strings"
"time"
"github.com/cilium/cilium/pkg/envoy/xds"
"github.com/cilium/proxy/go/cilium/api"
envoy_api_v2 "github.com/cilium/proxy/go/envoy/api/v2"
net_context "golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
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, ldsConfig, npdsConfig, nphdsConfig *xds.ResourceTypeConfiguration, resourceAccessTimeout time.Duration) context.CancelFunc {
grpcServer := grpc.NewServer()
xdsServer := xds.NewServer(map[string]*xds.ResourceTypeConfiguration{
ListenerTypeURL: ldsConfig,
NetworkPolicyTypeURL: npdsConfig,
NetworkPolicyHostsTypeURL: nphdsConfig,
}, resourceAccessTimeout)
dsServer := (*xdsGRPCServer)(xdsServer)
// TODO: https://github.com/cilium/cilium/issues/5051
// Implement IncrementalAggregatedResources to support Incremental xDS.
//envoy_service_discovery_v2.RegisterAggregatedDiscoveryServiceServer(grpcServer, dsServer)
envoy_api_v2.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 && !strings.Contains(err.Error(), "closed network connection") {
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_v2.AggregatedDiscoveryService_StreamAggregatedResourcesServer) error {
// return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, xds.AnyTypeURL)
//}
func (s *xdsGRPCServer) DeltaListeners(stream envoy_api_v2.ListenerDiscoveryService_DeltaListenersServer) error {
return ErrNotImplemented
}
func (s *xdsGRPCServer) StreamListeners(stream envoy_api_v2.ListenerDiscoveryService_StreamListenersServer) error {
return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, ListenerTypeURL)
}
func (s *xdsGRPCServer) FetchListeners(ctx net_context.Context, req *envoy_api_v2.DiscoveryRequest) (*envoy_api_v2.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 net_context.Context, req *envoy_api_v2.DiscoveryRequest) (*envoy_api_v2.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 net_context.Context, req *envoy_api_v2.DiscoveryRequest) (*envoy_api_v2.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
}