github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/xds.go (about)

     1  /*
     2   *
     3   * Copyright 2020 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  // Package xds contains an implementation of the xDS suite of protocols, to be
    20  // used by gRPC client and server applications.
    21  //
    22  // On the client-side, users simply need to import this package to get all xDS
    23  // functionality. On the server-side, users need to use the GRPCServer type
    24  // exported by this package instead of the regular grpc.Server.
    25  //
    26  // See https://github.com/grpc/grpc-go/tree/master/examples/features/xds for
    27  // example.
    28  package xds
    29  
    30  import (
    31  	"fmt"
    32  
    33  	v3statusgrpc "github.com/hxx258456/ccgo/go-control-plane/envoy/service/status/v3"
    34  	grpc "github.com/hxx258456/ccgo/grpc"
    35  	internaladmin "github.com/hxx258456/ccgo/grpc/internal/admin"
    36  	"github.com/hxx258456/ccgo/grpc/resolver"
    37  	"github.com/hxx258456/ccgo/grpc/xds/csds"
    38  
    39  	_ "github.com/hxx258456/ccgo/grpc/credentials/tls/certprovider/pemfile"         // Register the file watcher certificate provider plugin.
    40  	_ "github.com/hxx258456/ccgo/grpc/xds/internal/balancer"                        // Register the balancers.
    41  	_ "github.com/hxx258456/ccgo/grpc/xds/internal/httpfilter/fault"                // Register the fault injection filter.
    42  	_ "github.com/hxx258456/ccgo/grpc/xds/internal/httpfilter/rbac"                 // Register the RBAC filter.
    43  	_ "github.com/hxx258456/ccgo/grpc/xds/internal/httpfilter/router"               // Register the router filter.
    44  	xdsresolver "github.com/hxx258456/ccgo/grpc/xds/internal/resolver"              // Register the xds_resolver.
    45  	_ "github.com/hxx258456/ccgo/grpc/xds/internal/xdsclient/controller/version/v2" // Register the v2 xDS API client.
    46  	_ "github.com/hxx258456/ccgo/grpc/xds/internal/xdsclient/controller/version/v3" // Register the v3 xDS API client.
    47  )
    48  
    49  func init() {
    50  	internaladmin.AddService(func(registrar grpc.ServiceRegistrar) (func(), error) {
    51  		var grpcServer *grpc.Server
    52  		switch ss := registrar.(type) {
    53  		case *grpc.Server:
    54  			grpcServer = ss
    55  		case *GRPCServer:
    56  			sss, ok := ss.gs.(*grpc.Server)
    57  			if !ok {
    58  				logger.Warningf("grpc server within xds.GRPCServer is not *grpc.Server, CSDS will not be registered")
    59  				return nil, nil
    60  			}
    61  			grpcServer = sss
    62  		default:
    63  			// Returning an error would cause the top level admin.Register() to
    64  			// fail. Log a warning instead.
    65  			logger.Warningf("server to register service on is neither a *grpc.Server or a *xds.GRPCServer, CSDS will not be registered")
    66  			return nil, nil
    67  		}
    68  
    69  		csdss, err := csds.NewClientStatusDiscoveryServer()
    70  		if err != nil {
    71  			return nil, fmt.Errorf("failed to create csds server: %v", err)
    72  		}
    73  		v3statusgrpc.RegisterClientStatusDiscoveryServiceServer(grpcServer, csdss)
    74  		return csdss.Close, nil
    75  	})
    76  }
    77  
    78  // NewXDSResolverWithConfigForTesting creates a new xds resolver builder using
    79  // the provided xds bootstrap config instead of the global configuration from
    80  // the supported environment variables.  The resolver.Builder is meant to be
    81  // used in conjunction with the grpc.WithResolvers DialOption.
    82  //
    83  // Testing Only
    84  //
    85  // This function should ONLY be used for testing and may not work with some
    86  // other features, including the CSDS service.
    87  //
    88  // Experimental
    89  //
    90  // Notice: This API is EXPERIMENTAL and may be changed or removed in a
    91  // later release.
    92  func NewXDSResolverWithConfigForTesting(bootstrapConfig []byte) (resolver.Builder, error) {
    93  	return xdsresolver.NewBuilder(bootstrapConfig)
    94  }