| 1 |
// |
| 2 |
// Copyright 2022 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_specifier_plugin.h" |
| 20 |
|
| 21 |
#include <stddef.h> |
| 22 |
|
| 23 |
#include <algorithm> |
| 24 |
#include <map> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#include "absl/status/status.h" |
| 28 |
#include "absl/strings/str_cat.h" |
| 29 |
#include "absl/types/variant.h" |
| 30 |
#include "upb/json_encode.h" |
| 31 |
#include "upb/status.h" |
| 32 |
#include "upb/upb.hpp" |
| 33 |
|
| 34 |
#include <grpc/support/log.h> |
| 35 |
|
| 36 |
#include "src/core/lib/config/core_configuration.h" |
| 37 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 38 |
#include "src/core/lib/json/json.h" |
| 39 |
#include "src/core/lib/load_balancing/lb_policy_registry.h" |
| 40 |
#include "src/proto/grpc/lookup/v1/rls_config.upb.h" |
| 41 |
#include "src/proto/grpc/lookup/v1/rls_config.upbdefs.h" |
| 42 |
|
| 43 |
namespace grpc_core { |
| 44 |
|
| 45 |
// |
| 46 |
// XdsRouteLookupClusterSpecifierPlugin |
| 47 |
// |
| 48 |
|
| 49 |
const char* kXdsRouteLookupClusterSpecifierPluginConfigName = |
| 50 |
"grpc.lookup.v1.RouteLookupClusterSpecifier"; |
| 51 |
|
| 52 |
void XdsRouteLookupClusterSpecifierPlugin::PopulateSymtab( |
| 53 |
upb_DefPool* symtab) const { |
| 54 |
grpc_lookup_v1_RouteLookupConfig_getmsgdef(symtab); |
| 55 |
} |
| 56 |
|
| 57 |
absl::StatusOr<std::string> |
| 58 |
XdsRouteLookupClusterSpecifierPlugin::GenerateLoadBalancingPolicyConfig( |
| 59 |
XdsExtension extension, upb_Arena* arena, upb_DefPool* symtab) const { |
| 60 |
absl::string_view* serialized_plugin_config = |
| 61 |
absl::get_if<absl::string_view>(&extension.value); |
| 62 |
if (serialized_plugin_config == nullptr) { |
| 63 |
return absl::InvalidArgumentError("could not parse plugin config"); |
| 64 |
} |
| 65 |
const auto* specifier = grpc_lookup_v1_RouteLookupClusterSpecifier_parse( |
| 66 |
serialized_plugin_config->data(), serialized_plugin_config->size(), |
| 67 |
arena); |
| 68 |
if (specifier == nullptr) { |
| 69 |
return absl::InvalidArgumentError("Could not parse plugin config"); |
| 70 |
} |
| 71 |
const auto* plugin_config = |
| 72 |
grpc_lookup_v1_RouteLookupClusterSpecifier_route_lookup_config(specifier); |
| 73 |
if (plugin_config == nullptr) { |
| 74 |
return absl::InvalidArgumentError( |
| 75 |
"Could not get route lookup config from route lookup cluster " |
| 76 |
"specifier"); |
| 77 |
} |
| 78 |
upb::Status status; |
| 79 |
const upb_MessageDef* msg_type = |
| 80 |
grpc_lookup_v1_RouteLookupConfig_getmsgdef(symtab); |
| 81 |
size_t json_size = upb_JsonEncode(plugin_config, msg_type, symtab, 0, nullptr, |
| 82 |
0, status.ptr()); |
| 83 |
if (json_size == static_cast<size_t>(-1)) { |
| 84 |
return absl::InvalidArgumentError( |
| 85 |
absl::StrCat("failed to dump proto to JSON: ", |
| 86 |
upb_Status_ErrorMessage(status.ptr()))); |
| 87 |
} |
| 88 |
void* buf = upb_Arena_Malloc(arena, json_size + 1); |
| 89 |
upb_JsonEncode(plugin_config, msg_type, symtab, 0, |
| 90 |
reinterpret_cast<char*>(buf), json_size + 1, status.ptr()); |
| 91 |
Json::Object rls_policy; |
| 92 |
auto json = Json::Parse(reinterpret_cast<char*>(buf)); |
| 93 |
GPR_ASSERT(json.ok()); |
| 94 |
rls_policy["routeLookupConfig"] = std::move(*json); |
| 95 |
Json::Object cds_policy; |
| 96 |
cds_policy["cds_experimental"] = Json::Object(); |
| 97 |
Json::Array child_policy; |
| 98 |
child_policy.emplace_back(std::move(cds_policy)); |
| 99 |
rls_policy["childPolicy"] = std::move(child_policy); |
| 100 |
rls_policy["childPolicyConfigTargetFieldName"] = "cluster"; |
| 101 |
Json::Object policy; |
| 102 |
policy["rls_experimental"] = std::move(rls_policy); |
| 103 |
Json::Array policies; |
| 104 |
policies.emplace_back(std::move(policy)); |
| 105 |
Json lb_policy_config(std::move(policies)); |
| 106 |
// TODO(roth): If/when we ever add a second plugin, refactor this code |
| 107 |
// somehow such that we automatically validate the resulting config against |
| 108 |
// the gRPC LB policy registry instead of requiring each plugin to do that |
| 109 |
// itself. |
| 110 |
auto config = |
| 111 |
CoreConfiguration::Get().lb_policy_registry().ParseLoadBalancingConfig( |
| 112 |
lb_policy_config); |
| 113 |
if (!config.ok()) { |
| 114 |
return absl::InvalidArgumentError(absl::StrCat( |
| 115 |
kXdsRouteLookupClusterSpecifierPluginConfigName, |
| 116 |
" ClusterSpecifierPlugin returned invalid LB policy config: ", |
| 117 |
config.status().message())); |
| 118 |
} |
| 119 |
return lb_policy_config.Dump(); |
| 120 |
} |
| 121 |
|
| 122 |
// |
| 123 |
// XdsClusterSpecifierPluginRegistry |
| 124 |
// |
| 125 |
|
| 126 |
XdsClusterSpecifierPluginRegistry::XdsClusterSpecifierPluginRegistry() { |
| 127 |
RegisterPlugin(std::make_unique<XdsRouteLookupClusterSpecifierPlugin>(), |
| 128 |
kXdsRouteLookupClusterSpecifierPluginConfigName); |
| 129 |
} |
| 130 |
|
| 131 |
void XdsClusterSpecifierPluginRegistry::RegisterPlugin( |
| 132 |
std::unique_ptr<XdsClusterSpecifierPluginImpl> plugin, |
| 133 |
absl::string_view config_proto_type_name) { |
| 134 |
registry_[config_proto_type_name] = std::move(plugin); |
| 135 |
} |
| 136 |
|
| 137 |
const XdsClusterSpecifierPluginImpl* |
| 138 |
XdsClusterSpecifierPluginRegistry::GetPluginForType( |
| 139 |
absl::string_view config_proto_type_name) const { |
| 140 |
auto it = registry_.find(config_proto_type_name); |
| 141 |
if (it == registry_.end()) return nullptr; |
| 142 |
return it->second.get(); |
| 143 |
} |
| 144 |
|
| 145 |
void XdsClusterSpecifierPluginRegistry::PopulateSymtab( |
| 146 |
upb_DefPool* symtab) const { |
| 147 |
for (const auto& p : registry_) { |
| 148 |
p.second->PopulateSymtab(symtab); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
} // namespace grpc_core |