• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2022 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_SRC_CORE_XDS_GRPC_XDS_CLIENT_GRPC_H
18 #define GRPC_SRC_CORE_XDS_GRPC_XDS_CLIENT_GRPC_H
19 
20 #include <grpc/grpc.h>
21 #include <grpc/support/port_platform.h>
22 
23 #include <memory>
24 
25 #include "absl/status/statusor.h"
26 #include "absl/strings/string_view.h"
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/iomgr/iomgr_fwd.h"
29 #include "src/core/resolver/endpoint_addresses.h"
30 #include "src/core/telemetry/metrics.h"
31 #include "src/core/util/orphanable.h"
32 #include "src/core/util/ref_counted_ptr.h"
33 #include "src/core/util/useful.h"
34 #include "src/core/xds/grpc/certificate_provider_store.h"
35 #include "src/core/xds/grpc/xds_bootstrap_grpc.h"
36 #include "src/core/xds/xds_client/lrs_client.h"
37 #include "src/core/xds/xds_client/xds_client.h"
38 #include "src/core/xds/xds_client/xds_transport.h"
39 
40 namespace grpc_core {
41 
42 class GrpcXdsClient final : public XdsClient {
43  public:
44   // The key to pass to GetOrCreate() for gRPC servers.
45   static constexpr absl::string_view kServerKey = "#server";
46 
47   // Factory function to get or create the global XdsClient instance.
48   static absl::StatusOr<RefCountedPtr<GrpcXdsClient>> GetOrCreate(
49       absl::string_view key, const ChannelArgs& args, const char* reason);
50 
51   // Do not instantiate directly -- use GetOrCreate() instead.
52   // TODO(roth): The transport factory is injectable here to support
53   // tests that want to use a fake transport factory with code that
54   // expects a GrpcXdsClient instead of an XdsClient, typically because
55   // it needs to call the interested_parties() method.  Once we
56   // finish the EventEngine migration and remove the interested_parties()
57   // method, consider instead changing callers to an approach where the
58   // production code uses XdsClient instead of GrpcXdsClient, and then
59   // passing in a fake XdsClient impl in the tests.  Note that this will
60   // work for callers that use interested_parties() but not for callers
61   // that also use certificate_provider_store(), but we should consider
62   // alternatives for that case as well.
63   GrpcXdsClient(absl::string_view key,
64                 std::shared_ptr<GrpcXdsBootstrap> bootstrap,
65                 const ChannelArgs& args,
66                 RefCountedPtr<XdsTransportFactory> transport_factory,
67                 GlobalStatsPluginRegistry::StatsPluginGroup stats_plugin_group);
68 
69   // Helpers for encoding the XdsClient object in channel args.
ChannelArgName()70   static absl::string_view ChannelArgName() {
71     return GRPC_ARG_NO_SUBCHANNEL_PREFIX "xds_client";
72   }
ChannelArgsCompare(const XdsClient * a,const XdsClient * b)73   static int ChannelArgsCompare(const XdsClient* a, const XdsClient* b) {
74     return QsortCompare(a, b);
75   }
76 
77   void ResetBackoff() override;
78 
79   grpc_pollset_set* interested_parties() const;
80 
certificate_provider_store()81   CertificateProviderStore& certificate_provider_store() const {
82     return *certificate_provider_store_;
83   }
84 
key()85   absl::string_view key() const { return key_; }
86 
lrs_client()87   LrsClient& lrs_client() { return *lrs_client_; }
88 
89   // Builds ClientStatusResponse containing all resources from all XdsClients
90   static grpc_slice DumpAllClientConfigs();
91 
92  private:
93   class MetricsReporter;
94 
95   void ReportCallbackMetrics(CallbackMetricReporter& reporter);
96   void Orphaned() override;
97 
98   std::string key_;
99   OrphanablePtr<CertificateProviderStore> certificate_provider_store_;
100   GlobalStatsPluginRegistry::StatsPluginGroup stats_plugin_group_;
101   std::unique_ptr<RegisteredMetricCallback> registered_metric_callback_;
102   RefCountedPtr<LrsClient> lrs_client_;
103 };
104 
105 namespace internal {
106 void SetXdsChannelArgsForTest(grpc_channel_args* args);
107 void UnsetGlobalXdsClientsForTest();
108 // Sets bootstrap config to be used when no env var is set.
109 // Does not take ownership of config.
110 void SetXdsFallbackBootstrapConfig(const char* config);
111 }  // namespace internal
112 
113 }  // namespace grpc_core
114 
115 #endif  // GRPC_SRC_CORE_XDS_GRPC_XDS_CLIENT_GRPC_H
116