xds.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Package xds contains an implementation of the xDS suite of protocols, to be
  19. // used by gRPC client and server applications.
  20. //
  21. // On the client-side, users simply need to import this package to get all xDS
  22. // functionality. On the server-side, users need to use the GRPCServer type
  23. // exported by this package instead of the regular grpc.Server.
  24. //
  25. // See https://github.com/grpc/grpc-go/tree/master/examples/features/xds for
  26. // example.
  27. package xds
  28. import (
  29. "fmt"
  30. v3statusgrpc "github.com/envoyproxy/go-control-plane/envoy/service/status/v3"
  31. "google.golang.org/grpc"
  32. internaladmin "google.golang.org/grpc/internal/admin"
  33. "google.golang.org/grpc/xds/csds"
  34. _ "google.golang.org/grpc/credentials/tls/certprovider/pemfile" // Register the file watcher certificate provider plugin.
  35. _ "google.golang.org/grpc/xds/internal/balancer" // Register the balancers.
  36. _ "google.golang.org/grpc/xds/internal/client/v2" // Register the v2 xDS API client.
  37. _ "google.golang.org/grpc/xds/internal/client/v3" // Register the v3 xDS API client.
  38. _ "google.golang.org/grpc/xds/internal/httpfilter/fault" // Register the fault injection filter.
  39. _ "google.golang.org/grpc/xds/internal/resolver" // Register the xds_resolver.
  40. )
  41. func init() {
  42. internaladmin.AddService(func(registrar grpc.ServiceRegistrar) (func(), error) {
  43. var grpcServer *grpc.Server
  44. switch ss := registrar.(type) {
  45. case *grpc.Server:
  46. grpcServer = ss
  47. case *GRPCServer:
  48. sss, ok := ss.gs.(*grpc.Server)
  49. if !ok {
  50. logger.Warningf("grpc server within xds.GRPCServer is not *grpc.Server, CSDS will not be registered")
  51. return nil, nil
  52. }
  53. grpcServer = sss
  54. default:
  55. // Returning an error would cause the top level admin.Register() to
  56. // fail. Log a warning instead.
  57. logger.Warningf("server to register service on is neither a *grpc.Server or a *xds.GRPCServer, CSDS will not be registered")
  58. return nil, nil
  59. }
  60. csdss, err := csds.NewClientStatusDiscoveryServer()
  61. if err != nil {
  62. return nil, fmt.Errorf("failed to create csds server: %v", err)
  63. }
  64. v3statusgrpc.RegisterClientStatusDiscoveryServiceServer(grpcServer, csdss)
  65. return csdss.Close, nil
  66. })
  67. }