proxy.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 service
  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. "k8s.io/apiserver/pkg/registry/rest"
  23. api "k8s.io/kubernetes/pkg/apis/core"
  24. "k8s.io/kubernetes/pkg/capabilities"
  25. )
  26. // ProxyREST implements the proxy subresource for a Service
  27. type ProxyREST struct {
  28. Redirector rest.Redirector
  29. ProxyTransport http.RoundTripper
  30. }
  31. // Implement Connecter
  32. var _ = rest.Connecter(&ProxyREST{})
  33. var proxyMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
  34. // New returns an empty service resource
  35. func (r *ProxyREST) New() runtime.Object {
  36. return &api.ServiceProxyOptions{}
  37. }
  38. // ConnectMethods returns the list of HTTP methods that can be proxied
  39. func (r *ProxyREST) ConnectMethods() []string {
  40. return proxyMethods
  41. }
  42. // NewConnectOptions returns versioned resource that represents proxy parameters
  43. func (r *ProxyREST) NewConnectOptions() (runtime.Object, bool, string) {
  44. return &api.ServiceProxyOptions{}, true, "path"
  45. }
  46. // Connect returns a handler for the service proxy
  47. func (r *ProxyREST) Connect(ctx context.Context, id string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
  48. proxyOpts, ok := opts.(*api.ServiceProxyOptions)
  49. if !ok {
  50. return nil, fmt.Errorf("Invalid options object: %#v", opts)
  51. }
  52. location, transport, err := r.Redirector.ResourceLocation(ctx, id)
  53. if err != nil {
  54. return nil, err
  55. }
  56. location.Path = net.JoinPreservingTrailingSlash(location.Path, proxyOpts.Path)
  57. // Return a proxy handler that uses the desired transport, wrapped with additional proxy handling (to get URL rewriting, X-Forwarded-* headers, etc)
  58. return newThrottledUpgradeAwareProxyHandler(location, transport, true, false, responder), nil
  59. }
  60. func newThrottledUpgradeAwareProxyHandler(location *url.URL, transport http.RoundTripper, wrapTransport, upgradeRequired bool, responder rest.Responder) *proxy.UpgradeAwareHandler {
  61. handler := proxy.NewUpgradeAwareHandler(location, transport, wrapTransport, upgradeRequired, proxy.NewErrorResponder(responder))
  62. handler.MaxBytesPerSec = capabilities.Get().PerConnectionBandwidthLimitBytesPerSec
  63. return handler
  64. }