Skip to content

Commit

Permalink
googlec2p: use the bootstrap parsing code to generate parsed bootstra…
Browse files Browse the repository at this point in the history
…p config instead of handcrafting it
  • Loading branch information
clement2026 committed Mar 16, 2024
1 parent 4f43d2e commit 0268a86
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
81 changes: 42 additions & 39 deletions xds/googledirectpath/googlec2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"net/url"
"time"

"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/internal/envconfig"
"google.golang.org/grpc/internal/googlecloud"
Expand All @@ -39,9 +38,6 @@ import (
"google.golang.org/grpc/resolver"
"google.golang.org/grpc/xds/internal/xdsclient"
"google.golang.org/grpc/xds/internal/xdsclient/bootstrap"
"google.golang.org/protobuf/types/known/structpb"

v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"

_ "google.golang.org/grpc/xds" // To register xds resolvers and balancers.
)
Expand Down Expand Up @@ -106,25 +102,21 @@ func (c2pResolverBuilder) Build(t resolver.Target, cc resolver.ClientConn, opts
if balancerName == "" {
balancerName = tdURL
}
serverConfig, err := bootstrap.ServerConfigFromJSON([]byte(fmt.Sprintf(`

node := newNode(<-zoneCh, <-ipv6CapableCh)
xdsServer := newXdsServer(balancerName)
authorities := newAuthorities(xdsServer)

config, err := bootstrap.NewConfigFromContents([]byte(fmt.Sprintf(`
{
"server_uri": "%s",
"channel_creds": [{"type": "google_default"}],
"server_features": ["xds_v3", "ignore_resource_deletion"]
}`, balancerName)))
"xds_servers": [%s],
"authorities": %s,
"node": %s
}`, xdsServer, authorities, node)))

if err != nil {
return nil, fmt.Errorf("failed to build bootstrap configuration: %v", err)
}
config := &bootstrap.Config{
XDSServer: serverConfig,
ClientDefaultListenerResourceNameTemplate: "%s",
Authorities: map[string]*bootstrap.Authority{
c2pAuthority: {
XDSServer: serverConfig,
},
},
NodeProto: newNode(<-zoneCh, <-ipv6CapableCh),
}

// Create singleton xds client with this config. The xds client will be
// used by the xds resolver later.
Expand Down Expand Up @@ -165,30 +157,41 @@ func (r *c2pResolver) Close() {
r.clientCloseFunc()
}

var ipv6EnabledMetadata = &structpb.Struct{
Fields: map[string]*structpb.Value{
ipv6CapableMetadataName: structpb.NewBoolValue(true),
},
}

var id = fmt.Sprintf("C2P-%d", grpcrand.Int())

// newNode makes a copy of defaultNode, and populate it's Metadata and
// Locality fields.
func newNode(zone string, ipv6Capable bool) *v3corepb.Node {
ret := &v3corepb.Node{
// Not all required fields are set in defaultNote. Metadata will be set
// if ipv6 is enabled. Locality will be set to the value from metadata.
Id: id,
UserAgentName: gRPCUserAgentName,
UserAgentVersionType: &v3corepb.Node_UserAgentVersion{UserAgentVersion: grpc.Version},
ClientFeatures: []string{clientFeatureNoOverprovisioning},
}
ret.Locality = &v3corepb.Locality{Zone: zone}
func newNode(zone string, ipv6Capable bool) string {
metadata := ""
if ipv6Capable {
ret.Metadata = ipv6EnabledMetadata
metadata = fmt.Sprintf(`, "metadata": { "%s": true }`, ipv6CapableMetadataName)
}
return ret

return fmt.Sprintf(`
{
"id": "%s",
"locality": {
"zone": "%s"
}
%s
}`, id, zone, metadata)
}

func newAuthorities(xdsServer string) string {
return fmt.Sprintf(`
{
"%s": {
"xds_servers": [%s]
}
}
`, c2pAuthority, xdsServer)
}

func newXdsServer(balancerName string) string {
return fmt.Sprintf(`
{
"server_uri": "%s",
"channel_creds": [{"type": "google_default"}],
"server_features": ["xds_v3", "ignore_resource_deletion"]
}`, balancerName)
}

// runDirectPath returns whether this resolver should use direct path.
Expand Down
6 changes: 2 additions & 4 deletions xds/internal/xdsclient/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,9 @@ func NewConfig() (*Config, error) {
return newConfigFromContents(data)
}

// NewConfigFromContentsForTesting returns a new Config using the specified
// NewConfigFromContents returns a new Config using the specified
// bootstrap file contents instead of reading the environment variable.
//
// This is only suitable for testing purposes.
func NewConfigFromContentsForTesting(data []byte) (*Config, error) {
func NewConfigFromContents(data []byte) (*Config, error) {
return newConfigFromContents(data)
}

Expand Down
2 changes: 1 addition & 1 deletion xds/internal/xdsclient/client_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func getOrMakeClientForTesting(config []byte) (*clientRefCounted, error) {
return c, nil
}

bcfg, err := bootstrap.NewConfigFromContentsForTesting(config)
bcfg, err := bootstrap.NewConfigFromContents(config)
if err != nil {
return nil, fmt.Errorf("bootstrap config %s: %v", string(config), err)
}
Expand Down

0 comments on commit 0268a86

Please sign in to comment.