From 8f3c31e4e7c9ec9aa7929a9820129c410896ab6c Mon Sep 17 00:00:00 2001 From: mohanli-ml Date: Wed, 12 Apr 2023 21:44:46 +0000 Subject: [PATCH 1/2] feat(internaloption): add an option to enable DirectPath xDS --- internal/settings.go | 1 + option/internaloption/internaloption.go | 15 +++++++++++++++ transport/grpc/dial.go | 15 ++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/internal/settings.go b/internal/settings.go index 76efdb22772..e6039fa7983 100644 --- a/internal/settings.go +++ b/internal/settings.go @@ -46,6 +46,7 @@ type DialSettings struct { SkipValidation bool ImpersonationConfig *impersonate.Config EnableDirectPath bool + UseDirectPathXds bool AllowNonDefaultServiceAccount bool // Google API system parameters. For more information please read: diff --git a/option/internaloption/internaloption.go b/option/internaloption/internaloption.go index cc7ebfe277b..6d451eab94f 100644 --- a/option/internaloption/internaloption.go +++ b/option/internaloption/internaloption.go @@ -67,6 +67,21 @@ func (e enableDirectPath) Apply(o *internal.DialSettings) { o.EnableDirectPath = bool(e) } +// UseDirectPathXds 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 UseDirectPathXds(xds bool) option.ClientOption { + return useDirectPathXds(xds) +} + +type useDirectPathXds bool + +func (x useDirectPathXds) Apply(o *internal.DialSettings) { + o.UseDirectPathXds = bool(x) +} + // AllowNonDefaultServiceAccount returns a ClientOption that overrides the default // requirement for using the default service account for DirectPath. // diff --git a/transport/grpc/dial.go b/transport/grpc/dial.go index 9a9f57cc4e4..a50a1a164f7 100644 --- a/transport/grpc/dial.go +++ b/transport/grpc/dial.go @@ -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 @@ -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.UseDirectPathXds { + return true + } + return false + +} + func isTokenSourceDirectPathCompatible(ts oauth2.TokenSource, o *internal.DialSettings) bool { if ts == nil { return false From 1fdda0ae3d8702767897ced6030d229c61fc942d Mon Sep 17 00:00:00 2001 From: mohanli-ml Date: Tue, 18 Apr 2023 23:12:18 +0000 Subject: [PATCH 2/2] feat: add an option to enable DirectPath xDS --- internal/settings.go | 2 +- option/internaloption/internaloption.go | 12 ++++++------ transport/grpc/dial.go | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/settings.go b/internal/settings.go index e6039fa7983..3a3874df112 100644 --- a/internal/settings.go +++ b/internal/settings.go @@ -46,7 +46,7 @@ type DialSettings struct { SkipValidation bool ImpersonationConfig *impersonate.Config EnableDirectPath bool - UseDirectPathXds bool + EnableDirectPathXds bool AllowNonDefaultServiceAccount bool // Google API system parameters. For more information please read: diff --git a/option/internaloption/internaloption.go b/option/internaloption/internaloption.go index 6d451eab94f..3b8461d1da9 100644 --- a/option/internaloption/internaloption.go +++ b/option/internaloption/internaloption.go @@ -67,19 +67,19 @@ func (e enableDirectPath) Apply(o *internal.DialSettings) { o.EnableDirectPath = bool(e) } -// UseDirectPathXds returns a ClientOption that overrides the default +// 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 UseDirectPathXds(xds bool) option.ClientOption { - return useDirectPathXds(xds) +func EnableDirectPathXds() option.ClientOption { + return enableDirectPathXds(true) } -type useDirectPathXds bool +type enableDirectPathXds bool -func (x useDirectPathXds) Apply(o *internal.DialSettings) { - o.UseDirectPathXds = bool(x) +func (x enableDirectPathXds) Apply(o *internal.DialSettings) { + o.EnableDirectPathXds = bool(x) } // AllowNonDefaultServiceAccount returns a ClientOption that overrides the default diff --git a/transport/grpc/dial.go b/transport/grpc/dial.go index a50a1a164f7..e1403e08ee6 100644 --- a/transport/grpc/dial.go +++ b/transport/grpc/dial.go @@ -257,7 +257,7 @@ func isDirectPathXdsUsed(o *internal.DialSettings) bool { return true } // Method 2: Enable DirectPath xDS by option; - if o.UseDirectPathXds { + if o.EnableDirectPathXds { return true } return false