gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-control-plane/pkg/server/v3/server.go (about)

     1  // Copyright 2018 Envoyproxy Authors
     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 server provides an implementation of a streaming xDS server.
    16  package server
    17  
    18  import (
    19  	"context"
    20  
    21  	"gitee.com/ks-custle/core-gm/grpc/codes"
    22  	"gitee.com/ks-custle/core-gm/grpc/status"
    23  
    24  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/server/delta/v3"
    25  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/server/rest/v3"
    26  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/server/sotw/v3"
    27  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/server/stream/v3"
    28  
    29  	clusterservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/cluster/v3"
    30  	discovery "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/discovery/v3"
    31  	discoverygrpc "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/discovery/v3"
    32  	endpointservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/endpoint/v3"
    33  	extensionconfigservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/extension/v3"
    34  	listenerservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/listener/v3"
    35  	routeservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/route/v3"
    36  	runtimeservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/runtime/v3"
    37  	secretservice "gitee.com/ks-custle/core-gm/go-control-plane/envoy/service/secret/v3"
    38  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/v3"
    39  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/resource/v3"
    40  )
    41  
    42  // Server is a collection of handlers for streaming discovery requests.
    43  type Server interface {
    44  	endpointservice.EndpointDiscoveryServiceServer
    45  	clusterservice.ClusterDiscoveryServiceServer
    46  	routeservice.RouteDiscoveryServiceServer
    47  	listenerservice.ListenerDiscoveryServiceServer
    48  	discoverygrpc.AggregatedDiscoveryServiceServer
    49  	secretservice.SecretDiscoveryServiceServer
    50  	runtimeservice.RuntimeDiscoveryServiceServer
    51  	extensionconfigservice.ExtensionConfigDiscoveryServiceServer
    52  
    53  	rest.Server
    54  	sotw.Server
    55  	delta.Server
    56  }
    57  
    58  // Callbacks is a collection of callbacks inserted into the server operation.
    59  // The callbacks are invoked synchronously.
    60  type Callbacks interface {
    61  	rest.Callbacks
    62  	sotw.Callbacks
    63  	delta.Callbacks
    64  }
    65  
    66  // CallbackFuncs is a convenience type for implementing the Callbacks interface.
    67  type CallbackFuncs struct {
    68  	StreamOpenFunc          func(context.Context, int64, string) error
    69  	StreamClosedFunc        func(int64)
    70  	DeltaStreamOpenFunc     func(context.Context, int64, string) error
    71  	DeltaStreamClosedFunc   func(int64)
    72  	StreamRequestFunc       func(int64, *discovery.DiscoveryRequest) error
    73  	StreamResponseFunc      func(context.Context, int64, *discovery.DiscoveryRequest, *discovery.DiscoveryResponse)
    74  	StreamDeltaRequestFunc  func(int64, *discovery.DeltaDiscoveryRequest) error
    75  	StreamDeltaResponseFunc func(int64, *discovery.DeltaDiscoveryRequest, *discovery.DeltaDiscoveryResponse)
    76  	FetchRequestFunc        func(context.Context, *discovery.DiscoveryRequest) error
    77  	FetchResponseFunc       func(*discovery.DiscoveryRequest, *discovery.DiscoveryResponse)
    78  }
    79  
    80  var _ Callbacks = CallbackFuncs{}
    81  
    82  // OnStreamOpen invokes StreamOpenFunc.
    83  func (c CallbackFuncs) OnStreamOpen(ctx context.Context, streamID int64, typeURL string) error {
    84  	if c.StreamOpenFunc != nil {
    85  		return c.StreamOpenFunc(ctx, streamID, typeURL)
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  // OnStreamClosed invokes StreamClosedFunc.
    92  func (c CallbackFuncs) OnStreamClosed(streamID int64) {
    93  	if c.StreamClosedFunc != nil {
    94  		c.StreamClosedFunc(streamID)
    95  	}
    96  }
    97  
    98  // OnDeltaStreamOpen invokes DeltaStreamOpenFunc.
    99  func (c CallbackFuncs) OnDeltaStreamOpen(ctx context.Context, streamID int64, typeURL string) error {
   100  	if c.DeltaStreamOpenFunc != nil {
   101  		return c.DeltaStreamOpenFunc(ctx, streamID, typeURL)
   102  	}
   103  
   104  	return nil
   105  }
   106  
   107  // OnDeltaStreamClosed invokes DeltaStreamClosedFunc.
   108  func (c CallbackFuncs) OnDeltaStreamClosed(streamID int64) {
   109  	if c.DeltaStreamClosedFunc != nil {
   110  		c.DeltaStreamClosedFunc(streamID)
   111  	}
   112  }
   113  
   114  // OnStreamRequest invokes StreamRequestFunc.
   115  func (c CallbackFuncs) OnStreamRequest(streamID int64, req *discovery.DiscoveryRequest) error {
   116  	if c.StreamRequestFunc != nil {
   117  		return c.StreamRequestFunc(streamID, req)
   118  	}
   119  
   120  	return nil
   121  }
   122  
   123  // OnStreamResponse invokes StreamResponseFunc.
   124  func (c CallbackFuncs) OnStreamResponse(ctx context.Context, streamID int64, req *discovery.DiscoveryRequest, resp *discovery.DiscoveryResponse) {
   125  	if c.StreamResponseFunc != nil {
   126  		c.StreamResponseFunc(ctx, streamID, req, resp)
   127  	}
   128  }
   129  
   130  // OnStreamDeltaRequest invokes StreamDeltaResponseFunc
   131  func (c CallbackFuncs) OnStreamDeltaRequest(streamID int64, req *discovery.DeltaDiscoveryRequest) error {
   132  	if c.StreamDeltaRequestFunc != nil {
   133  		return c.StreamDeltaRequestFunc(streamID, req)
   134  	}
   135  
   136  	return nil
   137  }
   138  
   139  // OnStreamDeltaResponse invokes StreamDeltaResponseFunc.
   140  func (c CallbackFuncs) OnStreamDeltaResponse(streamID int64, req *discovery.DeltaDiscoveryRequest, resp *discovery.DeltaDiscoveryResponse) {
   141  	if c.StreamDeltaResponseFunc != nil {
   142  		c.StreamDeltaResponseFunc(streamID, req, resp)
   143  	}
   144  }
   145  
   146  // OnFetchRequest invokes FetchRequestFunc.
   147  func (c CallbackFuncs) OnFetchRequest(ctx context.Context, req *discovery.DiscoveryRequest) error {
   148  	if c.FetchRequestFunc != nil {
   149  		return c.FetchRequestFunc(ctx, req)
   150  	}
   151  
   152  	return nil
   153  }
   154  
   155  // OnFetchResponse invoked FetchResponseFunc.
   156  func (c CallbackFuncs) OnFetchResponse(req *discovery.DiscoveryRequest, resp *discovery.DiscoveryResponse) {
   157  	if c.FetchResponseFunc != nil {
   158  		c.FetchResponseFunc(req, resp)
   159  	}
   160  }
   161  
   162  // NewServer creates handlers from a config watcher and callbacks.
   163  func NewServer(ctx context.Context, config cache.Cache, callbacks Callbacks) Server {
   164  	return NewServerAdvanced(rest.NewServer(config, callbacks),
   165  		sotw.NewServer(ctx, config, callbacks),
   166  		delta.NewServer(ctx, config, callbacks),
   167  	)
   168  }
   169  
   170  func NewServerAdvanced(restServer rest.Server, sotwServer sotw.Server, deltaServer delta.Server) Server {
   171  	return &server{rest: restServer, sotw: sotwServer, delta: deltaServer}
   172  }
   173  
   174  type server struct {
   175  	rest  rest.Server
   176  	sotw  sotw.Server
   177  	delta delta.Server
   178  }
   179  
   180  func (s *server) StreamHandler(stream sotw.Stream, typeURL string) error {
   181  	return s.sotw.StreamHandler(stream, typeURL)
   182  }
   183  
   184  func (s *server) StreamAggregatedResources(stream discoverygrpc.AggregatedDiscoveryService_StreamAggregatedResourcesServer) error {
   185  	return s.StreamHandler(stream, resource.AnyType)
   186  }
   187  
   188  func (s *server) StreamEndpoints(stream endpointservice.EndpointDiscoveryService_StreamEndpointsServer) error {
   189  	return s.StreamHandler(stream, resource.EndpointType)
   190  }
   191  
   192  func (s *server) StreamClusters(stream clusterservice.ClusterDiscoveryService_StreamClustersServer) error {
   193  	return s.StreamHandler(stream, resource.ClusterType)
   194  }
   195  
   196  func (s *server) StreamRoutes(stream routeservice.RouteDiscoveryService_StreamRoutesServer) error {
   197  	return s.StreamHandler(stream, resource.RouteType)
   198  }
   199  
   200  func (s *server) StreamListeners(stream listenerservice.ListenerDiscoveryService_StreamListenersServer) error {
   201  	return s.StreamHandler(stream, resource.ListenerType)
   202  }
   203  
   204  func (s *server) StreamSecrets(stream secretservice.SecretDiscoveryService_StreamSecretsServer) error {
   205  	return s.StreamHandler(stream, resource.SecretType)
   206  }
   207  
   208  func (s *server) StreamRuntime(stream runtimeservice.RuntimeDiscoveryService_StreamRuntimeServer) error {
   209  	return s.StreamHandler(stream, resource.RuntimeType)
   210  }
   211  
   212  func (s *server) StreamExtensionConfigs(stream extensionconfigservice.ExtensionConfigDiscoveryService_StreamExtensionConfigsServer) error {
   213  	return s.StreamHandler(stream, resource.ExtensionConfigType)
   214  }
   215  
   216  // Fetch is the universal fetch method.
   217  func (s *server) Fetch(ctx context.Context, req *discovery.DiscoveryRequest) (*discovery.DiscoveryResponse, error) {
   218  	return s.rest.Fetch(ctx, req)
   219  }
   220  
   221  func (s *server) FetchEndpoints(ctx context.Context, req *discovery.DiscoveryRequest) (*discovery.DiscoveryResponse, error) {
   222  	if req == nil {
   223  		return nil, status.Errorf(codes.Unavailable, "empty request")
   224  	}
   225  	req.TypeUrl = resource.EndpointType
   226  	return s.Fetch(ctx, req)
   227  }
   228  
   229  func (s *server) FetchClusters(ctx context.Context, req *discovery.DiscoveryRequest) (*discovery.DiscoveryResponse, error) {
   230  	if req == nil {
   231  		return nil, status.Errorf(codes.Unavailable, "empty request")
   232  	}
   233  	req.TypeUrl = resource.ClusterType
   234  	return s.Fetch(ctx, req)
   235  }
   236  
   237  func (s *server) FetchRoutes(ctx context.Context, req *discovery.DiscoveryRequest) (*discovery.DiscoveryResponse, error) {
   238  	if req == nil {
   239  		return nil, status.Errorf(codes.Unavailable, "empty request")
   240  	}
   241  	req.TypeUrl = resource.RouteType
   242  	return s.Fetch(ctx, req)
   243  }
   244  
   245  func (s *server) FetchListeners(ctx context.Context, req *discovery.DiscoveryRequest) (*discovery.DiscoveryResponse, error) {
   246  	if req == nil {
   247  		return nil, status.Errorf(codes.Unavailable, "empty request")
   248  	}
   249  	req.TypeUrl = resource.ListenerType
   250  	return s.Fetch(ctx, req)
   251  }
   252  
   253  func (s *server) FetchSecrets(ctx context.Context, req *discovery.DiscoveryRequest) (*discovery.DiscoveryResponse, error) {
   254  	if req == nil {
   255  		return nil, status.Errorf(codes.Unavailable, "empty request")
   256  	}
   257  	req.TypeUrl = resource.SecretType
   258  	return s.Fetch(ctx, req)
   259  }
   260  
   261  func (s *server) FetchRuntime(ctx context.Context, req *discovery.DiscoveryRequest) (*discovery.DiscoveryResponse, error) {
   262  	if req == nil {
   263  		return nil, status.Errorf(codes.Unavailable, "empty request")
   264  	}
   265  	req.TypeUrl = resource.RuntimeType
   266  	return s.Fetch(ctx, req)
   267  }
   268  
   269  func (s *server) FetchExtensionConfigs(ctx context.Context, req *discovery.DiscoveryRequest) (*discovery.DiscoveryResponse, error) {
   270  	if req == nil {
   271  		return nil, status.Errorf(codes.Unavailable, "empty request")
   272  	}
   273  	req.TypeUrl = resource.ExtensionConfigType
   274  	return s.Fetch(ctx, req)
   275  }
   276  
   277  func (s *server) DeltaStreamHandler(stream stream.DeltaStream, typeURL string) error {
   278  	return s.delta.DeltaStreamHandler(stream, typeURL)
   279  }
   280  
   281  func (s *server) DeltaAggregatedResources(stream discoverygrpc.AggregatedDiscoveryService_DeltaAggregatedResourcesServer) error {
   282  	return s.DeltaStreamHandler(stream, resource.AnyType)
   283  }
   284  
   285  func (s *server) DeltaEndpoints(stream endpointservice.EndpointDiscoveryService_DeltaEndpointsServer) error {
   286  	return s.DeltaStreamHandler(stream, resource.EndpointType)
   287  }
   288  
   289  func (s *server) DeltaClusters(stream clusterservice.ClusterDiscoveryService_DeltaClustersServer) error {
   290  	return s.DeltaStreamHandler(stream, resource.ClusterType)
   291  }
   292  
   293  func (s *server) DeltaRoutes(stream routeservice.RouteDiscoveryService_DeltaRoutesServer) error {
   294  	return s.DeltaStreamHandler(stream, resource.RouteType)
   295  }
   296  
   297  func (s *server) DeltaListeners(stream listenerservice.ListenerDiscoveryService_DeltaListenersServer) error {
   298  	return s.DeltaStreamHandler(stream, resource.ListenerType)
   299  }
   300  
   301  func (s *server) DeltaSecrets(stream secretservice.SecretDiscoveryService_DeltaSecretsServer) error {
   302  	return s.DeltaStreamHandler(stream, resource.SecretType)
   303  }
   304  
   305  func (s *server) DeltaRuntime(stream runtimeservice.RuntimeDiscoveryService_DeltaRuntimeServer) error {
   306  	return s.DeltaStreamHandler(stream, resource.RuntimeType)
   307  }
   308  
   309  func (s *server) DeltaExtensionConfigs(stream extensionconfigservice.ExtensionConfigDiscoveryService_DeltaExtensionConfigsServer) error {
   310  	return s.DeltaStreamHandler(stream, resource.ExtensionConfigType)
   311  }