k3s/pkg/kubecfg/proxy_server.go

81 lines
2.3 KiB
Go
Raw Normal View History

2014-06-24 02:57:54 +00:00
/*
Copyright 2014 Google Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2014-06-26 00:55:43 +00:00
package kubecfg
2014-06-24 02:57:54 +00:00
import (
2014-06-24 05:18:14 +00:00
"fmt"
2014-06-24 02:57:54 +00:00
"net/http"
2014-06-24 05:18:14 +00:00
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
2014-06-24 02:57:54 +00:00
)
2014-07-10 11:48:48 +00:00
// ProxyServer is a http.Handler which proxies Kubenetes APIs to remote API server.
2014-06-24 02:57:54 +00:00
type ProxyServer struct {
Host string
2014-06-24 05:18:14 +00:00
Auth *client.AuthInfo
Client *client.Client
2014-06-24 02:57:54 +00:00
}
2014-06-24 20:36:57 +00:00
func makeFileHandler(prefix, base string) http.Handler {
return http.StripPrefix(prefix, http.FileServer(http.Dir(base)))
}
2014-07-10 11:48:48 +00:00
// NewProxyServer creates and installs a new ProxyServer.
// It automatically registers the created ProxyServer to http.DefaultServeMux.
2014-06-24 05:18:14 +00:00
func NewProxyServer(filebase, host string, auth *client.AuthInfo) *ProxyServer {
server := &ProxyServer{
Host: host,
Auth: auth,
Client: client.New(host, auth),
2014-06-24 02:57:54 +00:00
}
2014-06-24 05:18:14 +00:00
http.Handle("/api/", server)
2014-06-24 20:36:57 +00:00
http.Handle("/static/", makeFileHandler("/static/", filebase))
2014-06-24 05:18:14 +00:00
return server
}
2014-07-10 11:48:48 +00:00
// Serve starts the server (http.DefaultServeMux) on TCP port 8001, loops forever.
2014-06-24 05:18:14 +00:00
func (s *ProxyServer) Serve() error {
return http.ListenAndServe(":8001", nil)
}
func (s *ProxyServer) doError(w http.ResponseWriter, err error) {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Add("Content-type", "application/json")
data, _ := api.Encode(api.Status{
Status: api.StatusFailure,
Details: fmt.Sprintf("internal error: %#v", err),
})
w.Write(data)
}
func (s *ProxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
2014-06-24 21:57:09 +00:00
result := s.Client.Verb(r.Method).AbsPath(r.URL.Path).Body(r.Body).Do()
2014-06-24 05:18:14 +00:00
if result.Error() != nil {
s.doError(w, result.Error())
2014-06-24 02:57:54 +00:00
return
}
2014-06-24 05:18:14 +00:00
data, err := result.Raw()
2014-06-24 02:57:54 +00:00
if err != nil {
2014-06-24 05:18:14 +00:00
s.doError(w, err)
2014-06-24 02:57:54 +00:00
return
}
2014-06-24 05:18:14 +00:00
w.Header().Add("Content-type", "application/json")
2014-06-24 05:21:13 +00:00
w.WriteHeader(http.StatusOK)
2014-06-24 02:57:54 +00:00
w.Write(data)
}