...
1
16
17 package service
18
19 import (
20 "context"
21 "fmt"
22 "net/http"
23 "net/url"
24
25 "k8s.io/apimachinery/pkg/runtime"
26 "k8s.io/apimachinery/pkg/util/net"
27 "k8s.io/apimachinery/pkg/util/proxy"
28 "k8s.io/apiserver/pkg/registry/rest"
29 api "k8s.io/kubernetes/pkg/apis/core"
30 "k8s.io/kubernetes/pkg/capabilities"
31 )
32
33
34 type ProxyREST struct {
35 Redirector rest.Redirector
36 ProxyTransport http.RoundTripper
37 }
38
39
40 var _ = rest.Connecter(&ProxyREST{})
41
42 var proxyMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
43
44
45 func (r *ProxyREST) New() runtime.Object {
46 return &api.ServiceProxyOptions{}
47 }
48
49
50 func (r *ProxyREST) Destroy() {
51
52
53 }
54
55
56 func (r *ProxyREST) ConnectMethods() []string {
57 return proxyMethods
58 }
59
60
61 func (r *ProxyREST) NewConnectOptions() (runtime.Object, bool, string) {
62 return &api.ServiceProxyOptions{}, true, "path"
63 }
64
65
66 func (r *ProxyREST) Connect(ctx context.Context, id string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
67 proxyOpts, ok := opts.(*api.ServiceProxyOptions)
68 if !ok {
69 return nil, fmt.Errorf("Invalid options object: %#v", opts)
70 }
71 location, transport, err := r.Redirector.ResourceLocation(ctx, id)
72 if err != nil {
73 return nil, err
74 }
75 location.Path = net.JoinPreservingTrailingSlash(location.Path, proxyOpts.Path)
76
77 return newThrottledUpgradeAwareProxyHandler(location, transport, true, false, responder), nil
78 }
79
80 func newThrottledUpgradeAwareProxyHandler(location *url.URL, transport http.RoundTripper, wrapTransport, upgradeRequired bool, responder rest.Responder) *proxy.UpgradeAwareHandler {
81 handler := proxy.NewUpgradeAwareHandler(location, transport, wrapTransport, upgradeRequired, proxy.NewErrorResponder(responder))
82 handler.MaxBytesPerSec = capabilities.Get().PerConnectionBandwidthLimitBytesPerSec
83 return handler
84 }
85
View as plain text