Skip to content

Commit

Permalink
xds/bootstrap_config: add a string function to server config (#5031)
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl authored Dec 2, 2021
1 parent 46935b9 commit f3bbd12
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
26 changes: 23 additions & 3 deletions xds/internal/xdsclient/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ type ServerConfig struct {
// Creds contains the credentials to be used while talking to the xDS
// server, as a grpc.DialOption.
Creds grpc.DialOption
// credsType is the type of the creds. It will be used to dedup servers.
credsType string
// CredsType is the type of the creds. It will be used to dedup servers.
CredsType string
// TransportAPI indicates the API version of xDS transport protocol to use.
// This describes the xDS gRPC endpoint and version of
// DiscoveryRequest/Response used on the wire.
Expand All @@ -86,6 +86,26 @@ type ServerConfig struct {
NodeProto proto.Message
}

// String returns the string representation of the ServerConfig.
//
// This string representation will be used as map keys in federation
// (`map[ServerConfig]authority`), so that the xDS ClientConn and stream will be
// shared by authorities with different names but the same server config.
//
// It covers (almost) all the fields so the string can represent the config
// content. It doesn't cover NodeProto because NodeProto isn't used by
// federation.
func (sc *ServerConfig) String() string {
var ver string
switch sc.TransportAPI {
case version.TransportV3:
ver = "xDSv3"
case version.TransportV2:
ver = "xDSv2"
}
return strings.Join([]string{sc.ServerURI, sc.CredsType, ver}, "-")
}

// UnmarshalJSON takes the json data (a list of servers) and unmarshals the
// first one in the list.
func (sc *ServerConfig) UnmarshalJSON(data []byte) error {
Expand All @@ -100,7 +120,7 @@ func (sc *ServerConfig) UnmarshalJSON(data []byte) error {
sc.ServerURI = xs.ServerURI
for _, cc := range xs.ChannelCreds {
// We stop at the first credential type that we support.
sc.credsType = cc.Type
sc.CredsType = cc.Type
if cc.Type == credsGoogleDefault {
sc.Creds = grpc.WithCredentialsBundle(google.NewDefaultCredentials())
break
Expand Down
22 changes: 11 additions & 11 deletions xds/internal/xdsclient/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ var (
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithTransportCredentials(insecure.NewCredentials()),
credsType: "insecure",
CredsType: "insecure",
NodeProto: v2NodeProto,
},
ClientDefaultListenerResourceNameTemplate: "%s",
Expand All @@ -221,7 +221,7 @@ var (
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
credsType: "google_default",
CredsType: "google_default",
NodeProto: v2NodeProto,
},
ClientDefaultListenerResourceNameTemplate: "%s",
Expand All @@ -230,7 +230,7 @@ var (
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
credsType: "google_default",
CredsType: "google_default",
TransportAPI: version.TransportV3,
NodeProto: v3NodeProto,
},
Expand Down Expand Up @@ -395,7 +395,7 @@ func TestNewConfigV2ProtoSuccess(t *testing.T) {
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithTransportCredentials(insecure.NewCredentials()),
credsType: "insecure",
CredsType: "insecure",
NodeProto: &v2corepb.Node{
BuildVersion: gRPCVersion,
UserAgentName: gRPCUserAgentName,
Expand Down Expand Up @@ -665,7 +665,7 @@ func TestNewConfigWithCertificateProviders(t *testing.T) {
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
credsType: "google_default",
CredsType: "google_default",
TransportAPI: version.TransportV3,
NodeProto: v3NodeProto,
},
Expand Down Expand Up @@ -759,7 +759,7 @@ func TestNewConfigWithServerListenerResourceNameTemplate(t *testing.T) {
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
credsType: "google_default",
CredsType: "google_default",
TransportAPI: version.TransportV2,
NodeProto: v2NodeProto,
},
Expand Down Expand Up @@ -908,7 +908,7 @@ func TestNewConfigWithFederation(t *testing.T) {
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
credsType: "google_default",
CredsType: "google_default",
TransportAPI: version.TransportV2,
NodeProto: v2NodeProto,
},
Expand All @@ -920,7 +920,7 @@ func TestNewConfigWithFederation(t *testing.T) {
XDSServer: &ServerConfig{
ServerURI: "td.com",
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
credsType: "google_default",
CredsType: "google_default",
TransportAPI: version.TransportV3,
NodeProto: v3NodeProto,
},
Expand All @@ -934,7 +934,7 @@ func TestNewConfigWithFederation(t *testing.T) {
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
credsType: "google_default",
CredsType: "google_default",
TransportAPI: version.TransportV2,
NodeProto: v2NodeProto,
},
Expand All @@ -947,7 +947,7 @@ func TestNewConfigWithFederation(t *testing.T) {
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
credsType: "google_default",
CredsType: "google_default",
TransportAPI: version.TransportV2,
NodeProto: v2NodeProto,
},
Expand All @@ -965,7 +965,7 @@ func TestNewConfigWithFederation(t *testing.T) {
XDSServer: &ServerConfig{
ServerURI: "trafficdirector.googleapis.com:443",
Creds: grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()),
credsType: "google_default",
CredsType: "google_default",
TransportAPI: version.TransportV2,
NodeProto: v2NodeProto,
},
Expand Down

0 comments on commit f3bbd12

Please sign in to comment.