Merge pull request #4279 from jimmidyson/proxy-tweaks

Configurable API prefix for kubectl proxy
This commit is contained in:
Jeff Lowdermilk 2015-02-10 11:21:22 -08:00
commit 96c9a15ed5
3 changed files with 11 additions and 3 deletions

View File

@ -49,6 +49,7 @@ Usage:
Available Flags: Available Flags:
--alsologtostderr=false: log to standard error as well as files --alsologtostderr=false: log to standard error as well as files
--api-prefix="/api/": Prefix to serve the proxied API under
--api-version="": The API version to use when talking to the server --api-version="": The API version to use when talking to the server
-a, --auth-path="": Path to the auth info file. If missing, prompt the user. Only used if using https. -a, --auth-path="": Path to the auth info file. If missing, prompt the user. Only used if using https.
--certificate-authority="": Path to a cert. file for the certificate authority. --certificate-authority="": Path to a cert. file for the certificate authority.

View File

@ -42,13 +42,20 @@ func (f *Factory) NewCmdProxy(out io.Writer) *cobra.Command {
if !strings.HasSuffix(staticPrefix, "/") { if !strings.HasSuffix(staticPrefix, "/") {
staticPrefix += "/" staticPrefix += "/"
} }
server, err := kubectl.NewProxyServer(util.GetFlagString(cmd, "www"), staticPrefix, clientConfig)
apiProxyPrefix := util.GetFlagString(cmd, "api-prefix")
if !strings.HasSuffix(apiProxyPrefix, "/") {
apiProxyPrefix += "/"
}
server, err := kubectl.NewProxyServer(util.GetFlagString(cmd, "www"), apiProxyPrefix, staticPrefix, clientConfig)
checkErr(err) checkErr(err)
glog.Fatal(server.Serve(port)) glog.Fatal(server.Serve(port))
}, },
} }
cmd.Flags().StringP("www", "w", "", "Also serve static files from the given directory under the specified prefix") cmd.Flags().StringP("www", "w", "", "Also serve static files from the given directory under the specified prefix")
cmd.Flags().StringP("www-prefix", "P", "/static/", "Prefix to serve static files under, if static file dir is specified") cmd.Flags().StringP("www-prefix", "P", "/static/", "Prefix to serve static files under, if static file dir is specified")
cmd.Flags().StringP("api-prefix", "", "/api/", "Prefix to serve the proxied API under")
cmd.Flags().IntP("port", "p", 8001, "The port on which to run the proxy") cmd.Flags().IntP("port", "p", 8001, "The port on which to run the proxy")
return cmd return cmd
} }

View File

@ -33,7 +33,7 @@ type ProxyServer struct {
// NewProxyServer creates and installs a new ProxyServer. // NewProxyServer creates and installs a new ProxyServer.
// It automatically registers the created ProxyServer to http.DefaultServeMux. // It automatically registers the created ProxyServer to http.DefaultServeMux.
func NewProxyServer(filebase string, staticPrefix string, cfg *client.Config) (*ProxyServer, error) { func NewProxyServer(filebase string, apiProxyPrefix string, staticPrefix string, cfg *client.Config) (*ProxyServer, error) {
prefix := cfg.Prefix prefix := cfg.Prefix
if prefix == "" { if prefix == "" {
prefix = "/api" prefix = "/api"
@ -46,7 +46,7 @@ func NewProxyServer(filebase string, staticPrefix string, cfg *client.Config) (*
if proxy.Transport, err = client.TransportFor(cfg); err != nil { if proxy.Transport, err = client.TransportFor(cfg); err != nil {
return nil, err return nil, err
} }
http.Handle("/api/", http.StripPrefix("/api/", proxy)) http.Handle(apiProxyPrefix, http.StripPrefix(apiProxyPrefix, proxy))
http.Handle(staticPrefix, newFileHandler(staticPrefix, filebase)) http.Handle(staticPrefix, newFileHandler(staticPrefix, filebase))
return proxy, nil return proxy, nil
} }