Debian Codegraph @

src/core/ext/xds/xds_bootstrap_grpc.h

raw
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_CORE_EXT_XDS_XDS_BOOTSTRAP_GRPC_H
18 #define GRPC_CORE_EXT_XDS_XDS_BOOTSTRAP_GRPC_H
19
20 #include <grpc/support/port_platform.h>
21
22 #include <map>
23 #include <memory>
24 #include <set>
25 #include <string>
26 #include <vector>
27
28 #include "absl/status/statusor.h"
29 #include "absl/strings/string_view.h"
30 #include "absl/types/optional.h"
31
32 #include "src/core/ext/xds/certificate_provider_store.h"
33 #include "src/core/ext/xds/xds_bootstrap.h"
34 #include "src/core/ext/xds/xds_cluster_specifier_plugin.h"
35 #include "src/core/ext/xds/xds_http_filters.h"
36 #include "src/core/ext/xds/xds_lb_policy_registry.h"
37 #include "src/core/lib/gprpp/validation_errors.h"
38 #include "src/core/lib/json/json.h"
39 #include "src/core/lib/json/json_args.h"
40 #include "src/core/lib/json/json_object_loader.h"
41
42 namespace grpc_core {
43
44 class GrpcXdsBootstrap : public XdsBootstrap {
45 public:
46 class GrpcNode : public Node {
47 public:
48 const std::string& id() const override { return id_; }
49 const std::string& cluster() const override { return cluster_; }
50 const std::string& locality_region() const override {
51 return locality_.region;
52 }
53 const std::string& locality_zone() const override { return locality_.zone; }
54 const std::string& locality_sub_zone() const override {
55 return locality_.sub_zone;
56 }
57 const Json::Object& metadata() const override { return metadata_; }
58
59 static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
60
61 private:
62 struct Locality {
63 std::string region;
64 std::string zone;
65 std::string sub_zone;
66
67 static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
68 };
69
70 std::string id_;
71 std::string cluster_;
72 Locality locality_;
73 Json::Object metadata_;
74 };
75
76 class GrpcXdsServer : public XdsServer {
77 public:
78 const std::string& server_uri() const override { return server_uri_; }
79
80 bool IgnoreResourceDeletion() const override;
81
82 bool Equals(const XdsServer& other) const override;
83
84 const std::string& channel_creds_type() const {
85 return channel_creds_.type;
86 }
87 const Json::Object& channel_creds_config() const {
88 return channel_creds_.config;
89 }
90
91 static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
92 void JsonPostLoad(const Json& json, const JsonArgs& args,
93 ValidationErrors* errors);
94
95 Json ToJson() const;
96
97 private:
98 struct ChannelCreds {
99 std::string type;
100 Json::Object config;
101
102 static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
103 };
104
105 std::string server_uri_;
106 ChannelCreds channel_creds_;
107 std::set<std::string> server_features_;
108 };
109
110 class GrpcAuthority : public Authority {
111 public:
112 const XdsServer* server() const override {
113 return servers_.empty() ? nullptr : &servers_[0];
114 }
115
116 const std::string& client_listener_resource_name_template() const {
117 return client_listener_resource_name_template_;
118 }
119
120 static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
121
122 private:
123 std::vector<GrpcXdsServer> servers_;
124 std::string client_listener_resource_name_template_;
125 };
126
127 // Creates bootstrap object from json_string.
128 static absl::StatusOr<std::unique_ptr<GrpcXdsBootstrap>> Create(
129 absl::string_view json_string);
130
131 static const JsonLoaderInterface* JsonLoader(const JsonArgs&);
132 void JsonPostLoad(const Json& json, const JsonArgs& args,
133 ValidationErrors* errors);
134
135 std::string ToString() const override;
136
137 const XdsServer& server() const override { return servers_[0]; }
138 const Node* node() const override {
139 return node_.has_value() ? &*node_ : nullptr;
140 }
141 const Authority* LookupAuthority(const std::string& name) const override;
142 const XdsServer* FindXdsServer(const XdsServer& server) const override;
143
144 const std::string& client_default_listener_resource_name_template() const {
145 return client_default_listener_resource_name_template_;
146 }
147 const std::string& server_listener_resource_name_template() const {
148 return server_listener_resource_name_template_;
149 }
150 const CertificateProviderStore::PluginDefinitionMap& certificate_providers()
151 const {
152 return certificate_providers_;
153 }
154 const XdsHttpFilterRegistry& http_filter_registry() const {
155 return http_filter_registry_;
156 }
157 const XdsClusterSpecifierPluginRegistry& cluster_specifier_plugin_registry()
158 const {
159 return cluster_specifier_plugin_registry_;
160 }
161 const XdsLbPolicyRegistry& lb_policy_registry() const {
162 return lb_policy_registry_;
163 }
164
165 // Exposed for testing purposes only.
166 const std::map<std::string, GrpcAuthority>& authorities() const {
167 return authorities_;
168 }
169
170 private:
171 std::vector<GrpcXdsServer> servers_;
172 absl::optional<GrpcNode> node_;
173 std::string client_default_listener_resource_name_template_;
174 std::string server_listener_resource_name_template_;
175 std::map<std::string, GrpcAuthority> authorities_;
176 CertificateProviderStore::PluginDefinitionMap certificate_providers_;
177 XdsHttpFilterRegistry http_filter_registry_;
178 XdsClusterSpecifierPluginRegistry cluster_specifier_plugin_registry_;
179 XdsLbPolicyRegistry lb_policy_registry_;
180 };
181
182 } // namespace grpc_core
183
184 #endif // GRPC_CORE_EXT_XDS_XDS_BOOTSTRAP_GRPC_H