Skip to content

Commit

Permalink
xds: add support for multiple xDS clients, for fallback (#7347)
Browse files Browse the repository at this point in the history
  • Loading branch information
easwars authored Jul 2, 2024
1 parent 5ac73ac commit 4e9b596
Show file tree
Hide file tree
Showing 37 changed files with 1,365 additions and 759 deletions.
8 changes: 4 additions & 4 deletions internal/testutils/xds/e2e/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type serverLogger struct {
}

func (l serverLogger) Debugf(format string, args ...any) {
l.logger.Logf(format, args)
l.logger.Logf(format, args...)
}
func (l serverLogger) Infof(format string, args ...any) {
l.logger.Logf(format, args)
l.logger.Logf(format, args...)
}
func (l serverLogger) Warnf(format string, args ...any) {
l.logger.Logf(format, args)
l.logger.Logf(format, args...)
}
func (l serverLogger) Errorf(format string, args ...any) {
l.logger.Logf(format, args)
l.logger.Logf(format, args...)
}
18 changes: 12 additions & 6 deletions internal/xds/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"google.golang.org/grpc/credentials/tls/certprovider"
"google.golang.org/grpc/internal"
"google.golang.org/grpc/internal/envconfig"
"google.golang.org/grpc/internal/pretty"
"google.golang.org/grpc/xds/bootstrap"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
Expand Down Expand Up @@ -213,6 +212,9 @@ func (sc *ServerConfig) Equal(other *ServerConfig) bool {
// content. It doesn't cover NodeProto because NodeProto isn't used by
// federation.
func (sc *ServerConfig) String() string {
if len(sc.serverFeatures) == 0 {
return fmt.Sprintf("%s-%s", sc.serverURI, sc.selectedCreds.String())
}
features := strings.Join(sc.serverFeatures, "-")
return strings.Join([]string{sc.serverURI, sc.selectedCreds.String(), features}, "-")
}
Expand Down Expand Up @@ -418,6 +420,12 @@ func (c *Config) Equal(other *Config) bool {
return true
}

// String returns a string representation of the Config.
func (c *Config) String() string {
s, _ := c.MarshalJSON()
return string(s)
}

// The following fields correspond 1:1 with the JSON schema for Config.
type configJSON struct {
XDSServers []*ServerConfig `json:"xds_servers,omitempty"`
Expand All @@ -438,7 +446,7 @@ func (c *Config) MarshalJSON() ([]byte, error) {
Authorities: c.authorities,
Node: c.node,
}
return json.Marshal(config)
return json.MarshalIndent(config, " ", " ")
}

// UnmarshalJSON takes the json data (the complete bootstrap configuration) and
Expand Down Expand Up @@ -566,9 +574,7 @@ func newConfigFromContents(data []byte) (*Config, error) {
}

if logger.V(2) {
logger.Infof("Bootstrap config for creating xds-client: %v", pretty.ToJSON(config))
} else {
logger.Infof("Bootstrap config for creating xds-client: %+v", config)
logger.Infof("Bootstrap config for creating xds-client: %s", config)
}
return config, nil
}
Expand Down Expand Up @@ -632,7 +638,7 @@ func NewContentsForTesting(opts ConfigOptionsForTesting) ([]byte, error) {
Authorities: authorities,
Node: node{ID: opts.NodeID},
}
contents, err := json.Marshal(cfgJSON)
contents, err := json.MarshalIndent(cfgJSON, " ", " ")
if err != nil {
return nil, fmt.Errorf("failed to marshal bootstrap configuration for provided options %+v: %v", opts, err)
}
Expand Down
27 changes: 4 additions & 23 deletions xds/csds/csds.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"context"
"fmt"
"io"
"sync"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/grpclog"
Expand Down Expand Up @@ -55,22 +54,14 @@ func prefixLogger(s *ClientStatusDiscoveryServer) *internalgrpclog.PrefixLogger
// https://github.com/grpc/proposal/blob/master/A40-csds-support.md.
type ClientStatusDiscoveryServer struct {
logger *internalgrpclog.PrefixLogger

mu sync.Mutex
xdsClient xdsclient.XDSClient
xdsClientClose func()
}

// NewClientStatusDiscoveryServer returns an implementation of the CSDS server
// that can be registered on a gRPC server.
func NewClientStatusDiscoveryServer() (*ClientStatusDiscoveryServer, error) {
c, close, err := xdsclient.New()
if err != nil {
logger.Warningf("Failed to create xDS client: %v", err)
}
s := &ClientStatusDiscoveryServer{xdsClient: c, xdsClientClose: close}
s := &ClientStatusDiscoveryServer{}
s.logger = prefixLogger(s)
s.logger.Infof("Created CSDS server, with xdsClient %p", c)
s.logger.Infof("Created CSDS server")
return s, nil
}

Expand Down Expand Up @@ -104,24 +95,14 @@ func (s *ClientStatusDiscoveryServer) FetchClientStatus(_ context.Context, req *
//
// If it returns an error, the error is a status error.
func (s *ClientStatusDiscoveryServer) buildClientStatusRespForReq(req *v3statuspb.ClientStatusRequest) (*v3statuspb.ClientStatusResponse, error) {
s.mu.Lock()
defer s.mu.Unlock()

if s.xdsClient == nil {
return &v3statuspb.ClientStatusResponse{}, nil
}
// Field NodeMatchers is unsupported, by design
// https://github.com/grpc/proposal/blob/master/A40-csds-support.md#detail-node-matching.
if len(req.NodeMatchers) != 0 {
return nil, status.Errorf(codes.InvalidArgument, "node_matchers are not supported, request contains node_matchers: %v", req.NodeMatchers)
}

return s.xdsClient.DumpResources()
return xdsclient.DumpResources(), nil
}

// Close cleans up the resources.
func (s *ClientStatusDiscoveryServer) Close() {
if s.xdsClientClose != nil {
s.xdsClientClose()
}
}
func (s *ClientStatusDiscoveryServer) Close() {}
Loading

0 comments on commit 4e9b596

Please sign in to comment.