Skip to content

Commit f17ea7d

Browse files
xds: return all ServerConfig dial options together (#7680)
1 parent bdd444d commit f17ea7d

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

internal/xds/bootstrap/bootstrap.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,14 @@ func (sc *ServerConfig) ServerFeaturesIgnoreResourceDeletion() bool {
220220
return false
221221
}
222222

223-
// CredsDialOption returns the first supported transport credentials from the
224-
// configuration, as a dial option.
225-
func (sc *ServerConfig) CredsDialOption() grpc.DialOption {
226-
return sc.credsDialOption
227-
}
228-
229-
// DialerOption returns the Dialer function that specifies how to dial the xDS
230-
// server determined by the first supported credentials from the configuration,
231-
// as a dial option.
232-
//
233-
// TODO(https://github.com/grpc/grpc-go/issues/7661): change ServerConfig type
234-
// to have a single method that returns all configured dial options.
235-
func (sc *ServerConfig) DialerOption() grpc.DialOption {
236-
return sc.dialerOption
223+
// DialOptions returns a slice of all the configured dial options for this
224+
// server.
225+
func (sc *ServerConfig) DialOptions() []grpc.DialOption {
226+
dopts := []grpc.DialOption{sc.credsDialOption}
227+
if sc.dialerOption != nil {
228+
dopts = append(dopts, sc.dialerOption)
229+
}
230+
return dopts
237231
}
238232

239233
// Cleanups returns a collection of functions to be called when the xDS client

xds/internal/xdsclient/transport/transport.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -192,19 +192,14 @@ func New(opts Options) (*Transport, error) {
192192
return nil, errors.New("missing OnSend callback handler when creating a new transport")
193193
}
194194

195-
// Dial the xDS management with the passed in credentials.
196-
dopts := []grpc.DialOption{
197-
opts.ServerCfg.CredsDialOption(),
198-
grpc.WithKeepaliveParams(keepalive.ClientParameters{
199-
// We decided to use these sane defaults in all languages, and
200-
// kicked the can down the road as far making these configurable.
201-
Time: 5 * time.Minute,
202-
Timeout: 20 * time.Second,
203-
}),
204-
}
205-
if dialerOpts := opts.ServerCfg.DialerOption(); dialerOpts != nil {
206-
dopts = append(dopts, dialerOpts)
207-
}
195+
// Dial the xDS management server with dial options specified by the server
196+
// configuration and a static keepalive configuration that is common across
197+
// gRPC language implementations.
198+
kpCfg := grpc.WithKeepaliveParams(keepalive.ClientParameters{
199+
Time: 5 * time.Minute,
200+
Timeout: 20 * time.Second,
201+
})
202+
dopts := append([]grpc.DialOption{kpCfg}, opts.ServerCfg.DialOptions()...)
208203
grpcNewClient := transportinternal.GRPCNewClient.(func(string, ...grpc.DialOption) (*grpc.ClientConn, error))
209204
cc, err := grpcNewClient(opts.ServerCfg.ServerURI(), dopts...)
210205
if err != nil {

xds/internal/xdsclient/transport/transport_test.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
"net"
2424
"testing"
25+
"time"
2526

2627
"google.golang.org/grpc"
2728
"google.golang.org/grpc/credentials"
@@ -108,10 +109,17 @@ const testDialerCredsBuilderName = "test_dialer_creds"
108109
// testDialerCredsBuilder implements the `Credentials` interface defined in
109110
// package `xds/bootstrap` and encapsulates an insecure credential with a
110111
// custom Dialer that specifies how to dial the xDS server.
111-
type testDialerCredsBuilder struct{}
112+
type testDialerCredsBuilder struct {
113+
// Closed with the custom Dialer is invoked.
114+
// Needs to be passed in by the test.
115+
dialCalled chan struct{}
116+
}
112117

113118
func (t *testDialerCredsBuilder) Build(json.RawMessage) (credentials.Bundle, func(), error) {
114-
return &testDialerCredsBundle{insecure.NewBundle()}, func() {}, nil
119+
return &testDialerCredsBundle{
120+
Bundle: insecure.NewBundle(),
121+
dialCalled: t.dialCalled,
122+
}, func() {}, nil
115123
}
116124

117125
func (t *testDialerCredsBuilder) Name() string {
@@ -123,10 +131,12 @@ func (t *testDialerCredsBuilder) Name() string {
123131
// that specifies how to dial the xDS server.
124132
type testDialerCredsBundle struct {
125133
credentials.Bundle
134+
dialCalled chan struct{}
126135
}
127136

128-
func (t *testDialerCredsBundle) Dialer(context.Context, string) (net.Conn, error) {
129-
return nil, nil
137+
func (t *testDialerCredsBundle) Dialer(_ context.Context, address string) (net.Conn, error) {
138+
close(t.dialCalled)
139+
return net.Dial("tcp", address)
130140
}
131141

132142
func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
@@ -140,17 +150,16 @@ func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
140150
internal.GRPCNewClient = customGRPCNewClient
141151
defer func() { internal.GRPCNewClient = oldGRPCNewClient }()
142152

143-
bootstrap.RegisterCredentials(&testDialerCredsBuilder{})
153+
dialCalled := make(chan struct{})
154+
bootstrap.RegisterCredentials(&testDialerCredsBuilder{dialCalled: dialCalled})
144155
serverCfg, err := internalbootstrap.ServerConfigForTesting(internalbootstrap.ServerConfigTestingOptions{
145156
URI: "trafficdirector.googleapis.com:443",
146157
ChannelCreds: []internalbootstrap.ChannelCreds{{Type: testDialerCredsBuilderName}},
147158
})
148159
if err != nil {
149160
t.Fatalf("Failed to create server config for testing: %v", err)
150161
}
151-
if serverCfg.DialerOption() == nil {
152-
t.Fatalf("Dialer for xDS transport in server config for testing is nil, want non-nil")
153-
}
162+
154163
// Create a new transport.
155164
opts := transport.Options{
156165
ServerCfg: serverCfg,
@@ -171,6 +180,11 @@ func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
171180
if err != nil {
172181
t.Fatalf("transport.New(%v) failed: %v", opts, err)
173182
}
183+
select {
184+
case <-dialCalled:
185+
case <-time.After(defaultTestTimeout):
186+
t.Fatal("Timeout when waiting for Dialer() to be invoked")
187+
}
174188
// Verify there are three dial options passed to the custom grpc.NewClient.
175189
// The first is opts.ServerCfg.CredsDialOption(), the second is
176190
// grpc.WithKeepaliveParams(), and the third is opts.ServerCfg.DialerOption()

0 commit comments

Comments
 (0)