Skip to content

Commit

Permalink
feat: add an option to enable DirectPath xDS (#1942)
Browse files Browse the repository at this point in the history
Currently DirectPath xDS is enabled by using env. We want to also provide an option for this.

Java PR: googleapis/gax-java#1968
  • Loading branch information
mohanli-ml authored Apr 19, 2023
1 parent d85769c commit 685ec81
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type DialSettings struct {
SkipValidation bool
ImpersonationConfig *impersonate.Config
EnableDirectPath bool
EnableDirectPathXds bool
AllowNonDefaultServiceAccount bool

// Google API system parameters. For more information please read:
Expand Down
15 changes: 15 additions & 0 deletions option/internaloption/internaloption.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ func (e enableDirectPath) Apply(o *internal.DialSettings) {
o.EnableDirectPath = bool(e)
}

// EnableDirectPathXds returns a ClientOption that overrides the default
// DirectPath type. It is only valid when DirectPath is enabled.
//
// It should only be used internally by generated clients.
// This is an EXPERIMENTAL API and may be changed or removed in the future.
func EnableDirectPathXds() option.ClientOption {
return enableDirectPathXds(true)
}

type enableDirectPathXds bool

func (x enableDirectPathXds) Apply(o *internal.DialSettings) {
o.EnableDirectPathXds = bool(x)
}

// AllowNonDefaultServiceAccount returns a ClientOption that overrides the default
// requirement for using the default service account for DirectPath.
//
Expand Down
15 changes: 14 additions & 1 deletion transport/grpc/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func dial(ctx context.Context, insecure bool, o *internal.DialSettings) (*grpc.C
grpcOpts = append(grpcOpts, timeoutDialerOption)
}
// Check if google-c2p resolver is enabled for DirectPath
if strings.EqualFold(os.Getenv(enableDirectPathXds), "true") {
if isDirectPathXdsUsed(o) {
// google-c2p resolver target must not have a port number
if addr, _, err := net.SplitHostPort(endpoint); err == nil {
endpoint = "google-c2p:///" + addr
Expand Down Expand Up @@ -251,6 +251,19 @@ func isDirectPathEnabled(endpoint string, o *internal.DialSettings) bool {
return true
}

func isDirectPathXdsUsed(o *internal.DialSettings) bool {
// Method 1: Enable DirectPath xDS by env;
if strings.EqualFold(os.Getenv(enableDirectPathXds), "true") {
return true
}
// Method 2: Enable DirectPath xDS by option;
if o.EnableDirectPathXds {
return true
}
return false

}

func isTokenSourceDirectPathCompatible(ts oauth2.TokenSource, o *internal.DialSettings) bool {
if ts == nil {
return false
Expand Down

0 comments on commit 685ec81

Please sign in to comment.