• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2019 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_RESOLVER_XDS_XDS_CONFIG_H
18 #define GRPC_SRC_CORE_RESOLVER_XDS_XDS_CONFIG_H
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 #include "absl/container/flat_hash_map.h"
26 #include "absl/status/statusor.h"
27 #include "absl/strings/string_view.h"
28 #include "absl/types/variant.h"
29 #include "src/core/util/ref_counted.h"
30 #include "src/core/xds/grpc/xds_cluster.h"
31 #include "src/core/xds/grpc/xds_endpoint.h"
32 #include "src/core/xds/grpc/xds_listener.h"
33 #include "src/core/xds/grpc/xds_route_config.h"
34 
35 namespace grpc_core {
36 
37 // A complete gRPC client-side xDS config containing all necessary
38 // resources.
39 struct XdsConfig : public RefCounted<XdsConfig> {
40   // Listener resource.  Always non-null.
41   std::shared_ptr<const XdsListenerResource> listener;
42   // RouteConfig resource.  Will be populated even if RouteConfig is
43   // inlined into the Listener resource.
44   std::shared_ptr<const XdsRouteConfigResource> route_config;
45   // Virtual host.  Points into route_config.  Will always be non-null.
46   const XdsRouteConfigResource::VirtualHost* virtual_host;
47 
48   struct ClusterConfig {
49     // Cluster resource.  Always non-null.
50     std::shared_ptr<const XdsClusterResource> cluster;
51     // Endpoint info for EDS and LOGICAL_DNS clusters.  If there was an
52     // error, endpoints will be null and resolution_note will be set.
53     struct EndpointConfig {
54       std::shared_ptr<const XdsEndpointResource> endpoints;
55       std::string resolution_note;
56 
EndpointConfigXdsConfig::ClusterConfig::EndpointConfig57       EndpointConfig(std::shared_ptr<const XdsEndpointResource> endpoints,
58                      std::string resolution_note)
59           : endpoints(std::move(endpoints)),
60             resolution_note(std::move(resolution_note)) {}
61       bool operator==(const EndpointConfig& other) const {
62         return endpoints == other.endpoints &&
63                resolution_note == other.resolution_note;
64       }
65     };
66     // The list of leaf clusters for an aggregate cluster.
67     struct AggregateConfig {
68       std::vector<absl::string_view> leaf_clusters;
69 
AggregateConfigXdsConfig::ClusterConfig::AggregateConfig70       explicit AggregateConfig(std::vector<absl::string_view> leaf_clusters)
71           : leaf_clusters(std::move(leaf_clusters)) {}
72       bool operator==(const AggregateConfig& other) const {
73         return leaf_clusters == other.leaf_clusters;
74       }
75     };
76     absl::variant<EndpointConfig, AggregateConfig> children;
77 
78     // Ctor for leaf clusters.
79     ClusterConfig(std::shared_ptr<const XdsClusterResource> cluster,
80                   std::shared_ptr<const XdsEndpointResource> endpoints,
81                   std::string resolution_note);
82     // Ctor for aggregate clusters.
83     ClusterConfig(std::shared_ptr<const XdsClusterResource> cluster,
84                   std::vector<absl::string_view> leaf_clusters);
85 
86     bool operator==(const ClusterConfig& other) const {
87       return cluster == other.cluster && children == other.children;
88     }
89   };
90   // Cluster map.  A cluster will have a non-OK status if either
91   // (a) there was an error and we did not already have a valid
92   // resource or (b) the resource does not exist.
93   absl::flat_hash_map<std::string, absl::StatusOr<ClusterConfig>> clusters;
94 
95   std::string ToString() const;
96 
ChannelArgNameXdsConfig97   static absl::string_view ChannelArgName() {
98     return GRPC_ARG_NO_SUBCHANNEL_PREFIX "xds_config";
99   }
ChannelArgsCompareXdsConfig100   static int ChannelArgsCompare(const XdsConfig* a, const XdsConfig* b) {
101     return QsortCompare(a, b);
102   }
ChannelArgUseConstPtrXdsConfig103   static constexpr bool ChannelArgUseConstPtr() { return true; }
104 };
105 
106 }  // namespace grpc_core
107 
108 #endif  // GRPC_SRC_CORE_RESOLVER_XDS_XDS_CONFIG_H
109