xds.go 3.9 KB

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