@@ -2,16 +2,21 @@ package grpc
2
2
3
3
import (
4
4
"context"
5
- "github.com/operator-framework/operator-registry/pkg/client"
5
+ "net"
6
+ "net/url"
7
+ "os"
6
8
"sync"
7
9
"time"
8
10
11
+ "github.com/operator-framework/operator-registry/pkg/client"
9
12
"github.com/sirupsen/logrus"
13
+ "golang.org/x/net/http/httpproxy"
14
+ "golang.org/x/net/proxy"
10
15
"google.golang.org/grpc"
11
16
"google.golang.org/grpc/connectivity"
17
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
18
13
19
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/registry"
14
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
15
20
)
16
21
17
22
type SourceMeta struct {
@@ -99,10 +104,71 @@ func (s *SourceStore) Get(key registry.CatalogKey) *SourceConn {
99
104
return & source
100
105
}
101
106
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
+
102
168
func (s * SourceStore ) Add (key registry.CatalogKey , address string ) (* SourceConn , error ) {
103
169
_ = s .Remove (key )
104
170
105
- conn , err := grpc . Dial (address , grpc . WithInsecure () )
171
+ conn , err := grpcConnection (address )
106
172
if err != nil {
107
173
return nil , err
108
174
}
@@ -225,4 +291,4 @@ func (s *SourceStore) ClientsForNamespaces(namespaces ...string) map[registry.Ca
225
291
226
292
// TODO : remove unhealthy
227
293
return refs
228
- }
294
+ }
0 commit comments