Skip to content

Commit be37352

Browse files
committed
Introduce GRPC_PROXY EnvVar Support
Introduce the ability to specify a dial context for GRPC connections.
1 parent 2645388 commit be37352

File tree

11 files changed

+1357
-3
lines changed

11 files changed

+1357
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require (
3737
github.com/spf13/cobra v1.1.1
3838
github.com/spf13/pflag v1.0.5
3939
github.com/stretchr/testify v1.6.1
40+
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
4041
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
4142
google.golang.org/grpc v1.30.0
4243
gopkg.in/yaml.v2 v2.3.0

pkg/controller/registry/grpc/source.go

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@ package grpc
22

33
import (
44
"context"
5-
"github.com/operator-framework/operator-registry/pkg/client"
5+
"net"
6+
"net/url"
7+
"os"
68
"sync"
79
"time"
810

11+
"github.com/operator-framework/operator-registry/pkg/client"
12+
913
"github.com/sirupsen/logrus"
14+
"golang.org/x/net/http/httpproxy"
15+
"golang.org/x/net/proxy"
1016
"google.golang.org/grpc"
1117
"google.golang.org/grpc/connectivity"
1218

@@ -99,10 +105,69 @@ func (s *SourceStore) Get(key registry.CatalogKey) *SourceConn {
99105
return &source
100106
}
101107

108+
func grpcProxyURL(addr string) (*url.URL, error) {
109+
// Handle ip addresses
110+
host, _, err := net.SplitHostPort(addr)
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
url, err := url.Parse(host)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
// Hardcode fields required for proxy resolution
121+
url.Host = addr
122+
url.Scheme = "http"
123+
124+
proxyConfig := httpproxy.FromEnvironment()
125+
126+
// Override HTTPS_PROXY and HTTP_PROXY with GRPC_PROXY
127+
proxyConfig.HTTPProxy = getGRPCProxyEnv()
128+
proxyConfig.HTTPSProxy = getGRPCProxyEnv()
129+
130+
// Check if a proxy should be used based on Environment variables
131+
return proxyConfig.ProxyFunc()(url)
132+
}
133+
134+
func getGRPCProxyEnv() string {
135+
return getEnvAny("GRPC_PROXY", "grpc_proxy")
136+
}
137+
138+
func getEnvAny(names ...string) string {
139+
for _, n := range names {
140+
if val := os.Getenv(n); val != "" {
141+
return val
142+
}
143+
}
144+
return ""
145+
}
146+
147+
func grpcConnection(address string) (*grpc.ClientConn, error) {
148+
// Check if a proxy should be used based on Environment variables
149+
proxyURL, err := grpcProxyURL(address)
150+
if err != nil {
151+
return nil, err
152+
}
153+
154+
if proxyURL != nil {
155+
return grpc.Dial(address, grpc.WithInsecure(), grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
156+
dialer, err := proxy.FromURL(proxyURL, &net.Dialer{})
157+
if err != nil {
158+
return nil, err
159+
}
160+
return dialer.Dial("tcp", addr)
161+
}))
162+
}
163+
164+
return grpc.Dial(address, grpc.WithInsecure())
165+
}
166+
102167
func (s *SourceStore) Add(key registry.CatalogKey, address string) (*SourceConn, error) {
103168
_ = s.Remove(key)
104169

105-
conn, err := grpc.Dial(address, grpc.WithInsecure())
170+
conn, err := grpcConnection(address)
106171
if err != nil {
107172
return nil, err
108173
}
@@ -225,4 +290,4 @@ func (s *SourceStore) ClientsForNamespaces(namespaces ...string) map[registry.Ca
225290

226291
// TODO : remove unhealthy
227292
return refs
228-
}
293+
}

0 commit comments

Comments
 (0)