xref: /aosp_15_r20/external/grpc-grpc/src/core/ext/xds/xds_cluster.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 #ifndef GRPC_SRC_CORE_EXT_XDS_XDS_CLUSTER_H
18 #define GRPC_SRC_CORE_EXT_XDS_XDS_CLUSTER_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <stdint.h>
23 
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include "absl/strings/string_view.h"
29 #include "absl/types/optional.h"
30 #include "absl/types/variant.h"
31 #include "envoy/config/cluster/v3/cluster.upbdefs.h"
32 #include "envoy/extensions/clusters/aggregate/v3/cluster.upbdefs.h"
33 #include "envoy/extensions/transport_sockets/tls/v3/tls.upbdefs.h"
34 #include "envoy/extensions/upstreams/http/v3/http_protocol_options.upbdefs.h"
35 #include "upb/reflection/def.h"
36 
37 #include <grpc/support/json.h>
38 
39 #include "src/core/ext/xds/xds_bootstrap.h"
40 #include "src/core/ext/xds/xds_bootstrap_grpc.h"
41 #include "src/core/ext/xds/xds_client.h"
42 #include "src/core/ext/xds/xds_common_types.h"
43 #include "src/core/ext/xds/xds_health_status.h"
44 #include "src/core/ext/xds/xds_resource_type.h"
45 #include "src/core/ext/xds/xds_resource_type_impl.h"
46 #include "src/core/lib/json/json.h"
47 #include "src/core/load_balancing/outlier_detection/outlier_detection.h"
48 
49 namespace grpc_core {
50 
51 struct XdsClusterResource : public XdsResourceType::ResourceData {
52   struct Eds {
53     // If empty, defaults to the cluster name.
54     std::string eds_service_name;
55 
56     bool operator==(const Eds& other) const {
57       return eds_service_name == other.eds_service_name;
58     }
59   };
60 
61   struct LogicalDns {
62     // The hostname to lookup in DNS.
63     std::string hostname;
64 
65     bool operator==(const LogicalDns& other) const {
66       return hostname == other.hostname;
67     }
68   };
69 
70   struct Aggregate {
71     // Prioritized list of cluster names.
72     std::vector<std::string> prioritized_cluster_names;
73 
74     bool operator==(const Aggregate& other) const {
75       return prioritized_cluster_names == other.prioritized_cluster_names;
76     }
77   };
78 
79   absl::variant<Eds, LogicalDns, Aggregate> type;
80 
81   // The LB policy to use for locality and endpoint picking.
82   Json::Array lb_policy_config;
83 
84   // Note: Remaining fields are not used for aggregate clusters.
85 
86   // The LRS server to use for load reporting.
87   // If not set, load reporting will be disabled.
88   absl::optional<GrpcXdsBootstrap::GrpcXdsServer> lrs_load_reporting_server;
89 
90   // Tls Context used by clients
91   CommonTlsContext common_tls_context;
92 
93   // Connection idle timeout.  Currently used only for SSA.
94   Duration connection_idle_timeout = Duration::Hours(1);
95 
96   // Maximum number of outstanding requests can be made to the upstream
97   // cluster.
98   uint32_t max_concurrent_requests = 1024;
99 
100   absl::optional<OutlierDetectionConfig> outlier_detection;
101 
102   XdsHealthStatusSet override_host_statuses;
103 
104   RefCountedStringValue service_telemetry_label;
105   RefCountedStringValue namespace_telemetry_label;
106 
107   bool operator==(const XdsClusterResource& other) const {
108     return type == other.type && lb_policy_config == other.lb_policy_config &&
109            lrs_load_reporting_server == other.lrs_load_reporting_server &&
110            common_tls_context == other.common_tls_context &&
111            connection_idle_timeout == other.connection_idle_timeout &&
112            max_concurrent_requests == other.max_concurrent_requests &&
113            outlier_detection == other.outlier_detection &&
114            override_host_statuses == other.override_host_statuses &&
115            service_telemetry_label == other.service_telemetry_label &&
116            namespace_telemetry_label == other.namespace_telemetry_label;
117   }
118 
119   std::string ToString() const;
120 };
121 
122 class XdsClusterResourceType
123     : public XdsResourceTypeImpl<XdsClusterResourceType, XdsClusterResource> {
124  public:
type_url()125   absl::string_view type_url() const override {
126     return "envoy.config.cluster.v3.Cluster";
127   }
128 
129   DecodeResult Decode(const XdsResourceType::DecodeContext& context,
130                       absl::string_view serialized_resource) const override;
131 
AllResourcesRequiredInSotW()132   bool AllResourcesRequiredInSotW() const override { return true; }
133 
InitUpbSymtab(XdsClient *,upb_DefPool * symtab)134   void InitUpbSymtab(XdsClient*, upb_DefPool* symtab) const override {
135     envoy_config_cluster_v3_Cluster_getmsgdef(symtab);
136     envoy_extensions_clusters_aggregate_v3_ClusterConfig_getmsgdef(symtab);
137     envoy_extensions_transport_sockets_tls_v3_UpstreamTlsContext_getmsgdef(
138         symtab);
139     envoy_extensions_upstreams_http_v3_HttpProtocolOptions_getmsgdef(symtab);
140   }
141 };
142 
143 }  // namespace grpc_core
144 
145 #endif  // GRPC_SRC_CORE_EXT_XDS_XDS_CLUSTER_H
146