diff --git a/src/core/BUILD b/src/core/BUILD index 56417492e00..d7f76fbd352 100644 --- a/src/core/BUILD +++ b/src/core/BUILD @@ -4368,6 +4368,7 @@ grpc_cc_library( "//:ref_counted_ptr", "//:uri_parser", "//:work_serializer", + "//:xds_client", ], ) diff --git a/src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc b/src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc index 7b41afb8d99..9a8c4229d04 100644 --- a/src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc +++ b/src/core/ext/filters/client_channel/resolver/google_c2p/google_c2p_resolver.cc @@ -38,6 +38,7 @@ #include #include +#include "src/core/ext/xds/xds_bootstrap.h" #include "src/core/ext/xds/xds_client_grpc.h" #include "src/core/lib/channel/channel_args.h" #include "src/core/lib/config/core_configuration.h" @@ -66,6 +67,8 @@ namespace grpc_core { namespace { +const char* kC2PAuthority = "traffic-director-c2p.xds.googleapis.com"; + class GoogleCloud2ProdResolver : public Resolver { public: explicit GoogleCloud2ProdResolver(ResolverArgs args); @@ -261,6 +264,11 @@ void GoogleCloud2ProdResolver::IPv6Query::OnDone( // GoogleCloud2ProdResolver // +bool XdsBootstrapConfigured() { + return GetEnv("GRPC_XDS_BOOTSTRAP").has_value() || + GetEnv("GRPC_XDS_BOOTSTRAP_CONFIG").has_value(); +} + GoogleCloud2ProdResolver::GoogleCloud2ProdResolver(ResolverArgs args) : resource_quota_(args.args.GetObjectRef()), work_serializer_(std::move(args.work_serializer)), @@ -274,18 +282,19 @@ GoogleCloud2ProdResolver::GoogleCloud2ProdResolver(ResolverArgs args) .value_or(false); const bool running_on_gcp = test_only_pretend_running_on_gcp || grpc_alts_is_running_on_gcp(); + const bool federation_enabled = XdsFederationEnabled(); if (!running_on_gcp || - // If the client is already using xDS, we can't use it here, because - // they may be talking to a completely different xDS server than we - // want to. - // TODO(roth): When we implement xDS federation, remove this constraint. - GetEnv("GRPC_XDS_BOOTSTRAP").has_value() || - GetEnv("GRPC_XDS_BOOTSTRAP_CONFIG").has_value()) { + // If the client is already using xDS and federation is not enabled, + // we can't use it here, because they may be talking to a completely + // different xDS server than we want to. + // TODO(roth): When we remove xDS federation env var protection, + // remove this constraint. + (!federation_enabled && XdsBootstrapConfigured())) { using_dns_ = true; child_resolver_ = CoreConfiguration::Get().resolver_registry().CreateResolver( - absl::StrCat("dns:", name_to_resolve).c_str(), args.args, - args.pollset_set, work_serializer_, std::move(args.result_handler)); + absl::StrCat("dns:", name_to_resolve), args.args, args.pollset_set, + work_serializer_, std::move(args.result_handler)); GPR_ASSERT(child_resolver_ != nullptr); return; } @@ -298,9 +307,13 @@ GoogleCloud2ProdResolver::GoogleCloud2ProdResolver(ResolverArgs args) metadata_server_name_ = std::move(*test_only_metadata_server_override); } // Create xds resolver. + std::string xds_uri = + federation_enabled + ? absl::StrCat("xds://", kC2PAuthority, "/", name_to_resolve) + : absl::StrCat("xds:", name_to_resolve); child_resolver_ = CoreConfiguration::Get().resolver_registry().CreateResolver( - absl::StrCat("xds:", name_to_resolve).c_str(), args.args, - args.pollset_set, work_serializer_, std::move(args.result_handler)); + xds_uri, args.args, args.pollset_set, work_serializer_, + std::move(args.result_handler)); GPR_ASSERT(child_resolver_ != nullptr); } @@ -389,7 +402,7 @@ void GoogleCloud2ProdResolver::StartXdsResolver() { {"xds_servers", xds_server}, {"authorities", Json::Object{ - {"traffic-director-c2p.xds.googleapis.com", + {kC2PAuthority, Json::Object{ {"xds_servers", std::move(xds_server)}, }},