...

Source file src/google.golang.org/grpc/xds/xds.go

Documentation: google.golang.org/grpc/xds

     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  	"google.golang.org/grpc"
    34  	"google.golang.org/grpc/grpclog"
    35  	"google.golang.org/grpc/internal"
    36  	internaladmin "google.golang.org/grpc/internal/admin"
    37  	"google.golang.org/grpc/resolver"
    38  	"google.golang.org/grpc/xds/csds"
    39  
    40  	_ "google.golang.org/grpc/credentials/tls/certprovider/pemfile"           // Register the file watcher certificate provider plugin.
    41  	_ "google.golang.org/grpc/xds/internal/balancer"                          // Register the balancers.
    42  	_ "google.golang.org/grpc/xds/internal/clusterspecifier/rls"              // Register the RLS cluster specifier plugin. Note that this does not register the RLS LB policy.
    43  	_ "google.golang.org/grpc/xds/internal/httpfilter/fault"                  // Register the fault injection filter.
    44  	_ "google.golang.org/grpc/xds/internal/httpfilter/rbac"                   // Register the RBAC filter.
    45  	_ "google.golang.org/grpc/xds/internal/httpfilter/router"                 // Register the router filter.
    46  	_ "google.golang.org/grpc/xds/internal/resolver"                          // Register the xds_resolver.
    47  	_ "google.golang.org/grpc/xds/internal/xdsclient/xdslbregistry/converter" // Register the xDS LB Registry Converters.
    48  
    49  	v3statusgrpc "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
    50  )
    51  
    52  var logger = grpclog.Component("xds")
    53  
    54  func init() {
    55  	internaladmin.AddService(func(registrar grpc.ServiceRegistrar) (func(), error) {
    56  		var grpcServer *grpc.Server
    57  		switch ss := registrar.(type) {
    58  		case *grpc.Server:
    59  			grpcServer = ss
    60  		case *GRPCServer:
    61  			sss, ok := ss.gs.(*grpc.Server)
    62  			if !ok {
    63  				logger.Warning("grpc server within xds.GRPCServer is not *grpc.Server, CSDS will not be registered")
    64  				return nil, nil
    65  			}
    66  			grpcServer = sss
    67  		default:
    68  			// Returning an error would cause the top level admin.Register() to
    69  			// fail. Log a warning instead.
    70  			logger.Error("Server to register service on is neither a *grpc.Server or a *xds.GRPCServer, CSDS will not be registered")
    71  			return nil, nil
    72  		}
    73  
    74  		csdss, err := csds.NewClientStatusDiscoveryServer()
    75  		if err != nil {
    76  			return nil, fmt.Errorf("failed to create csds server: %v", err)
    77  		}
    78  		v3statusgrpc.RegisterClientStatusDiscoveryServiceServer(grpcServer, csdss)
    79  		return csdss.Close, nil
    80  	})
    81  }
    82  
    83  // NewXDSResolverWithConfigForTesting creates a new xDS resolver builder using
    84  // the provided xDS bootstrap config instead of the global configuration from
    85  // the supported environment variables.  The resolver.Builder is meant to be
    86  // used in conjunction with the grpc.WithResolvers DialOption.
    87  //
    88  // # Testing Only
    89  //
    90  // This function should ONLY be used for testing and may not work with some
    91  // other features, including the CSDS service.
    92  //
    93  // # Experimental
    94  //
    95  // Notice: This API is EXPERIMENTAL and may be changed or removed in a
    96  // later release.
    97  func NewXDSResolverWithConfigForTesting(bootstrapConfig []byte) (resolver.Builder, error) {
    98  	return internal.NewXDSResolverWithConfigForTesting.(func([]byte) (resolver.Builder, error))(bootstrapConfig)
    99  }
   100  

View as plain text