rest.Config: support configuring an explict proxy URL

With support of http, https, and socks5 proxy support. We already
support configuring this via environmnet variables, but this approach
becomes inconvenient dealing with multiple clusters on different
networks, that require different proxies to connect to. Most solutions
require wrapping clients (like kubectl) in bash scripts.

Part of: https://github.com/kubernetes/client-go/issues/351

Kubernetes-commit: f3f666d5f1f6f74a8c948a5c64af993696178244
This commit is contained in:
Mike Danese
2019-05-03 13:50:17 -07:00
committed by Kubernetes Publisher
parent 06f6a9f888
commit 0caa50056a
14 changed files with 291 additions and 27 deletions

View File

@@ -18,16 +18,16 @@ package rest
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"os"
"reflect"
"testing"
"time"
"fmt"
v1 "k8s.io/api/core/v1"
v1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
@@ -37,6 +37,8 @@ import (
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/client-go/kubernetes/scheme"
utiltesting "k8s.io/client-go/util/testing"
"github.com/google/go-cmp/cmp"
)
type TestParam struct {
@@ -252,7 +254,7 @@ func validate(testParam TestParam, t *testing.T, body []byte, fakeHandler *utilt
}
func TestHttpMethods(t *testing.T) {
func TestHTTPMethods(t *testing.T) {
testServer, _, _ := testServerEnv(t, 200)
defer testServer.Close()
c, _ := restClient(testServer)
@@ -283,6 +285,57 @@ func TestHttpMethods(t *testing.T) {
}
}
func TestHTTPProxy(t *testing.T) {
ctx := context.Background()
testServer, fh, _ := testServerEnv(t, 200)
fh.ResponseBody = "backend data"
defer testServer.Close()
testProxyServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
to, err := url.Parse(req.RequestURI)
if err != nil {
t.Fatalf("err: %v", err)
}
w.Write([]byte("proxied: "))
httputil.NewSingleHostReverseProxy(to).ServeHTTP(w, req)
}))
defer testProxyServer.Close()
t.Logf(testProxyServer.URL)
u, err := url.Parse(testProxyServer.URL)
if err != nil {
t.Fatalf("Failed to parse test proxy server url: %v", err)
}
c, err := RESTClientFor(&Config{
Host: testServer.URL,
ContentConfig: ContentConfig{
GroupVersion: &v1.SchemeGroupVersion,
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
},
Proxy: http.ProxyURL(u),
Username: "user",
Password: "pass",
})
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
request := c.Get()
if request == nil {
t.Fatalf("Get: Object returned should not be nil")
}
b, err := request.DoRaw(ctx)
if err != nil {
t.Fatalf("unexpected err: %v", err)
}
if got, want := string(b), "proxied: backend data"; !cmp.Equal(got, want) {
t.Errorf("unexpected body: %v", cmp.Diff(want, got))
}
}
func TestCreateBackoffManager(t *testing.T) {
theUrl, _ := url.Parse("http://localhost")