Skip to content

Commit 4115c21

Browse files
xds: return all ServerConfig dial options together (#7718)
1 parent b8ee37d commit 4115c21

File tree

3 files changed

+16
-110
lines changed

3 files changed

+16
-110
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: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@
1818
package transport_test
1919

2020
import (
21-
"context"
22-
"encoding/json"
23-
"net"
2421
"testing"
2522

2623
"google.golang.org/grpc"
27-
"google.golang.org/grpc/credentials"
28-
"google.golang.org/grpc/credentials/insecure"
2924
"google.golang.org/grpc/internal/grpctest"
3025
internalbootstrap "google.golang.org/grpc/internal/xds/bootstrap"
31-
"google.golang.org/grpc/xds/bootstrap"
3226
"google.golang.org/grpc/xds/internal/xdsclient/transport"
3327
"google.golang.org/grpc/xds/internal/xdsclient/transport/internal"
3428

@@ -102,80 +96,3 @@ func (s) TestNewWithGRPCDial(t *testing.T) {
10296
t.Fatalf("transport.New(%+v) custom dialer called = true, want false", opts)
10397
}
10498
}
105-
106-
const testDialerCredsBuilderName = "test_dialer_creds"
107-
108-
// testDialerCredsBuilder implements the `Credentials` interface defined in
109-
// package `xds/bootstrap` and encapsulates an insecure credential with a
110-
// custom Dialer that specifies how to dial the xDS server.
111-
type testDialerCredsBuilder struct{}
112-
113-
func (t *testDialerCredsBuilder) Build(json.RawMessage) (credentials.Bundle, func(), error) {
114-
return &testDialerCredsBundle{insecure.NewBundle()}, func() {}, nil
115-
}
116-
117-
func (t *testDialerCredsBuilder) Name() string {
118-
return testDialerCredsBuilderName
119-
}
120-
121-
// testDialerCredsBundle implements the `Bundle` interface defined in package
122-
// `credentials` and encapsulates an insecure credential with a custom Dialer
123-
// that specifies how to dial the xDS server.
124-
type testDialerCredsBundle struct {
125-
credentials.Bundle
126-
}
127-
128-
func (t *testDialerCredsBundle) Dialer(context.Context, string) (net.Conn, error) {
129-
return nil, nil
130-
}
131-
132-
func (s) TestNewWithDialerFromCredentialsBundle(t *testing.T) {
133-
// Override grpc.NewClient with a custom one.
134-
doptsLen := 0
135-
customGRPCNewClient := func(target string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
136-
doptsLen = len(opts)
137-
return grpc.NewClient(target, opts...)
138-
}
139-
oldGRPCNewClient := internal.GRPCNewClient
140-
internal.GRPCNewClient = customGRPCNewClient
141-
defer func() { internal.GRPCNewClient = oldGRPCNewClient }()
142-
143-
bootstrap.RegisterCredentials(&testDialerCredsBuilder{})
144-
serverCfg, err := internalbootstrap.ServerConfigForTesting(internalbootstrap.ServerConfigTestingOptions{
145-
URI: "trafficdirector.googleapis.com:443",
146-
ChannelCreds: []internalbootstrap.ChannelCreds{{Type: testDialerCredsBuilderName}},
147-
})
148-
if err != nil {
149-
t.Fatalf("Failed to create server config for testing: %v", err)
150-
}
151-
if serverCfg.DialerOption() == nil {
152-
t.Fatalf("Dialer for xDS transport in server config for testing is nil, want non-nil")
153-
}
154-
// Create a new transport.
155-
opts := transport.Options{
156-
ServerCfg: serverCfg,
157-
NodeProto: &v3corepb.Node{},
158-
OnRecvHandler: func(update transport.ResourceUpdate, onDone func()) error {
159-
onDone()
160-
return nil
161-
},
162-
OnErrorHandler: func(error) {},
163-
OnSendHandler: func(*transport.ResourceSendInfo) {},
164-
}
165-
c, err := transport.New(opts)
166-
defer func() {
167-
if c != nil {
168-
c.Close()
169-
}
170-
}()
171-
if err != nil {
172-
t.Fatalf("transport.New(%v) failed: %v", opts, err)
173-
}
174-
// Verify there are three dial options passed to the custom grpc.NewClient.
175-
// The first is opts.ServerCfg.CredsDialOption(), the second is
176-
// grpc.WithKeepaliveParams(), and the third is opts.ServerCfg.DialerOption()
177-
// from the credentials bundle.
178-
if doptsLen != 3 {
179-
t.Fatalf("transport.New(%v) custom grpc.NewClient called with %d dial options, want 3", opts, doptsLen)
180-
}
181-
}

0 commit comments

Comments
 (0)