Skip to content

Bug 2011927: Introduce GRPC_PROXY EnvVar Support (#2364) #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions staging/operator-lifecycle-manager/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ require (
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
google.golang.org/grpc v1.38.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@ package grpc

import (
"context"
"github.com/operator-framework/operator-registry/pkg/client"
"net"
"net/url"
"os"
"sync"
"time"

"github.com/operator-framework/operator-registry/pkg/client"
"github.com/sirupsen/logrus"
"golang.org/x/net/http/httpproxy"
"golang.org/x/net/proxy"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

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

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

func grpcProxyURL(addr string) (*url.URL, error) {
// Handle ip addresses
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}

url, err := url.Parse(host)
if err != nil {
return nil, err
}

// Hardcode fields required for proxy resolution
url.Host = addr
url.Scheme = "http"

// Override HTTPS_PROXY and HTTP_PROXY with GRPC_PROXY
proxyConfig := &httpproxy.Config{
HTTPProxy: getGRPCProxyEnv(),
HTTPSProxy: getGRPCProxyEnv(),
NoProxy: getEnvAny("NO_PROXY", "no_proxy"),
CGI: os.Getenv("REQUEST_METHOD") != "",
}

// Check if a proxy should be used based on environment variables
return proxyConfig.ProxyFunc()(url)
}

func getGRPCProxyEnv() string {
return getEnvAny("GRPC_PROXY", "grpc_proxy")
}

func getEnvAny(names ...string) string {
for _, n := range names {
if val := os.Getenv(n); val != "" {
return val
}
}
return ""
}

func grpcConnection(address string) (*grpc.ClientConn, error) {
dialOptions := []grpc.DialOption{grpc.WithInsecure()}
proxyURL, err := grpcProxyURL(address)
if err != nil {
return nil, err
}

if proxyURL != nil {
dialOptions = append(dialOptions, grpc.WithContextDialer(func(ctx context.Context, addr string) (net.Conn, error) {
dialer, err := proxy.FromURL(proxyURL, &net.Dialer{})
if err != nil {
return nil, err
}
return dialer.Dial("tcp", addr)
}))
}

return grpc.Dial(address, dialOptions...)
}

func (s *SourceStore) Add(key registry.CatalogKey, address string) (*SourceConn, error) {
_ = s.Remove(key)

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

// TODO : remove unhealthy
return refs
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading