Skip to content

Commit dde7292

Browse files
Merge pull request #206 from njhale/sync/grpc-no-proxy
Bug 2011927: Introduce GRPC_PROXY EnvVar Support (#2364)
2 parents 76afb08 + c4e2447 commit dde7292

File tree

5 files changed

+510
-8
lines changed
  • staging/operator-lifecycle-manager
  • vendor
    • github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/grpc
    • golang.org/x/net/http/httpproxy

5 files changed

+510
-8
lines changed

staging/operator-lifecycle-manager/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ require (
3636
github.com/spf13/cobra v1.1.3
3737
github.com/spf13/pflag v1.0.5
3838
github.com/stretchr/testify v1.7.0
39+
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
3940
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
4041
google.golang.org/grpc v1.38.0
4142
gopkg.in/yaml.v2 v2.4.0

staging/operator-lifecycle-manager/pkg/controller/registry/grpc/source.go

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ 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"
912
"github.com/sirupsen/logrus"
13+
"golang.org/x/net/http/httpproxy"
14+
"golang.org/x/net/proxy"
1015
"google.golang.org/grpc"
1116
"google.golang.org/grpc/connectivity"
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1218

1319
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
14-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1520
)
1621

1722
type SourceMeta struct {
@@ -99,10 +104,71 @@ func (s *SourceStore) Get(key registry.CatalogKey) *SourceConn {
99104
return &source
100105
}
101106

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

105-
conn, err := grpc.Dial(address, grpc.WithInsecure())
171+
conn, err := grpcConnection(address)
106172
if err != nil {
107173
return nil, err
108174
}
@@ -225,4 +291,4 @@ func (s *SourceStore) ClientsForNamespaces(namespaces ...string) map[registry.Ca
225291

226292
// TODO : remove unhealthy
227293
return refs
228-
}
294+
}

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry/grpc/source.go

Lines changed: 70 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)