...

Source file src/k8s.io/kubernetes/pkg/registry/core/service/proxy.go

Documentation: k8s.io/kubernetes/pkg/registry/core/service

     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 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  // ProxyREST implements the proxy subresource for a Service
    34  type ProxyREST struct {
    35  	Redirector     rest.Redirector
    36  	ProxyTransport http.RoundTripper
    37  }
    38  
    39  // Implement Connecter
    40  var _ = rest.Connecter(&ProxyREST{})
    41  
    42  var proxyMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"}
    43  
    44  // New returns an empty service resource
    45  func (r *ProxyREST) New() runtime.Object {
    46  	return &api.ServiceProxyOptions{}
    47  }
    48  
    49  // Destroy cleans up resources on shutdown.
    50  func (r *ProxyREST) Destroy() {
    51  	// Given no underlying store, we don't destroy anything
    52  	// here explicitly.
    53  }
    54  
    55  // ConnectMethods returns the list of HTTP methods that can be proxied
    56  func (r *ProxyREST) ConnectMethods() []string {
    57  	return proxyMethods
    58  }
    59  
    60  // NewConnectOptions returns versioned resource that represents proxy parameters
    61  func (r *ProxyREST) NewConnectOptions() (runtime.Object, bool, string) {
    62  	return &api.ServiceProxyOptions{}, true, "path"
    63  }
    64  
    65  // Connect returns a handler for the service proxy
    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  	// Return a proxy handler that uses the desired transport, wrapped with additional proxy handling (to get URL rewriting, X-Forwarded-* headers, etc)
    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