Debian Codegraph @

src/core/ext/xds/xds_client_grpc.cc

raw
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 #include <grpc/support/port_platform.h>
18
19 #include "src/core/ext/xds/xds_client_grpc.h"
20
21 #include <algorithm>
22 #include <memory>
23 #include <string>
24 #include <utility>
25
26 #include "absl/base/thread_annotations.h"
27 #include "absl/status/status.h"
28 #include "absl/strings/string_view.h"
29 #include "absl/types/optional.h"
30
31 #include <grpc/grpc.h>
32 #include <grpc/slice.h>
33 #include <grpc/support/alloc.h>
34 #include <grpc/support/log.h>
35 #include <grpc/support/string_util.h>
36
37 #include "src/core/ext/xds/xds_bootstrap.h"
38 #include "src/core/ext/xds/xds_bootstrap_grpc.h"
39 #include "src/core/ext/xds/xds_channel_args.h"
40 #include "src/core/ext/xds/xds_transport.h"
41 #include "src/core/ext/xds/xds_transport_grpc.h"
42 #include "src/core/lib/channel/channel_args.h"
43 #include "src/core/lib/debug/trace.h"
44 #include "src/core/lib/gprpp/debug_location.h"
45 #include "src/core/lib/gprpp/env.h"
46 #include "src/core/lib/gprpp/orphanable.h"
47 #include "src/core/lib/gprpp/ref_counted_ptr.h"
48 #include "src/core/lib/gprpp/sync.h"
49 #include "src/core/lib/gprpp/time.h"
50 #include "src/core/lib/iomgr/error.h"
51 #include "src/core/lib/iomgr/exec_ctx.h"
52 #include "src/core/lib/iomgr/load_file.h"
53 #include "src/core/lib/slice/slice.h"
54 #include "src/core/lib/slice/slice_internal.h"
55 #include "src/core/lib/transport/error_utils.h"
56
57 namespace grpc_core {
58
59 //
60 // GrpcXdsClient
61 //
62
63 namespace {
64
65 Mutex* g_mu = new Mutex;
66 const grpc_channel_args* g_channel_args ABSL_GUARDED_BY(*g_mu) = nullptr;
67 GrpcXdsClient* g_xds_client ABSL_GUARDED_BY(*g_mu) = nullptr;
68 char* g_fallback_bootstrap_config ABSL_GUARDED_BY(*g_mu) = nullptr;
69
70 } // namespace
71
72 namespace {
73
74 absl::StatusOr<std::string> GetBootstrapContents(const char* fallback_config) {
75 // First, try GRPC_XDS_BOOTSTRAP env var.
76 auto path = GetEnv("GRPC_XDS_BOOTSTRAP");
77 if (path.has_value()) {
78 if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_trace)) {
79 gpr_log(GPR_INFO,
80 "Got bootstrap file location from GRPC_XDS_BOOTSTRAP "
81 "environment variable: %s",
82 path->c_str());
83 }
84 grpc_slice contents;
85 grpc_error_handle error =
86 grpc_load_file(path->c_str(), /*add_null_terminator=*/true, &contents);
87 if (!error.ok()) return grpc_error_to_absl_status(error);
88 std::string contents_str(StringViewFromSlice(contents));
89 CSliceUnref(contents);
90 return contents_str;
91 }
92 // Next, try GRPC_XDS_BOOTSTRAP_CONFIG env var.
93 auto env_config = GetEnv("GRPC_XDS_BOOTSTRAP_CONFIG");
94 if (env_config.has_value()) {
95 if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_trace)) {
96 gpr_log(GPR_INFO,
97 "Got bootstrap contents from GRPC_XDS_BOOTSTRAP_CONFIG "
98 "environment variable");
99 }
100 return std::move(*env_config);
101 }
102 // Finally, try fallback config.
103 if (fallback_config != nullptr) {
104 if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_trace)) {
105 gpr_log(GPR_INFO, "Got bootstrap contents from fallback config");
106 }
107 return fallback_config;
108 }
109 // No bootstrap config found.
110 return absl::FailedPreconditionError(
111 "Environment variables GRPC_XDS_BOOTSTRAP or GRPC_XDS_BOOTSTRAP_CONFIG "
112 "not defined");
113 }
114
115 } // namespace
116
117 absl::StatusOr<RefCountedPtr<GrpcXdsClient>> GrpcXdsClient::GetOrCreate(
118 const ChannelArgs& args, const char* reason) {
119 // If getting bootstrap from channel args, create a local XdsClient
120 // instance for the channel or server instead of using the global instance.
121 absl::optional<absl::string_view> bootstrap_config = args.GetString(
122 GRPC_ARG_TEST_ONLY_DO_NOT_USE_IN_PROD_XDS_BOOTSTRAP_CONFIG);
123 if (bootstrap_config.has_value()) {
124 auto bootstrap = GrpcXdsBootstrap::Create(*bootstrap_config);
125 if (!bootstrap.ok()) return bootstrap.status();
126 grpc_channel_args* xds_channel_args = args.GetPointer<grpc_channel_args>(
127 GRPC_ARG_TEST_ONLY_DO_NOT_USE_IN_PROD_XDS_CLIENT_CHANNEL_ARGS);
128 return MakeRefCounted<GrpcXdsClient>(std::move(*bootstrap),
129 ChannelArgs::FromC(xds_channel_args));
130 }
131 // Otherwise, use the global instance.
132 MutexLock lock(g_mu);
133 if (g_xds_client != nullptr) {
134 auto xds_client = g_xds_client->RefIfNonZero(DEBUG_LOCATION, reason);
135 if (xds_client != nullptr) return xds_client;
136 }
137 // Find bootstrap contents.
138 auto bootstrap_contents = GetBootstrapContents(g_fallback_bootstrap_config);
139 if (!bootstrap_contents.ok()) return bootstrap_contents.status();
140 if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_trace)) {
141 gpr_log(GPR_INFO, "xDS bootstrap contents: %s",
142 bootstrap_contents->c_str());
143 }
144 // Parse bootstrap.
145 auto bootstrap = GrpcXdsBootstrap::Create(*bootstrap_contents);
146 if (!bootstrap.ok()) return bootstrap.status();
147 // Instantiate XdsClient.
148 auto xds_client = MakeRefCounted<GrpcXdsClient>(
149 std::move(*bootstrap), ChannelArgs::FromC(g_channel_args));
150 g_xds_client = xds_client.get();
151 return xds_client;
152 }
153
154 GrpcXdsClient::GrpcXdsClient(std::unique_ptr<GrpcXdsBootstrap> bootstrap,
155 const ChannelArgs& args)
156 : XdsClient(
157 std::move(bootstrap), MakeOrphanable<GrpcXdsTransportFactory>(args),
158 std::max(Duration::Zero(),
159 args.GetDurationFromIntMillis(
160 GRPC_ARG_XDS_RESOURCE_DOES_NOT_EXIST_TIMEOUT_MS)
161 .value_or(Duration::Seconds(15)))),
162 certificate_provider_store_(MakeOrphanable<CertificateProviderStore>(
163 static_cast<const GrpcXdsBootstrap&>(this->bootstrap())
164 .certificate_providers())) {}
165
166 GrpcXdsClient::~GrpcXdsClient() {
167 MutexLock lock(g_mu);
168 if (g_xds_client == this) g_xds_client = nullptr;
169 }
170
171 grpc_pollset_set* GrpcXdsClient::interested_parties() const {
172 return reinterpret_cast<GrpcXdsTransportFactory*>(transport_factory())
173 ->interested_parties();
174 }
175
176 namespace internal {
177
178 void SetXdsChannelArgsForTest(grpc_channel_args* args) {
179 MutexLock lock(g_mu);
180 g_channel_args = args;
181 }
182
183 void UnsetGlobalXdsClientForTest() {
184 MutexLock lock(g_mu);
185 g_xds_client = nullptr;
186 }
187
188 void SetXdsFallbackBootstrapConfig(const char* config) {
189 MutexLock lock(g_mu);
190 gpr_free(g_fallback_bootstrap_config);
191 g_fallback_bootstrap_config = gpr_strdup(config);
192 }
193
194 } // namespace internal
195
196 } // namespace grpc_core
197
198 // The returned bytes may contain NULL(0), so we can't use c-string.
199 grpc_slice grpc_dump_xds_configs(void) {
200 grpc_core::ApplicationCallbackExecCtx callback_exec_ctx;
201 grpc_core::ExecCtx exec_ctx;
202 grpc_error_handle error;
203 auto xds_client = grpc_core::GrpcXdsClient::GetOrCreate(
204 grpc_core::ChannelArgs(), "grpc_dump_xds_configs()");
205 if (!xds_client.ok()) {
206 // If we aren't using xDS, just return an empty string.
207 return grpc_empty_slice();
208 }
209 return grpc_slice_from_cpp_string((*xds_client)->DumpClientConfigBinary());
210 }