@@ -30,7 +30,6 @@ import (
3030 "net/url"
3131 "time"
3232
33- "google.golang.org/grpc"
3433 "google.golang.org/grpc/grpclog"
3534 "google.golang.org/grpc/internal/envconfig"
3635 "google.golang.org/grpc/internal/googlecloud"
@@ -39,9 +38,6 @@ import (
3938 "google.golang.org/grpc/resolver"
4039 "google.golang.org/grpc/xds/internal/xdsclient"
4140 "google.golang.org/grpc/xds/internal/xdsclient/bootstrap"
42- "google.golang.org/protobuf/types/known/structpb"
43-
44- v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
4541
4642 _ "google.golang.org/grpc/xds" // To register xds resolvers and balancers.
4743)
@@ -57,6 +53,7 @@ const (
5753
5854 gRPCUserAgentName = "gRPC Go"
5955 clientFeatureNoOverprovisioning = "envoy.lb.does_not_support_overprovisioning"
56+ clientFeatureResourceWrapper = "xds.config.resource-in-sotw"
6057 ipv6CapableMetadataName = "TRAFFICDIRECTOR_DIRECTPATH_C2P_IPV6_CAPABLE"
6158
6259 logPrefix = "[google-c2p-resolver]"
@@ -102,30 +99,26 @@ func (c2pResolverBuilder) Build(t resolver.Target, cc resolver.ClientConn, opts
10299 go func () { zoneCh <- getZone (httpReqTimeout ) }()
103100 go func () { ipv6CapableCh <- getIPv6Capable (httpReqTimeout ) }()
104101
105- balancerName := envconfig .C2PResolverTestOnlyTrafficDirectorURI
106- if balancerName == "" {
107- balancerName = tdURL
102+ xdsServerURI := envconfig .C2PResolverTestOnlyTrafficDirectorURI
103+ if xdsServerURI == "" {
104+ xdsServerURI = tdURL
108105 }
109- serverConfig , err := bootstrap .ServerConfigFromJSON ([]byte (fmt .Sprintf (`
106+
107+ nodeCfg := newNodeConfig (<- zoneCh , <- ipv6CapableCh )
108+ xdsServerCfg := newXdsServerConfig (xdsServerURI )
109+ authoritiesCfg := newAuthoritiesConfig (xdsServerCfg )
110+
111+ config , err := bootstrap .NewConfigFromContents ([]byte (fmt .Sprintf (`
110112 {
111- "server_uri": "%s",
112- "channel_creds": [{"type": "google_default"}],
113- "server_features": ["xds_v3", "ignore_resource_deletion", "xds.config.resource-in-sotw"]
114- }` , balancerName )))
113+ "xds_servers": [%s],
114+ "client_default_listener_resource_name_template": "%%s",
115+ "authorities": %s,
116+ "node": %s
117+ }` , xdsServerCfg , authoritiesCfg , nodeCfg )))
118+
115119 if err != nil {
116120 return nil , fmt .Errorf ("failed to build bootstrap configuration: %v" , err )
117121 }
118- config := & bootstrap.Config {
119- XDSServer : serverConfig ,
120- ClientDefaultListenerResourceNameTemplate : "%s" ,
121- Authorities : map [string ]* bootstrap.Authority {
122- c2pAuthority : {
123- XDSServer : serverConfig ,
124- ClientListenerResourceNameTemplate : fmt .Sprintf ("xdstp://%s/envoy.config.listener.v3.Listener/%%s" , c2pAuthority ),
125- },
126- },
127- NodeProto : newNode (<- zoneCh , <- ipv6CapableCh ),
128- }
129122
130123 // Create singleton xds client with this config. The xds client will be
131124 // used by the xds resolver later.
@@ -166,30 +159,41 @@ func (r *c2pResolver) Close() {
166159 r .clientCloseFunc ()
167160}
168161
169- var ipv6EnabledMetadata = & structpb.Struct {
170- Fields : map [string ]* structpb.Value {
171- ipv6CapableMetadataName : structpb .NewBoolValue (true ),
172- },
173- }
174-
175162var id = fmt .Sprintf ("C2P-%d" , grpcrand .Int ())
176163
177- // newNode makes a copy of defaultNode, and populate it's Metadata and
178- // Locality fields.
179- func newNode (zone string , ipv6Capable bool ) * v3corepb.Node {
180- ret := & v3corepb.Node {
181- // Not all required fields are set in defaultNote. Metadata will be set
182- // if ipv6 is enabled. Locality will be set to the value from metadata.
183- Id : id ,
184- UserAgentName : gRPCUserAgentName ,
185- UserAgentVersionType : & v3corepb.Node_UserAgentVersion {UserAgentVersion : grpc .Version },
186- ClientFeatures : []string {clientFeatureNoOverprovisioning },
187- }
188- ret .Locality = & v3corepb.Locality {Zone : zone }
164+ func newNodeConfig (zone string , ipv6Capable bool ) string {
165+ metadata := ""
189166 if ipv6Capable {
190- ret . Metadata = ipv6EnabledMetadata
167+ metadata = fmt . Sprintf ( `, "metadata": { "%s": true }` , ipv6CapableMetadataName )
191168 }
192- return ret
169+
170+ return fmt .Sprintf (`
171+ {
172+ "id": "%s",
173+ "locality": {
174+ "zone": "%s"
175+ }
176+ %s
177+ }` , id , zone , metadata )
178+ }
179+
180+ func newAuthoritiesConfig (xdsServer string ) string {
181+ return fmt .Sprintf (`
182+ {
183+ "%s": {
184+ "xds_servers": [%s]
185+ }
186+ }
187+ ` , c2pAuthority , xdsServer )
188+ }
189+
190+ func newXdsServerConfig (xdsServerURI string ) string {
191+ return fmt .Sprintf (`
192+ {
193+ "server_uri": "%s",
194+ "channel_creds": [{"type": "google_default"}],
195+ "server_features": ["xds_v3", "ignore_resource_deletion", "xds.config.resource-in-sotw"]
196+ }` , xdsServerURI )
193197}
194198
195199// runDirectPath returns whether this resolver should use direct path.
0 commit comments