github.com/kubearmor/cilium@v1.6.12/pkg/envoy/grpc.go (about)

     1  // Copyright 2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package envoy
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"net"
    21  	"strings"
    22  	"time"
    23  
    24  	"github.com/cilium/cilium/pkg/envoy/xds"
    25  
    26  	"github.com/cilium/proxy/go/cilium/api"
    27  	envoy_api_v2 "github.com/cilium/proxy/go/envoy/api/v2"
    28  	net_context "golang.org/x/net/context"
    29  	"google.golang.org/grpc"
    30  	"google.golang.org/grpc/reflection"
    31  )
    32  
    33  var (
    34  	// ErrNotImplemented is the error returned by gRPC methods that are not
    35  	// implemented by Cilium.
    36  	ErrNotImplemented = errors.New("not implemented")
    37  )
    38  
    39  // startXDSGRPCServer starts a gRPC server to serve xDS APIs using the given
    40  // resource watcher and network listener.
    41  // Returns a function that stops the GRPC server when called.
    42  func startXDSGRPCServer(listener net.Listener, ldsConfig, npdsConfig, nphdsConfig *xds.ResourceTypeConfiguration, resourceAccessTimeout time.Duration) context.CancelFunc {
    43  	grpcServer := grpc.NewServer()
    44  
    45  	xdsServer := xds.NewServer(map[string]*xds.ResourceTypeConfiguration{
    46  		ListenerTypeURL:           ldsConfig,
    47  		NetworkPolicyTypeURL:      npdsConfig,
    48  		NetworkPolicyHostsTypeURL: nphdsConfig,
    49  	}, resourceAccessTimeout)
    50  	dsServer := (*xdsGRPCServer)(xdsServer)
    51  
    52  	// TODO: https://github.com/cilium/cilium/issues/5051
    53  	// Implement IncrementalAggregatedResources to support Incremental xDS.
    54  	//envoy_service_discovery_v2.RegisterAggregatedDiscoveryServiceServer(grpcServer, dsServer)
    55  	envoy_api_v2.RegisterListenerDiscoveryServiceServer(grpcServer, dsServer)
    56  	cilium.RegisterNetworkPolicyDiscoveryServiceServer(grpcServer, dsServer)
    57  	cilium.RegisterNetworkPolicyHostsDiscoveryServiceServer(grpcServer, dsServer)
    58  
    59  	reflection.Register(grpcServer)
    60  
    61  	go func() {
    62  		log.Infof("Envoy: Starting xDS gRPC server listening on %s", listener.Addr())
    63  		if err := grpcServer.Serve(listener); err != nil && !strings.Contains(err.Error(), "closed network connection") {
    64  			log.WithError(err).Fatal("Envoy: Failed to serve xDS gRPC API")
    65  		}
    66  	}()
    67  
    68  	return grpcServer.Stop
    69  }
    70  
    71  // xdsGRPCServer handles gRPC streaming discovery requests for the
    72  // resource types supported by Cilium.
    73  type xdsGRPCServer xds.Server
    74  
    75  // TODO: https://github.com/cilium/cilium/issues/5051
    76  // Implement IncrementalAggregatedResources also to support Incremental xDS.
    77  //func (s *xdsGRPCServer) StreamAggregatedResources(stream envoy_service_discovery_v2.AggregatedDiscoveryService_StreamAggregatedResourcesServer) error {
    78  //	return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, xds.AnyTypeURL)
    79  //}
    80  
    81  func (s *xdsGRPCServer) DeltaListeners(stream envoy_api_v2.ListenerDiscoveryService_DeltaListenersServer) error {
    82  	return ErrNotImplemented
    83  }
    84  
    85  func (s *xdsGRPCServer) StreamListeners(stream envoy_api_v2.ListenerDiscoveryService_StreamListenersServer) error {
    86  	return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, ListenerTypeURL)
    87  }
    88  
    89  func (s *xdsGRPCServer) FetchListeners(ctx net_context.Context, req *envoy_api_v2.DiscoveryRequest) (*envoy_api_v2.DiscoveryResponse, error) {
    90  	// The Fetch methods are only called via the REST API, which is not
    91  	// implemented in Cilium. Only the Stream methods are called over gRPC.
    92  	return nil, ErrNotImplemented
    93  }
    94  
    95  func (s *xdsGRPCServer) StreamNetworkPolicies(stream cilium.NetworkPolicyDiscoveryService_StreamNetworkPoliciesServer) error {
    96  	return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, NetworkPolicyTypeURL)
    97  }
    98  
    99  func (s *xdsGRPCServer) FetchNetworkPolicies(ctx net_context.Context, req *envoy_api_v2.DiscoveryRequest) (*envoy_api_v2.DiscoveryResponse, error) {
   100  	// The Fetch methods are only called via the REST API, which is not
   101  	// implemented in Cilium. Only the Stream methods are called over gRPC.
   102  	return nil, ErrNotImplemented
   103  }
   104  
   105  func (s *xdsGRPCServer) StreamNetworkPolicyHosts(stream cilium.NetworkPolicyHostsDiscoveryService_StreamNetworkPolicyHostsServer) error {
   106  	return (*xds.Server)(s).HandleRequestStream(stream.Context(), stream, NetworkPolicyHostsTypeURL)
   107  }
   108  
   109  func (s *xdsGRPCServer) FetchNetworkPolicyHosts(ctx net_context.Context, req *envoy_api_v2.DiscoveryRequest) (*envoy_api_v2.DiscoveryResponse, error) {
   110  	// The Fetch methods are only called via the REST API, which is not
   111  	// implemented in Cilium. Only the Stream methods are called over gRPC.
   112  	return nil, ErrNotImplemented
   113  }