From ee300dbdbe918acfa59e478c04adb6f4587b905d Mon Sep 17 00:00:00 2001 From: Vivek Singh Chauhan Date: Thu, 14 Nov 2024 13:41:10 -0700 Subject: [PATCH] APIGOV-29235 - override resolver for gRPC (#858) * INT - override resolver for gRPC * INT - moved resolver builder to utils for reusability --- pkg/util/dialer.go | 40 +++++++++++++++++++++++++++++++++++++ pkg/watchmanager/manager.go | 5 +++++ 2 files changed, 45 insertions(+) diff --git a/pkg/util/dialer.go b/pkg/util/dialer.go index 14331e65f..d0a0345e4 100644 --- a/pkg/util/dialer.go +++ b/pkg/util/dialer.go @@ -12,6 +12,7 @@ import ( "github.com/Axway/agent-sdk/pkg/util/log" "golang.org/x/net/proxy" + gr "google.golang.org/grpc/resolver" ) const ( @@ -167,3 +168,42 @@ func (d *dialer) createConnectRequest(ctx context.Context, targetAddress, sniHos } return req.WithContext(ctx) } + +type customGRPCResolverBuilder struct { + addr string + authority string + schema string +} + +func CreateCustomGRPCResolverBuilder(addr, authority, scheme string) gr.Builder { + return &customGRPCResolverBuilder{ + addr: addr, + authority: authority, + schema: scheme, + } +} + +func (b *customGRPCResolverBuilder) Build(target gr.Target, cc gr.ClientConn, _ gr.BuildOptions) (gr.Resolver, error) { + cc.UpdateState(gr.State{Endpoints: []gr.Endpoint{ + { + Addresses: []gr.Address{ + { + Addr: b.addr, + ServerName: b.authority, + }, + }, + }, + }}) + return &nopResolver{}, nil +} + +func (b *customGRPCResolverBuilder) Scheme() string { + return b.schema +} + +type nopResolver struct { +} + +func (*nopResolver) ResolveNow(gr.ResolveNowOptions) {} + +func (*nopResolver) Close() {} diff --git a/pkg/watchmanager/manager.go b/pkg/watchmanager/manager.go index cd741555f..99941097a 100644 --- a/pkg/watchmanager/manager.go +++ b/pkg/watchmanager/manager.go @@ -87,6 +87,11 @@ func (m *watchManager) createConnection() (*grpc.ClientConn, error) { logrusStreamClientInterceptor(m.options.loggerEntry), ), grpc.WithUserAgent(m.cfg.UserAgent), + grpc.WithResolvers( + util.CreateCustomGRPCResolverBuilder( + fmt.Sprintf("%s:%d", m.cfg.Host, m.cfg.Port), + m.cfg.Host, + "https")), } m.logger.