Debian Codegraph @

src/core/ext/xds/xds_cluster.cc

raw
1 //
2 // Copyright 2018 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_cluster.h"
20
21 #include <stddef.h>
22
23 #include <utility>
24
25 #include "absl/status/status.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/str_cat.h"
28 #include "absl/strings/str_join.h"
29 #include "absl/strings/strip.h"
30 #include "absl/types/variant.h"
31 #include "envoy/config/cluster/v3/circuit_breaker.upb.h"
32 #include "envoy/config/cluster/v3/cluster.upb.h"
33 #include "envoy/config/cluster/v3/cluster.upbdefs.h"
34 #include "envoy/config/cluster/v3/outlier_detection.upb.h"
35 #include "envoy/config/core/v3/address.upb.h"
36 #include "envoy/config/core/v3/base.upb.h"
37 #include "envoy/config/core/v3/config_source.upb.h"
38 #include "envoy/config/endpoint/v3/endpoint.upb.h"
39 #include "envoy/config/endpoint/v3/endpoint_components.upb.h"
40 #include "envoy/extensions/clusters/aggregate/v3/cluster.upb.h"
41 #include "envoy/extensions/transport_sockets/tls/v3/tls.upb.h"
42 #include "google/protobuf/any.upb.h"
43 #include "google/protobuf/duration.upb.h"
44 #include "google/protobuf/wrappers.upb.h"
45 #include "upb/text_encode.h"
46 #include "upb/upb.h"
47
48 #include <grpc/support/log.h>
49
50 #include "src/core/ext/xds/upb_utils.h"
51 #include "src/core/ext/xds/xds_client.h"
52 #include "src/core/ext/xds/xds_common_types.h"
53 #include "src/core/ext/xds/xds_lb_policy_registry.h"
54 #include "src/core/ext/xds/xds_resource_type.h"
55 #include "src/core/lib/config/core_configuration.h"
56 #include "src/core/lib/debug/trace.h"
57 #include "src/core/lib/gpr/string.h"
58 #include "src/core/lib/gprpp/env.h"
59 #include "src/core/lib/gprpp/host_port.h"
60 #include "src/core/lib/gprpp/ref_counted_ptr.h"
61 #include "src/core/lib/gprpp/time.h"
62 #include "src/core/lib/gprpp/validation_errors.h"
63 #include "src/core/lib/load_balancing/lb_policy_registry.h"
64 #include "src/core/lib/matchers/matchers.h"
65
66 namespace grpc_core {
67
68 // TODO(roth): Remove once custom LB policy support is no longer experimental.
69 bool XdsCustomLbPolicyEnabled() {
70 auto value = GetEnv("GRPC_EXPERIMENTAL_XDS_CUSTOM_LB_CONFIG");
71 if (!value.has_value()) return false;
72 bool parsed_value;
73 bool parse_succeeded = gpr_parse_bool_value(value->c_str(), &parsed_value);
74 return parse_succeeded && parsed_value;
75 }
76
77 //
78 // XdsClusterResource
79 //
80
81 std::string XdsClusterResource::ToString() const {
82 std::vector<std::string> contents;
83 switch (cluster_type) {
84 case EDS:
85 contents.push_back("cluster_type=EDS");
86 if (!eds_service_name.empty()) {
87 contents.push_back(absl::StrCat("eds_service_name=", eds_service_name));
88 }
89 break;
90 case LOGICAL_DNS:
91 contents.push_back("cluster_type=LOGICAL_DNS");
92 contents.push_back(absl::StrCat("dns_hostname=", dns_hostname));
93 break;
94 case AGGREGATE:
95 contents.push_back("cluster_type=AGGREGATE");
96 contents.push_back(
97 absl::StrCat("prioritized_cluster_names=[",
98 absl::StrJoin(prioritized_cluster_names, ", "), "]"));
99 }
100 if (!common_tls_context.Empty()) {
101 contents.push_back(
102 absl::StrCat("common_tls_context=", common_tls_context.ToString()));
103 }
104 if (lrs_load_reporting_server.has_value()) {
105 contents.push_back(absl::StrCat("lrs_load_reporting_server_name=",
106 lrs_load_reporting_server->server_uri()));
107 }
108 contents.push_back(
109 absl::StrCat("lb_policy_config=", Json{lb_policy_config}.Dump()));
110 contents.push_back(
111 absl::StrCat("max_concurrent_requests=", max_concurrent_requests));
112 return absl::StrCat("{", absl::StrJoin(contents, ", "), "}");
113 }
114
115 //
116 // XdsClusterResourceType
117 //
118
119 namespace {
120
121 CommonTlsContext UpstreamTlsContextParse(
122 const XdsResourceType::DecodeContext& context,
123 const envoy_config_core_v3_TransportSocket* transport_socket,
124 ValidationErrors* errors) {
125 ValidationErrors::ScopedField field(errors, ".typed_config");
126 const auto* typed_config =
127 envoy_config_core_v3_TransportSocket_typed_config(transport_socket);
128 auto extension = ExtractXdsExtension(context, typed_config, errors);
129 if (!extension.has_value()) return {};
130 if (extension->type !=
131 "envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext") {
132 ValidationErrors::ScopedField field(errors, ".type_url");
133 errors->AddError("unsupported transport socket type");
134 return {};
135 }
136 absl::string_view* serialized_upstream_tls_context =
137 absl::get_if<absl::string_view>(&extension->value);
138 if (serialized_upstream_tls_context == nullptr) {
139 errors->AddError("can't decode UpstreamTlsContext");
140 return {};
141 }
142 const auto* upstream_tls_context =
143 envoy_extensions_transport_sockets_tls_v3_UpstreamTlsContext_parse(
144 serialized_upstream_tls_context->data(),
145 serialized_upstream_tls_context->size(), context.arena);
146 if (upstream_tls_context == nullptr) {
147 errors->AddError("can't decode UpstreamTlsContext");
148 return {};
149 }
150 ValidationErrors::ScopedField field3(errors, ".common_tls_context");
151 const auto* common_tls_context_proto =
152 envoy_extensions_transport_sockets_tls_v3_UpstreamTlsContext_common_tls_context(
153 upstream_tls_context);
154 CommonTlsContext common_tls_context;
155 if (common_tls_context_proto != nullptr) {
156 common_tls_context =
157 CommonTlsContext::Parse(context, common_tls_context_proto, errors);
158 }
159 if (common_tls_context.certificate_validation_context
160 .ca_certificate_provider_instance.instance_name.empty()) {
161 errors->AddError("no CA certificate provider instance configured");
162 }
163 return common_tls_context;
164 }
165
166 void EdsConfigParse(const envoy_config_cluster_v3_Cluster* cluster,
167 XdsClusterResource* cds_update, ValidationErrors* errors) {
168 ValidationErrors::ScopedField field(errors, ".eds_cluster_config");
169 const envoy_config_cluster_v3_Cluster_EdsClusterConfig* eds_cluster_config =
170 envoy_config_cluster_v3_Cluster_eds_cluster_config(cluster);
171 if (eds_cluster_config == nullptr) {
172 errors->AddError("field not present");
173 } else {
174 ValidationErrors::ScopedField field(errors, ".eds_config");
175 const envoy_config_core_v3_ConfigSource* eds_config =
176 envoy_config_cluster_v3_Cluster_EdsClusterConfig_eds_config(
177 eds_cluster_config);
178 if (eds_config == nullptr) {
179 errors->AddError("field not present");
180 } else {
181 if (!envoy_config_core_v3_ConfigSource_has_ads(eds_config) &&
182 !envoy_config_core_v3_ConfigSource_has_self(eds_config)) {
183 errors->AddError("ConfigSource is not ads or self");
184 }
185 // Record EDS service_name (if any).
186 upb_StringView service_name =
187 envoy_config_cluster_v3_Cluster_EdsClusterConfig_service_name(
188 eds_cluster_config);
189 if (service_name.size != 0) {
190 cds_update->eds_service_name = UpbStringToStdString(service_name);
191 }
192 }
193 }
194 }
195
196 void LogicalDnsParse(const envoy_config_cluster_v3_Cluster* cluster,
197 XdsClusterResource* cds_update, ValidationErrors* errors) {
198 ValidationErrors::ScopedField field(errors, ".load_assignment");
199 const auto* load_assignment =
200 envoy_config_cluster_v3_Cluster_load_assignment(cluster);
201 if (load_assignment == nullptr) {
202 errors->AddError("field not present for LOGICAL_DNS cluster");
203 return;
204 }
205 ValidationErrors::ScopedField field2(errors, ".endpoints");
206 size_t num_localities;
207 const auto* const* localities =
208 envoy_config_endpoint_v3_ClusterLoadAssignment_endpoints(load_assignment,
209 &num_localities);
210 if (num_localities != 1) {
211 errors->AddError(absl::StrCat(
212 "must contain exactly one locality for LOGICAL_DNS cluster, found ",
213 num_localities));
214 return;
215 }
216 ValidationErrors::ScopedField field3(errors, "[0].lb_endpoints");
217 size_t num_endpoints;
218 const auto* const* endpoints =
219 envoy_config_endpoint_v3_LocalityLbEndpoints_lb_endpoints(localities[0],
220 &num_endpoints);
221 if (num_endpoints != 1) {
222 errors->AddError(absl::StrCat(
223 "must contain exactly one endpoint for LOGICAL_DNS cluster, found ",
224 num_endpoints));
225 return;
226 }
227 ValidationErrors::ScopedField field4(errors, "[0].endpoint");
228 const auto* endpoint =
229 envoy_config_endpoint_v3_LbEndpoint_endpoint(endpoints[0]);
230 if (endpoint == nullptr) {
231 errors->AddError("field not present");
232 return;
233 }
234 ValidationErrors::ScopedField field5(errors, ".address");
235 const auto* address = envoy_config_endpoint_v3_Endpoint_address(endpoint);
236 if (address == nullptr) {
237 errors->AddError("field not present");
238 return;
239 }
240 ValidationErrors::ScopedField field6(errors, ".socket_address");
241 const auto* socket_address =
242 envoy_config_core_v3_Address_socket_address(address);
243 if (socket_address == nullptr) {
244 errors->AddError("field not present");
245 return;
246 }
247 if (envoy_config_core_v3_SocketAddress_resolver_name(socket_address).size !=
248 0) {
249 ValidationErrors::ScopedField field(errors, ".resolver_name");
250 errors->AddError(
251 "LOGICAL_DNS clusters must NOT have a custom resolver name set");
252 }
253 absl::string_view address_str = UpbStringToAbsl(
254 envoy_config_core_v3_SocketAddress_address(socket_address));
255 if (address_str.empty()) {
256 ValidationErrors::ScopedField field(errors, ".address");
257 errors->AddError("field not present");
258 }
259 if (!envoy_config_core_v3_SocketAddress_has_port_value(socket_address)) {
260 ValidationErrors::ScopedField field(errors, ".port_value");
261 errors->AddError("field not present");
262 }
263 cds_update->dns_hostname = JoinHostPort(
264 address_str,
265 envoy_config_core_v3_SocketAddress_port_value(socket_address));
266 }
267
268 void AggregateClusterParse(const XdsResourceType::DecodeContext& context,
269 absl::string_view serialized_config,
270 XdsClusterResource* cds_update,
271 ValidationErrors* errors) {
272 const auto* aggregate_cluster_config =
273 envoy_extensions_clusters_aggregate_v3_ClusterConfig_parse(
274 serialized_config.data(), serialized_config.size(), context.arena);
275 if (aggregate_cluster_config == nullptr) {
276 errors->AddError("can't parse aggregate cluster config");
277 return;
278 }
279 size_t size;
280 const upb_StringView* clusters =
281 envoy_extensions_clusters_aggregate_v3_ClusterConfig_clusters(
282 aggregate_cluster_config, &size);
283 for (size_t i = 0; i < size; ++i) {
284 cds_update->prioritized_cluster_names.emplace_back(
285 UpbStringToStdString(clusters[i]));
286 }
287 }
288
289 void ParseLbPolicyConfig(const XdsResourceType::DecodeContext& context,
290 const envoy_config_cluster_v3_Cluster* cluster,
291 XdsClusterResource* cds_update,
292 ValidationErrors* errors) {
293 // First, check the new load_balancing_policy field.
294 if (XdsCustomLbPolicyEnabled()) {
295 const auto* load_balancing_policy =
296 envoy_config_cluster_v3_Cluster_load_balancing_policy(cluster);
297 if (load_balancing_policy != nullptr) {
298 const auto& registry =
299 static_cast<const GrpcXdsBootstrap&>(context.client->bootstrap())
300 .lb_policy_registry();
301 ValidationErrors::ScopedField field(errors, ".load_balancing_policy");
302 const size_t original_error_count = errors->size();
303 cds_update->lb_policy_config = registry.ConvertXdsLbPolicyConfig(
304 context, load_balancing_policy, errors);
305 // If there were no conversion errors, validate that the converted config
306 // parses with the gRPC LB policy registry.
307 if (original_error_count == errors->size()) {
308 auto config =
309 CoreConfiguration::Get()
310 .lb_policy_registry()
311 .ParseLoadBalancingConfig(cds_update->lb_policy_config);
312 if (!config.ok()) errors->AddError(config.status().message());
313 }
314 return;
315 }
316 }
317 // Didn't find load_balancing_policy field, so fall back to the old
318 // lb_policy enum field.
319 if (envoy_config_cluster_v3_Cluster_lb_policy(cluster) ==
320 envoy_config_cluster_v3_Cluster_ROUND_ROBIN) {
321 cds_update->lb_policy_config = {
322 Json::Object{
323 {"xds_wrr_locality_experimental",
324 Json::Object{
325 {"childPolicy",
326 Json::Array{
327 Json::Object{
328 {"round_robin", Json::Object()},
329 },
330 }},
331 }},
332 },
333 };
334 } else if (envoy_config_cluster_v3_Cluster_lb_policy(cluster) ==
335 envoy_config_cluster_v3_Cluster_RING_HASH) {
336 // Record ring hash lb config
337 auto* ring_hash_config =
338 envoy_config_cluster_v3_Cluster_ring_hash_lb_config(cluster);
339 uint64_t min_ring_size = 1024;
340 uint64_t max_ring_size = 8388608;
341 if (ring_hash_config != nullptr) {
342 ValidationErrors::ScopedField field(errors, ".ring_hash_lb_config");
343 const google_protobuf_UInt64Value* uint64_value =
344 envoy_config_cluster_v3_Cluster_RingHashLbConfig_maximum_ring_size(
345 ring_hash_config);
346 if (uint64_value != nullptr) {
347 ValidationErrors::ScopedField field(errors, ".maximum_ring_size");
348 max_ring_size = google_protobuf_UInt64Value_value(uint64_value);
349 if (max_ring_size > 8388608 || max_ring_size == 0) {
350 errors->AddError("must be in the range of 1 to 8388608");
351 }
352 }
353 uint64_value =
354 envoy_config_cluster_v3_Cluster_RingHashLbConfig_minimum_ring_size(
355 ring_hash_config);
356 if (uint64_value != nullptr) {
357 ValidationErrors::ScopedField field(errors, ".minimum_ring_size");
358 min_ring_size = google_protobuf_UInt64Value_value(uint64_value);
359 if (min_ring_size > 8388608 || min_ring_size == 0) {
360 errors->AddError("must be in the range of 1 to 8388608");
361 }
362 if (min_ring_size > max_ring_size) {
363 errors->AddError("cannot be greater than maximum_ring_size");
364 }
365 }
366 if (envoy_config_cluster_v3_Cluster_RingHashLbConfig_hash_function(
367 ring_hash_config) !=
368 envoy_config_cluster_v3_Cluster_RingHashLbConfig_XX_HASH) {
369 ValidationErrors::ScopedField field(errors, ".hash_function");
370 errors->AddError("invalid hash function");
371 }
372 }
373 cds_update->lb_policy_config = {
374 Json::Object{
375 {"ring_hash_experimental",
376 Json::Object{
377 {"minRingSize", min_ring_size},
378 {"maxRingSize", max_ring_size},
379 }},
380 },
381 };
382 } else {
383 ValidationErrors::ScopedField field(errors, ".lb_policy");
384 errors->AddError("LB policy is not supported");
385 }
386 }
387
388 absl::StatusOr<XdsClusterResource> CdsResourceParse(
389 const XdsResourceType::DecodeContext& context,
390 const envoy_config_cluster_v3_Cluster* cluster) {
391 XdsClusterResource cds_update;
392 ValidationErrors errors;
393 // Check the cluster discovery type.
394 if (envoy_config_cluster_v3_Cluster_type(cluster) ==
395 envoy_config_cluster_v3_Cluster_EDS) {
396 cds_update.cluster_type = XdsClusterResource::ClusterType::EDS;
397 EdsConfigParse(cluster, &cds_update, &errors);
398 } else if (envoy_config_cluster_v3_Cluster_type(cluster) ==
399 envoy_config_cluster_v3_Cluster_LOGICAL_DNS) {
400 cds_update.cluster_type = XdsClusterResource::ClusterType::LOGICAL_DNS;
401 LogicalDnsParse(cluster, &cds_update, &errors);
402 } else if (envoy_config_cluster_v3_Cluster_has_cluster_type(cluster)) {
403 ValidationErrors::ScopedField field(&errors, ".cluster_type");
404 const auto* custom_cluster_type =
405 envoy_config_cluster_v3_Cluster_cluster_type(cluster);
406 GPR_ASSERT(custom_cluster_type != nullptr);
407 ValidationErrors::ScopedField field2(&errors, ".typed_config");
408 const auto* typed_config =
409 envoy_config_cluster_v3_Cluster_CustomClusterType_typed_config(
410 custom_cluster_type);
411 if (typed_config == nullptr) {
412 errors.AddError("field not present");
413 } else {
414 absl::string_view type_url = absl::StripPrefix(
415 UpbStringToAbsl(google_protobuf_Any_type_url(typed_config)),
416 "type.googleapis.com/");
417 if (type_url != "envoy.extensions.clusters.aggregate.v3.ClusterConfig") {
418 ValidationErrors::ScopedField field(&errors, ".type_url");
419 errors.AddError(
420 absl::StrCat("unknown cluster_type extension: ", type_url));
421 } else {
422 cds_update.cluster_type = XdsClusterResource::ClusterType::AGGREGATE;
423 // Retrieve aggregate clusters.
424 ValidationErrors::ScopedField field(
425 &errors,
426 ".value[envoy.extensions.clusters.aggregate.v3.ClusterConfig]");
427 absl::string_view serialized_config =
428 UpbStringToAbsl(google_protobuf_Any_value(typed_config));
429 AggregateClusterParse(context, serialized_config, &cds_update, &errors);
430 }
431 }
432 } else {
433 ValidationErrors::ScopedField field(&errors, ".type");
434 errors.AddError("unknown discovery type");
435 }
436 // Check the LB policy.
437 ParseLbPolicyConfig(context, cluster, &cds_update, &errors);
438 // transport_socket
439 auto* transport_socket =
440 envoy_config_cluster_v3_Cluster_transport_socket(cluster);
441 if (transport_socket != nullptr) {
442 ValidationErrors::ScopedField field(&errors, ".transport_socket");
443 cds_update.common_tls_context =
444 UpstreamTlsContextParse(context, transport_socket, &errors);
445 }
446 // Record LRS server name (if any).
447 const envoy_config_core_v3_ConfigSource* lrs_server =
448 envoy_config_cluster_v3_Cluster_lrs_server(cluster);
449 if (lrs_server != nullptr) {
450 if (!envoy_config_core_v3_ConfigSource_has_self(lrs_server)) {
451 ValidationErrors::ScopedField field(&errors, ".lrs_server");
452 errors.AddError("ConfigSource is not self");
453 }
454 cds_update.lrs_load_reporting_server.emplace(
455 static_cast<const GrpcXdsBootstrap::GrpcXdsServer&>(context.server));
456 }
457 // The Cluster resource encodes the circuit breaking parameters in a list of
458 // Thresholds messages, where each message specifies the parameters for a
459 // particular RoutingPriority. we will look only at the first entry in the
460 // list for priority DEFAULT and default to 1024 if not found.
461 if (envoy_config_cluster_v3_Cluster_has_circuit_breakers(cluster)) {
462 const envoy_config_cluster_v3_CircuitBreakers* circuit_breakers =
463 envoy_config_cluster_v3_Cluster_circuit_breakers(cluster);
464 size_t num_thresholds;
465 const envoy_config_cluster_v3_CircuitBreakers_Thresholds* const*
466 thresholds = envoy_config_cluster_v3_CircuitBreakers_thresholds(
467 circuit_breakers, &num_thresholds);
468 for (size_t i = 0; i < num_thresholds; ++i) {
469 const auto* threshold = thresholds[i];
470 if (envoy_config_cluster_v3_CircuitBreakers_Thresholds_priority(
471 threshold) == envoy_config_core_v3_DEFAULT) {
472 const google_protobuf_UInt32Value* max_requests =
473 envoy_config_cluster_v3_CircuitBreakers_Thresholds_max_requests(
474 threshold);
475 if (max_requests != nullptr) {
476 cds_update.max_concurrent_requests =
477 google_protobuf_UInt32Value_value(max_requests);
478 }
479 break;
480 }
481 }
482 }
483 // Outlier detection config.
484 if (envoy_config_cluster_v3_Cluster_has_outlier_detection(cluster)) {
485 ValidationErrors::ScopedField field(&errors, ".outlier_detection");
486 OutlierDetectionConfig outlier_detection_update;
487 const envoy_config_cluster_v3_OutlierDetection* outlier_detection =
488 envoy_config_cluster_v3_Cluster_outlier_detection(cluster);
489 const google_protobuf_Duration* duration =
490 envoy_config_cluster_v3_OutlierDetection_interval(outlier_detection);
491 if (duration != nullptr) {
492 ValidationErrors::ScopedField field(&errors, ".interval");
493 outlier_detection_update.interval = ParseDuration(duration, &errors);
494 }
495 duration = envoy_config_cluster_v3_OutlierDetection_base_ejection_time(
496 outlier_detection);
497 if (duration != nullptr) {
498 ValidationErrors::ScopedField field(&errors, ".base_ejection_time");
499 outlier_detection_update.base_ejection_time =
500 ParseDuration(duration, &errors);
501 }
502 duration = envoy_config_cluster_v3_OutlierDetection_max_ejection_time(
503 outlier_detection);
504 if (duration != nullptr) {
505 ValidationErrors::ScopedField field(&errors, ".max_ejection_time");
506 outlier_detection_update.max_ejection_time =
507 ParseDuration(duration, &errors);
508 }
509 const google_protobuf_UInt32Value* max_ejection_percent =
510 envoy_config_cluster_v3_OutlierDetection_max_ejection_percent(
511 outlier_detection);
512 if (max_ejection_percent != nullptr) {
513 outlier_detection_update.max_ejection_percent =
514 google_protobuf_UInt32Value_value(max_ejection_percent);
515 if (outlier_detection_update.max_ejection_percent > 100) {
516 ValidationErrors::ScopedField field(&errors, ".max_ejection_percent");
517 errors.AddError("value must be <= 100");
518 }
519 }
520 const google_protobuf_UInt32Value* enforcing_success_rate =
521 envoy_config_cluster_v3_OutlierDetection_enforcing_success_rate(
522 outlier_detection);
523 if (enforcing_success_rate != nullptr) {
524 uint32_t enforcement_percentage =
525 google_protobuf_UInt32Value_value(enforcing_success_rate);
526 if (enforcement_percentage > 100) {
527 ValidationErrors::ScopedField field(&errors, ".enforcing_success_rate");
528 errors.AddError("value must be <= 100");
529 }
530 if (enforcement_percentage != 0) {
531 OutlierDetectionConfig::SuccessRateEjection success_rate_ejection;
532 success_rate_ejection.enforcement_percentage = enforcement_percentage;
533 const google_protobuf_UInt32Value* minimum_hosts =
534 envoy_config_cluster_v3_OutlierDetection_success_rate_minimum_hosts(
535 outlier_detection);
536 if (minimum_hosts != nullptr) {
537 success_rate_ejection.minimum_hosts =
538 google_protobuf_UInt32Value_value(minimum_hosts);
539 }
540 const google_protobuf_UInt32Value* request_volume =
541 envoy_config_cluster_v3_OutlierDetection_success_rate_request_volume(
542 outlier_detection);
543 if (request_volume != nullptr) {
544 success_rate_ejection.request_volume =
545 google_protobuf_UInt32Value_value(request_volume);
546 }
547 const google_protobuf_UInt32Value* stdev_factor =
548 envoy_config_cluster_v3_OutlierDetection_success_rate_stdev_factor(
549 outlier_detection);
550 if (stdev_factor != nullptr) {
551 success_rate_ejection.stdev_factor =
552 google_protobuf_UInt32Value_value(stdev_factor);
553 }
554 outlier_detection_update.success_rate_ejection = success_rate_ejection;
555 }
556 }
557 const google_protobuf_UInt32Value* enforcing_failure_percentage =
558 envoy_config_cluster_v3_OutlierDetection_enforcing_failure_percentage(
559 outlier_detection);
560 if (enforcing_failure_percentage != nullptr) {
561 uint32_t enforcement_percentage =
562 google_protobuf_UInt32Value_value(enforcing_failure_percentage);
563 if (enforcement_percentage > 100) {
564 ValidationErrors::ScopedField field(&errors,
565 ".enforcing_failure_percentage");
566 errors.AddError("value must be <= 100");
567 }
568 if (enforcement_percentage != 0) {
569 OutlierDetectionConfig::FailurePercentageEjection
570 failure_percentage_ejection;
571 failure_percentage_ejection.enforcement_percentage =
572 enforcement_percentage;
573 const google_protobuf_UInt32Value* minimum_hosts =
574 envoy_config_cluster_v3_OutlierDetection_failure_percentage_minimum_hosts(
575 outlier_detection);
576 if (minimum_hosts != nullptr) {
577 failure_percentage_ejection.minimum_hosts =
578 google_protobuf_UInt32Value_value(minimum_hosts);
579 }
580 const google_protobuf_UInt32Value* request_volume =
581 envoy_config_cluster_v3_OutlierDetection_failure_percentage_request_volume(
582 outlier_detection);
583 if (request_volume != nullptr) {
584 failure_percentage_ejection.request_volume =
585 google_protobuf_UInt32Value_value(request_volume);
586 }
587 const google_protobuf_UInt32Value* threshold =
588 envoy_config_cluster_v3_OutlierDetection_failure_percentage_threshold(
589 outlier_detection);
590 if (threshold != nullptr) {
591 failure_percentage_ejection.threshold =
592 google_protobuf_UInt32Value_value(threshold);
593 if (enforcement_percentage > 100) {
594 ValidationErrors::ScopedField field(
595 &errors, ".failure_percentage_threshold");
596 errors.AddError("value must be <= 100");
597 }
598 }
599 outlier_detection_update.failure_percentage_ejection =
600 failure_percentage_ejection;
601 }
602 }
603 cds_update.outlier_detection = outlier_detection_update;
604 }
605 // Return result.
606 if (!errors.ok()) return errors.status("errors validating Cluster resource");
607 return cds_update;
608 }
609
610 void MaybeLogCluster(const XdsResourceType::DecodeContext& context,
611 const envoy_config_cluster_v3_Cluster* cluster) {
612 if (GRPC_TRACE_FLAG_ENABLED(*context.tracer) &&
613 gpr_should_log(GPR_LOG_SEVERITY_DEBUG)) {
614 const upb_MessageDef* msg_type =
615 envoy_config_cluster_v3_Cluster_getmsgdef(context.symtab);
616 char buf[10240];
617 upb_TextEncode(cluster, msg_type, nullptr, 0, buf, sizeof(buf));
618 gpr_log(GPR_DEBUG, "[xds_client %p] Cluster: %s", context.client, buf);
619 }
620 }
621
622 } // namespace
623
624 XdsResourceType::DecodeResult XdsClusterResourceType::Decode(
625 const XdsResourceType::DecodeContext& context,
626 absl::string_view serialized_resource) const {
627 DecodeResult result;
628 // Parse serialized proto.
629 auto* resource = envoy_config_cluster_v3_Cluster_parse(
630 serialized_resource.data(), serialized_resource.size(), context.arena);
631 if (resource == nullptr) {
632 result.resource =
633 absl::InvalidArgumentError("Can't parse Cluster resource.");
634 return result;
635 }
636 MaybeLogCluster(context, resource);
637 // Validate resource.
638 result.name =
639 UpbStringToStdString(envoy_config_cluster_v3_Cluster_name(resource));
640 auto cds_resource = CdsResourceParse(context, resource);
641 if (!cds_resource.ok()) {
642 if (GRPC_TRACE_FLAG_ENABLED(*context.tracer)) {
643 gpr_log(GPR_ERROR, "[xds_client %p] invalid Cluster %s: %s",
644 context.client, result.name->c_str(),
645 cds_resource.status().ToString().c_str());
646 }
647 result.resource = cds_resource.status();
648 } else {
649 if (GRPC_TRACE_FLAG_ENABLED(*context.tracer)) {
650 gpr_log(GPR_INFO, "[xds_client %p] parsed Cluster %s: %s", context.client,
651 result.name->c_str(), cds_resource->ToString().c_str());
652 }
653 result.resource =
654 std::make_unique<XdsClusterResource>(std::move(*cds_resource));
655 }
656 return result;
657 }
658
659 } // namespace grpc_core