|
| 1 | +package origin |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "net/url" |
| 8 | + |
| 9 | + "k8s.io/apimachinery/pkg/runtime" |
| 10 | + utilnet "k8s.io/apimachinery/pkg/util/net" |
| 11 | + "k8s.io/apiserver/pkg/endpoints/handlers/responsewriters" |
| 12 | + genericrest "k8s.io/apiserver/pkg/registry/generic/rest" |
| 13 | + restclient "k8s.io/client-go/rest" |
| 14 | +) |
| 15 | + |
| 16 | +// A ServiceResolver knows how to get a URL given a service. |
| 17 | +type ServiceResolver interface { |
| 18 | + ResolveEndpoint(namespace, name string) (*url.URL, error) |
| 19 | +} |
| 20 | + |
| 21 | +// proxyHandler provides a http.Handler which will proxy traffic to locations |
| 22 | +// specified by items implementing Redirector. |
| 23 | +type webConsoleProxyHandler struct { |
| 24 | + // Endpoints based routing to map from cluster IP to routable IP |
| 25 | + serviceResolver ServiceResolver |
| 26 | + |
| 27 | + // proxyRoundTripper is the re-useable portion of the transport. It does not vary with any request. |
| 28 | + proxyRoundTripper http.RoundTripper |
| 29 | + |
| 30 | + restConfig *restclient.Config |
| 31 | +} |
| 32 | + |
| 33 | +const ( |
| 34 | + serviceName = "webconsole" |
| 35 | + serviceNamespace = "openshift-web-console" |
| 36 | +) |
| 37 | + |
| 38 | +func NewWebConsoleProxyHandler(serviceResolver ServiceResolver, caBundle []byte) (*webConsoleProxyHandler, error) { |
| 39 | + restConfig := &restclient.Config{ |
| 40 | + TLSClientConfig: restclient.TLSClientConfig{ |
| 41 | + ServerName: serviceName + "." + serviceNamespace + ".svc", |
| 42 | + CAData: caBundle, |
| 43 | + }, |
| 44 | + } |
| 45 | + proxyRoundTripper, err := restclient.TransportFor(restConfig) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + return &webConsoleProxyHandler{ |
| 51 | + serviceResolver: serviceResolver, |
| 52 | + proxyRoundTripper: proxyRoundTripper, |
| 53 | + restConfig: restConfig, |
| 54 | + }, nil |
| 55 | +} |
| 56 | + |
| 57 | +func (r *webConsoleProxyHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 58 | + // write a new location based on the existing request pointed at the target service |
| 59 | + location := &url.URL{} |
| 60 | + location.Scheme = "https" |
| 61 | + rloc, err := r.serviceResolver.ResolveEndpoint(serviceNamespace, serviceName) |
| 62 | + if err != nil { |
| 63 | + http.Error(w, fmt.Sprintf("missing route (%s)", err.Error()), http.StatusInternalServerError) |
| 64 | + return |
| 65 | + } |
| 66 | + location.Host = rloc.Host |
| 67 | + location.Path = req.URL.Path |
| 68 | + location.RawQuery = req.URL.Query().Encode() |
| 69 | + |
| 70 | + // WithContext creates a shallow clone of the request with the new context. |
| 71 | + newReq := req.WithContext(context.Background()) |
| 72 | + newReq.Header = utilnet.CloneHeader(req.Header) |
| 73 | + newReq.URL = location |
| 74 | + |
| 75 | + handler := genericrest.NewUpgradeAwareProxyHandler(location, r.proxyRoundTripper, true, false, &responder{w: w}) |
| 76 | + handler.ServeHTTP(w, newReq) |
| 77 | +} |
| 78 | + |
| 79 | +// responder implements rest.Responder for assisting a connector in writing objects or errors. |
| 80 | +type responder struct { |
| 81 | + w http.ResponseWriter |
| 82 | +} |
| 83 | + |
| 84 | +// TODO this should properly handle content type negotiation |
| 85 | +// if the caller asked for protobuf and you write JSON bad things happen. |
| 86 | +func (r *responder) Object(statusCode int, obj runtime.Object) { |
| 87 | + responsewriters.WriteRawJSON(statusCode, obj, r.w) |
| 88 | +} |
| 89 | + |
| 90 | +func (r *responder) Error(err error) { |
| 91 | + http.Error(r.w, err.Error(), http.StatusInternalServerError) |
| 92 | +} |
0 commit comments