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