-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
proxy.go
96 lines (79 loc) · 2.71 KB
/
proxy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package k8s
import (
"fmt"
"net"
"net/url"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
"k8s.io/kubernetes/pkg/kubectl/proxy"
// Load all the auth plugins for the cloud providers.
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
type KubernetesProxy struct {
listener net.Listener
server *proxy.Server
}
// NewProxy returns a new KubernetesProxy object and starts listening on a
// network address.
func NewProxy(configPath string, proxyPort int) (*KubernetesProxy, error) {
config, err := getConfig(configPath)
if err != nil {
return nil, fmt.Errorf("error configuring Kubernetes API client: %v", err)
}
server, err := proxyCreate(config)
if err != nil {
return nil, fmt.Errorf("Failed to create proxy: %+v", err)
}
listener, err := proxyListen(server, proxyPort)
if err != nil {
return nil, fmt.Errorf("Failed to listen with proxy: %+v", err)
}
return &KubernetesProxy{
listener: listener,
server: server,
}, nil
}
// Run starts proxying a connection to Kubernetes, and blocks until the process
// exits.
func (kp *KubernetesProxy) Run() error {
// blocks until process is killed
err := proxyServe(kp.server, kp.listener)
if err != nil {
return fmt.Errorf("Failed to serve with proxy: %+v", err)
}
return nil
}
// URLFor generates a URL based on the configured KubernetesProxy.
func (kp *KubernetesProxy) URLFor(namespace string, extraPathStartingWithSlash string) (*url.URL, error) {
schemeHostAndPort := fmt.Sprintf("http://127.0.0.1:%d", kp.listener.Addr().(*net.TCPAddr).Port)
return generateKubernetesApiBaseUrlFor(schemeHostAndPort, namespace, extraPathStartingWithSlash)
}
func proxyCreate(config *rest.Config) (*proxy.Server, error) {
filter := &proxy.FilterServer{
AcceptPaths: proxy.MakeRegexpArrayOrDie(proxy.DefaultPathAcceptRE),
RejectPaths: proxy.MakeRegexpArrayOrDie(proxy.DefaultPathRejectRE),
AcceptHosts: proxy.MakeRegexpArrayOrDie(proxy.DefaultHostAcceptRE),
RejectMethods: proxy.MakeRegexpArrayOrDie(proxy.DefaultMethodRejectRE),
}
server, err := proxy.NewServer("", "/", "/static/", filter, config)
if err != nil {
return nil, fmt.Errorf("Failed to create proxy server: %+v", err)
}
return server, nil
}
func proxyListen(server *proxy.Server, proxyPort int) (net.Listener, error) {
listener, err := server.Listen("127.0.0.1", proxyPort)
if err != nil {
return nil, fmt.Errorf("Failed to listen via proxy server: %+v", err)
}
return listener, nil
}
func proxyServe(server *proxy.Server, listener net.Listener) error {
log.Infof("Starting to serve on %s", listener.Addr().String())
// blocks until process is killed
err := server.ServeOnListener(listener)
if err != nil {
return fmt.Errorf("Failed to serve via proxy server: %+v", err)
}
return nil
}