proxy.go 2.9 KB

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