Skip to content

Commit b827466

Browse files
authored
Feat: support proxy-url (#141)
Signed-off-by: Yin Da <[email protected]>
1 parent 8ab4e3b commit b827466

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

pkg/apis/cluster/v1alpha1/clustergateway_types.go

+2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ type ClusterEndpointConst struct {
108108
// Insecure indicates the cluster should be access'd w/o verifying
109109
// CA certificate at client-side.
110110
Insecure *bool `json:"insecure,omitempty"`
111+
// ProxyURL indicates the proxy url of the server
112+
ProxyURL *string `json:"proxy-url,omitempty"`
111113
}
112114

113115
type ClusterAccessCredential struct {

pkg/apis/cluster/v1alpha1/clustergateway_types_secret.go

+6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ func convert(caData []byte, apiServerEndpoint string, insecure bool, secret *v1.
189189
if !ok {
190190
endpointType = string(ClusterEndpointTypeConst)
191191
}
192+
var proxyURL *string
193+
if url, useProxy := secret.Data["proxy-url"]; useProxy && len(url) > 0 {
194+
proxyURL = pointer.String(string(url))
195+
}
192196
switch ClusterEndpointType(endpointType) {
193197
case ClusterEndpointTypeClusterProxy:
194198
c.Spec.Access.Endpoint = &ClusterEndpoint{
@@ -206,6 +210,7 @@ func convert(caData []byte, apiServerEndpoint string, insecure bool, secret *v1.
206210
Const: &ClusterEndpointConst{
207211
Address: apiServerEndpoint,
208212
Insecure: &insecure,
213+
ProxyURL: proxyURL,
209214
},
210215
}
211216
} else {
@@ -214,6 +219,7 @@ func convert(caData []byte, apiServerEndpoint string, insecure bool, secret *v1.
214219
Const: &ClusterEndpointConst{
215220
Address: apiServerEndpoint,
216221
CABundle: caData,
222+
ProxyURL: proxyURL,
217223
},
218224
}
219225
}

pkg/apis/cluster/v1alpha1/transport.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package v1alpha1
33
import (
44
"context"
55
"net"
6+
"net/http"
67
"net/url"
78
"strconv"
89
"time"
910

10-
"github.com/oam-dev/cluster-gateway/pkg/config"
1111
"github.com/pkg/errors"
1212
"google.golang.org/grpc"
1313
grpccredentials "google.golang.org/grpc/credentials"
@@ -16,6 +16,8 @@ import (
1616
restclient "k8s.io/client-go/rest"
1717
konnectivity "sigs.k8s.io/apiserver-network-proxy/konnectivity-client/pkg/client"
1818
"sigs.k8s.io/apiserver-network-proxy/pkg/util"
19+
20+
"github.com/oam-dev/cluster-gateway/pkg/config"
1921
)
2022

2123
var DialerGetter = func(ctx context.Context) (k8snet.DialFunc, error) {
@@ -72,6 +74,14 @@ func NewConfigFromCluster(ctx context.Context, c *ClusterGateway) (*restclient.C
7274
host = u.Host
7375
}
7476
cfg.ServerName = host // apiserver may listen on SNI cert
77+
78+
if c.Spec.Access.Endpoint.Const.ProxyURL != nil {
79+
_url, _err := url.Parse(*c.Spec.Access.Endpoint.Const.ProxyURL)
80+
if _err != nil {
81+
return nil, _err
82+
}
83+
cfg.Proxy = http.ProxyURL(_url)
84+
}
7585
case ClusterEndpointTypeClusterProxy:
7686
cfg.Host = c.Name // the same as the cluster name
7787
cfg.Insecure = true

pkg/apis/cluster/v1alpha1/transport_test.go

+41
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package v1alpha1
33
import (
44
"context"
55
"net"
6+
"net/http"
7+
"net/url"
68
"testing"
79
"time"
810

@@ -19,6 +21,8 @@ func TestClusterRestConfigConversion(t *testing.T) {
1921
testCAData := []byte(`test-ca`)
2022
testCertData := []byte(`test-cert`)
2123
testKeyData := []byte(`test-key`)
24+
proxyURLData := []byte(`socks5://localhost:1080`)
25+
proxyURL, _ := url.Parse(string(proxyURLData))
2226
testDialFunc := func(ctx context.Context, net, addr string) (net.Conn, error) {
2327
return nil, nil
2428
}
@@ -179,6 +183,38 @@ func TestClusterRestConfigConversion(t *testing.T) {
179183
},
180184
},
181185
},
186+
{
187+
name: "proxy-url should work",
188+
clusterGateway: &ClusterGateway{
189+
ObjectMeta: metav1.ObjectMeta{
190+
Name: "my-cluster",
191+
},
192+
Spec: ClusterGatewaySpec{
193+
Access: ClusterAccess{
194+
Endpoint: &ClusterEndpoint{
195+
Type: ClusterEndpointTypeConst,
196+
Const: &ClusterEndpointConst{
197+
Address: "https://foo.bar:33",
198+
ProxyURL: pointer.String(string(proxyURLData)),
199+
},
200+
},
201+
Credential: &ClusterAccessCredential{
202+
Type: CredentialTypeServiceAccountToken,
203+
ServiceAccountToken: testToken,
204+
},
205+
},
206+
},
207+
},
208+
expectedCfg: &rest.Config{
209+
Host: "https://foo.bar:33",
210+
Timeout: 40 * time.Second,
211+
BearerToken: testToken,
212+
Proxy: http.ProxyURL(proxyURL),
213+
TLSClientConfig: rest.TLSClientConfig{
214+
ServerName: "foo.bar",
215+
},
216+
},
217+
},
182218
}
183219
for _, c := range cases {
184220
t.Run(c.name, func(t *testing.T) {
@@ -194,6 +230,11 @@ func TestClusterRestConfigConversion(t *testing.T) {
194230
c.expectedCfg.Dial = nil
195231
cfg.Dial = nil
196232
}
233+
if cfg.Proxy != nil {
234+
assert.NotNil(t, c.expectedCfg.Proxy)
235+
cfg.Proxy = nil
236+
c.expectedCfg.Proxy = nil
237+
}
197238
assert.Equal(t, c.expectedCfg, cfg)
198239
})
199240
}

0 commit comments

Comments
 (0)