Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
fc80fd68
Commit
fc80fd68
authored
Jun 04, 2015
by
Quinton Hoole
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9233 from lavalamp/no-ro-nonbreaking
Allow kubectl proxy to proxy everything
parents
1a679bcf
d615bbb2
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
131 additions
and
15 deletions
+131
-15
prometheus-all.json
contrib/prometheus/prometheus-all.json
+2
-2
kubectl_proxy.md
docs/kubectl_proxy.md
+17
-2
kubectl-proxy.1
docs/man/man1/kubectl-proxy.1
+22
-1
proxy.go
pkg/kubectl/cmd/proxy.go
+16
-1
proxy_server.go
pkg/kubectl/proxy_server.go
+20
-9
proxy_server_test.go
pkg/kubectl/proxy_server_test.go
+54
-0
No files found.
contrib/prometheus/prometheus-all.json
View file @
fc80fd68
...
@@ -73,9 +73,9 @@
...
@@ -73,9 +73,9 @@
},
},
{
{
"name"
:
"kubectl"
,
"name"
:
"kubectl"
,
"image"
:
"gcr.io/google_containers/kubectl:v0.18.0-
120-gaeb4ac55ad12b1-dirty
"
,
"image"
:
"gcr.io/google_containers/kubectl:v0.18.0-
350-gfb3305edcf6c1a
"
,
"args"
:
[
"args"
:
[
"proxy"
,
"-p"
,
"8001"
"proxy"
,
"-p"
,
"8001"
,
"--api-prefix=/"
]
]
}
}
],
],
...
...
docs/kubectl_proxy.md
View file @
fc80fd68
...
@@ -5,7 +5,22 @@ Run a proxy to the Kubernetes API server
...
@@ -5,7 +5,22 @@ Run a proxy to the Kubernetes API server
### Synopsis
### Synopsis
Run a proxy to the Kubernetes API server.
To proxy all of the kubernetes api and nothing else, use:
kubectl proxy --api-prefix=/
To proxy only part of the kubernetes api and also some static files:
kubectl proxy --www=/my/files --www-prefix=/static/ --api-prefix=/api/
The above lets you 'curl localhost:8001/api/v1/pods'.
To proxy the entire kubernetes api at a different root, use:
kubectl proxy --api-prefix=/custom/
The above lets you 'curl localhost:8001/custom/api/v1/pods'
```
```
kubectl proxy [--port=PORT] [--www=static-dir] [--www-prefix=prefix] [--api-prefix=prefix]
kubectl proxy [--port=PORT] [--www=static-dir] [--www-prefix=prefix] [--api-prefix=prefix]
...
@@ -64,6 +79,6 @@ $ kubectl proxy --api-prefix=/k8s-api
...
@@ -64,6 +79,6 @@ $ kubectl proxy --api-prefix=/k8s-api
### SEE ALSO
### SEE ALSO
*
[
kubectl
](
kubectl.md
)
- kubectl controls the Kubernetes cluster manager
*
[
kubectl
](
kubectl.md
)
- kubectl controls the Kubernetes cluster manager
###### Auto generated by spf13/cobra at 2015-0
5-28 20:57:46.689818993
+0000 UTC
###### Auto generated by spf13/cobra at 2015-0
6-04 01:34:05.594492715
+0000 UTC
[

]()
[

]()
docs/man/man1/kubectl-proxy.1
View file @
fc80fd68
...
@@ -13,7 +13,28 @@ kubectl proxy \- Run a proxy to the Kubernetes API server
...
@@ -13,7 +13,28 @@ kubectl proxy \- Run a proxy to the Kubernetes API server
.SH DESCRIPTION
.SH DESCRIPTION
.PP
.PP
Run a proxy to the Kubernetes API server.
To proxy all of the kubernetes api and nothing else, use:
.PP
kubectl proxy \-\-api\-prefix=/
.PP
To proxy only part of the kubernetes api and also some static files:
.PP
kubectl proxy \-\-www=/my/files \-\-www\-prefix=/static/ \-\-api\-prefix=/api/
.PP
The above lets you 'curl localhost:8001/api/v1/pods'.
.PP
To proxy the entire kubernetes api at a different root, use:
.PP
kubectl proxy \-\-api\-prefix=/custom/
.PP
The above lets you 'curl localhost:8001/custom/api/v1/pods'
.SH OPTIONS
.SH OPTIONS
...
...
pkg/kubectl/cmd/proxy.go
View file @
fc80fd68
...
@@ -40,7 +40,22 @@ func NewCmdProxy(f *cmdutil.Factory, out io.Writer) *cobra.Command {
...
@@ -40,7 +40,22 @@ func NewCmdProxy(f *cmdutil.Factory, out io.Writer) *cobra.Command {
cmd
:=
&
cobra
.
Command
{
cmd
:=
&
cobra
.
Command
{
Use
:
"proxy [--port=PORT] [--www=static-dir] [--www-prefix=prefix] [--api-prefix=prefix]"
,
Use
:
"proxy [--port=PORT] [--www=static-dir] [--www-prefix=prefix] [--api-prefix=prefix]"
,
Short
:
"Run a proxy to the Kubernetes API server"
,
Short
:
"Run a proxy to the Kubernetes API server"
,
Long
:
`Run a proxy to the Kubernetes API server. `
,
Long
:
`To proxy all of the kubernetes api and nothing else, use:
kubectl proxy --api-prefix=/
To proxy only part of the kubernetes api and also some static files:
kubectl proxy --www=/my/files --www-prefix=/static/ --api-prefix=/api/
The above lets you 'curl localhost:8001/api/v1/pods'.
To proxy the entire kubernetes api at a different root, use:
kubectl proxy --api-prefix=/custom/
The above lets you 'curl localhost:8001/custom/api/v1/pods'
`
,
Example
:
proxy_example
,
Example
:
proxy_example
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
err
:=
RunProxy
(
f
,
out
,
cmd
)
err
:=
RunProxy
(
f
,
out
,
cmd
)
...
...
pkg/kubectl/proxy_server.go
View file @
fc80fd68
...
@@ -28,17 +28,18 @@ import (
...
@@ -28,17 +28,18 @@ import (
// ProxyServer is a http.Handler which proxies Kubernetes APIs to remote API server.
// ProxyServer is a http.Handler which proxies Kubernetes APIs to remote API server.
type
ProxyServer
struct
{
type
ProxyServer
struct
{
mux
*
http
.
ServeMux
httputil
.
ReverseProxy
httputil
.
ReverseProxy
}
}
// 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
,
apiProxyPrefix
string
,
staticPrefix
string
,
cfg
*
client
.
Config
)
(
*
ProxyServer
,
error
)
{
func
NewProxyServer
(
filebase
string
,
apiProxyPrefix
string
,
staticPrefix
string
,
cfg
*
client
.
Config
)
(
*
ProxyServer
,
error
)
{
prefix
:=
cfg
.
Prefix
host
:=
cfg
.
Host
if
prefix
==
""
{
if
!
strings
.
HasSuffix
(
host
,
"/"
)
{
prefix
=
"/api
"
host
=
host
+
"/
"
}
}
target
,
err
:=
url
.
Parse
(
singleJoiningSlash
(
cfg
.
Host
,
prefix
)
)
target
,
err
:=
url
.
Parse
(
host
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
...
@@ -46,15 +47,22 @@ func NewProxyServer(filebase string, apiProxyPrefix string, staticPrefix string,
...
@@ -46,15 +47,22 @@ func NewProxyServer(filebase string, apiProxyPrefix string, staticPrefix string,
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
(
apiProxyPrefix
,
http
.
StripPrefix
(
apiProxyPrefix
,
proxy
))
if
strings
.
HasPrefix
(
apiProxyPrefix
,
"/api"
)
{
http
.
Handle
(
staticPrefix
,
newFileHandler
(
staticPrefix
,
filebase
))
proxy
.
mux
.
Handle
(
apiProxyPrefix
,
proxy
)
}
else
{
proxy
.
mux
.
Handle
(
apiProxyPrefix
,
http
.
StripPrefix
(
apiProxyPrefix
,
proxy
))
}
proxy
.
mux
.
Handle
(
staticPrefix
,
newFileHandler
(
staticPrefix
,
filebase
))
return
proxy
,
nil
return
proxy
,
nil
}
}
// Serve starts the server (http.DefaultServeMux) on given port, loops forever.
// Serve starts the server (http.DefaultServeMux) on given port, loops forever.
func
(
s
*
ProxyServer
)
Serve
(
port
int
)
error
{
func
(
s
*
ProxyServer
)
Serve
(
port
int
)
error
{
addr
:=
fmt
.
Sprintf
(
":%d"
,
port
)
server
:=
http
.
Server
{
return
http
.
ListenAndServe
(
addr
,
nil
)
Addr
:
fmt
.
Sprintf
(
":%d"
,
port
),
Handler
:
s
.
mux
,
}
return
server
.
ListenAndServe
()
}
}
func
newProxyServer
(
target
*
url
.
URL
)
*
ProxyServer
{
func
newProxyServer
(
target
*
url
.
URL
)
*
ProxyServer
{
...
@@ -63,7 +71,10 @@ func newProxyServer(target *url.URL) *ProxyServer {
...
@@ -63,7 +71,10 @@ func newProxyServer(target *url.URL) *ProxyServer {
req
.
URL
.
Host
=
target
.
Host
req
.
URL
.
Host
=
target
.
Host
req
.
URL
.
Path
=
singleJoiningSlash
(
target
.
Path
,
req
.
URL
.
Path
)
req
.
URL
.
Path
=
singleJoiningSlash
(
target
.
Path
,
req
.
URL
.
Path
)
}
}
return
&
ProxyServer
{
ReverseProxy
:
httputil
.
ReverseProxy
{
Director
:
director
}}
return
&
ProxyServer
{
ReverseProxy
:
httputil
.
ReverseProxy
{
Director
:
director
},
mux
:
http
.
NewServeMux
(),
}
}
}
func
newFileHandler
(
prefix
,
base
string
)
http
.
Handler
{
func
newFileHandler
(
prefix
,
base
string
)
http
.
Handler
{
...
...
pkg/kubectl/proxy_server_test.go
View file @
fc80fd68
...
@@ -25,6 +25,8 @@ import (
...
@@ -25,6 +25,8 @@ import (
"path/filepath"
"path/filepath"
"strings"
"strings"
"testing"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
)
)
func
TestFileServing
(
t
*
testing
.
T
)
{
func
TestFileServing
(
t
*
testing
.
T
)
{
...
@@ -104,3 +106,55 @@ func TestAPIRequests(t *testing.T) {
...
@@ -104,3 +106,55 @@ func TestAPIRequests(t *testing.T) {
}
}
}
}
}
}
func
TestPathHandling
(
t
*
testing
.
T
)
{
ts
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
fmt
.
Fprint
(
w
,
r
.
URL
.
Path
)
}))
defer
ts
.
Close
()
table
:=
[]
struct
{
prefix
string
reqPath
string
expectPath
string
}{
{
"/api/"
,
"/metrics"
,
"404 page not found
\n
"
},
{
"/api/"
,
"/api/metrics"
,
"/api/metrics"
},
{
"/api/"
,
"/api/v1/pods/"
,
"/api/v1/pods/"
},
{
"/"
,
"/metrics"
,
"/metrics"
},
{
"/"
,
"/api/v1/pods/"
,
"/api/v1/pods/"
},
{
"/custom/"
,
"/metrics"
,
"404 page not found
\n
"
},
{
"/custom/"
,
"/api/metrics"
,
"404 page not found
\n
"
},
{
"/custom/"
,
"/api/v1/pods/"
,
"404 page not found
\n
"
},
{
"/custom/"
,
"/custom/api/metrics"
,
"/api/metrics"
},
{
"/custom/"
,
"/custom/api/v1/pods/"
,
"/api/v1/pods/"
},
}
cc
:=
&
client
.
Config
{
Host
:
ts
.
URL
,
}
for
_
,
item
:=
range
table
{
func
()
{
p
,
err
:=
NewProxyServer
(
""
,
item
.
prefix
,
"/not/used/for/this/test"
,
cc
)
if
err
!=
nil
{
t
.
Fatalf
(
"%#v: %v"
,
item
,
err
)
}
pts
:=
httptest
.
NewServer
(
p
.
mux
)
defer
pts
.
Close
()
r
,
err
:=
http
.
Get
(
pts
.
URL
+
item
.
reqPath
)
if
err
!=
nil
{
t
.
Fatalf
(
"%#v: %v"
,
item
,
err
)
}
body
,
err
:=
ioutil
.
ReadAll
(
r
.
Body
)
r
.
Body
.
Close
()
if
err
!=
nil
{
t
.
Fatalf
(
"%#v: %v"
,
item
,
err
)
}
if
e
,
a
:=
item
.
expectPath
,
string
(
body
);
e
!=
a
{
t
.
Errorf
(
"%#v: Wanted %q, got %q"
,
item
,
e
,
a
)
}
}()
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment