Skip to content

Commit

Permalink
Dialer method via credentials bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
danielzhaotongliu committed Sep 6, 2024
1 parent 5666049 commit 354e088
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/xds/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ package bootstrap

import (
"bytes"
"context"
"encoding/json"
"fmt"
"maps"
"net"
"net/url"
"os"
"slices"
Expand Down Expand Up @@ -179,6 +181,7 @@ type ServerConfig struct {
// credentials and store it here for easy access.
selectedCreds ChannelCreds
credsDialOption grpc.DialOption
dialerOption grpc.DialOption

cleanups []func()
}
Expand Down Expand Up @@ -223,6 +226,12 @@ func (sc *ServerConfig) CredsDialOption() grpc.DialOption {
return sc.credsDialOption
}

// DialerOption returns the first supported Dialer function that specifies how
// to dial the xDS server from the configuration, as a dial option.
func (sc *ServerConfig) DialerOption() grpc.DialOption {
return sc.dialerOption
}

// Cleanups returns a collection of functions to be called when the xDS client
// for this server is closed. Allows cleaning up resources created specifically
// for this server.
Expand Down Expand Up @@ -275,6 +284,12 @@ func (sc *ServerConfig) MarshalJSON() ([]byte, error) {
return json.Marshal(server)
}

// dialer captures the Dialer method specified via the credentials bundle.
type dialer interface {
// Dialer specifies how to dial the xDS server.
Dialer(context.Context, string) (net.Conn, error)
}

// UnmarshalJSON takes the json data (a server) and unmarshals it to the struct.
func (sc *ServerConfig) UnmarshalJSON(data []byte) error {
server := serverConfigJSON{}
Expand All @@ -298,6 +313,10 @@ func (sc *ServerConfig) UnmarshalJSON(data []byte) error {
}
sc.selectedCreds = cc
sc.credsDialOption = grpc.WithCredentialsBundle(bundle)
d, ok := bundle.(dialer)
if ok {
sc.dialerOption = grpc.WithContextDialer(d.Dialer)

Check warning on line 318 in internal/xds/bootstrap/bootstrap.go

View check run for this annotation

Codecov / codecov/patch

internal/xds/bootstrap/bootstrap.go#L318

Added line #L318 was not covered by tests
}
sc.cleanups = append(sc.cleanups, cancel)
break
}
Expand Down
4 changes: 4 additions & 0 deletions xds/internal/xdsclient/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ func New(opts Options) (*Transport, error) {
Timeout: 20 * time.Second,
}),
}
dialerOpts := opts.ServerCfg.DialerOption()
if dialerOpts != nil {
dopts = append(dopts, dialerOpts)

Check warning on line 207 in xds/internal/xdsclient/transport/transport.go

View check run for this annotation

Codecov / codecov/patch

xds/internal/xdsclient/transport/transport.go#L207

Added line #L207 was not covered by tests
}
grpcNewClient := transportinternal.GRPCNewClient.(func(string, ...grpc.DialOption) (*grpc.ClientConn, error))
cc, err := grpcNewClient(opts.ServerCfg.ServerURI(), dopts...)
if err != nil {
Expand Down

0 comments on commit 354e088

Please sign in to comment.