k8s.io/kubernetes@v1.29.3/pkg/registry/core/node/rest/proxy.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package node 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 genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" 29 "k8s.io/apiserver/pkg/registry/rest" 30 api "k8s.io/kubernetes/pkg/apis/core" 31 "k8s.io/kubernetes/pkg/capabilities" 32 "k8s.io/kubernetes/pkg/kubelet/client" 33 "k8s.io/kubernetes/pkg/registry/core/node" 34 ) 35 36 // ProxyREST implements the proxy subresource for a Node 37 type ProxyREST struct { 38 Store *genericregistry.Store 39 Connection client.ConnectionInfoGetter 40 ProxyTransport http.RoundTripper 41 } 42 43 // Implement Connecter 44 var _ = rest.Connecter(&ProxyREST{}) 45 46 var proxyMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"} 47 48 // New returns an empty nodeProxyOptions object. 49 func (r *ProxyREST) New() runtime.Object { 50 return &api.NodeProxyOptions{} 51 } 52 53 // Destroy cleans up resources on shutdown. 54 func (r *ProxyREST) Destroy() { 55 // Given that underlying store is shared with REST, 56 // we don't destroy it here explicitly. 57 } 58 59 // ConnectMethods returns the list of HTTP methods that can be proxied 60 func (r *ProxyREST) ConnectMethods() []string { 61 return proxyMethods 62 } 63 64 // NewConnectOptions returns versioned resource that represents proxy parameters 65 func (r *ProxyREST) NewConnectOptions() (runtime.Object, bool, string) { 66 return &api.NodeProxyOptions{}, true, "path" 67 } 68 69 // Connect returns a handler for the node proxy 70 func (r *ProxyREST) Connect(ctx context.Context, id string, opts runtime.Object, responder rest.Responder) (http.Handler, error) { 71 proxyOpts, ok := opts.(*api.NodeProxyOptions) 72 if !ok { 73 return nil, fmt.Errorf("Invalid options object: %#v", opts) 74 } 75 location, transport, err := node.ResourceLocation(r.Store, r.Connection, r.ProxyTransport, ctx, id) 76 if err != nil { 77 return nil, err 78 } 79 location.Path = net.JoinPreservingTrailingSlash(location.Path, proxyOpts.Path) 80 // Return a proxy handler that uses the desired transport, wrapped with additional proxy handling (to get URL rewriting, X-Forwarded-* headers, etc) 81 return newThrottledUpgradeAwareProxyHandler(location, transport, true, false, responder), nil 82 } 83 84 func newThrottledUpgradeAwareProxyHandler(location *url.URL, transport http.RoundTripper, wrapTransport, upgradeRequired bool, responder rest.Responder) *proxy.UpgradeAwareHandler { 85 handler := proxy.NewUpgradeAwareHandler(location, transport, wrapTransport, upgradeRequired, proxy.NewErrorResponder(responder)) 86 handler.MaxBytesPerSec = capabilities.Get().PerConnectionBandwidthLimitBytesPerSec 87 return handler 88 }