mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 13:02:14 +00:00
Merge pull request #1557 from smarterclayton/allow_prefix
Allow RESTClient to define arbitrary (non-default) prefix in config
This commit is contained in:
commit
2a412a1b82
@ -31,6 +31,11 @@ import (
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
// Host must be a host string, a host:port pair, or a URL to the base of the API.
|
// Host must be a host string, a host:port pair, or a URL to the base of the API.
|
||||||
Host string
|
Host string
|
||||||
|
// Prefix is the sub path of the server. If not specified, the client will set
|
||||||
|
// a default value. Use "/" to indicate the server root should be used
|
||||||
|
Prefix string
|
||||||
|
// Version is the API version to talk to. If not specified, the client will use
|
||||||
|
// the preferred version.
|
||||||
Version string
|
Version string
|
||||||
|
|
||||||
// Server requires Basic authentication
|
// Server requires Basic authentication
|
||||||
@ -61,7 +66,11 @@ type Config struct {
|
|||||||
// and delete on these objects. An error is returned if the provided configuration
|
// and delete on these objects. An error is returned if the provided configuration
|
||||||
// is not valid.
|
// is not valid.
|
||||||
func New(c *Config) (*Client, error) {
|
func New(c *Config) (*Client, error) {
|
||||||
client, err := RESTClientFor(c)
|
config := *c
|
||||||
|
if config.Prefix == "" {
|
||||||
|
config.Prefix = "/api"
|
||||||
|
}
|
||||||
|
client, err := RESTClientFor(&config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -150,7 +159,7 @@ func TransportFor(config *Config) (http.RoundTripper, error) {
|
|||||||
// DefaultServerURL converts a host, host:port, or URL string to the default base server API path
|
// DefaultServerURL converts a host, host:port, or URL string to the default base server API path
|
||||||
// to use with a Client at a given API version following the standard conventions for a
|
// to use with a Client at a given API version following the standard conventions for a
|
||||||
// Kubernetes API.
|
// Kubernetes API.
|
||||||
func DefaultServerURL(host, version string, defaultSecure bool) (*url.URL, error) {
|
func DefaultServerURL(host, prefix, version string, defaultSecure bool) (*url.URL, error) {
|
||||||
if host == "" {
|
if host == "" {
|
||||||
return nil, fmt.Errorf("host must be a URL or a host:port pair")
|
return nil, fmt.Errorf("host must be a URL or a host:port pair")
|
||||||
}
|
}
|
||||||
@ -177,9 +186,12 @@ func DefaultServerURL(host, version string, defaultSecure bool) (*url.URL, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If the user specified a URL without a path component (http://server.com), automatically
|
// If the user specified a URL without a path component (http://server.com), automatically
|
||||||
// append the default API prefix
|
// append the default prefix
|
||||||
if hostURL.Path == "" {
|
if hostURL.Path == "" {
|
||||||
hostURL.Path = "/api"
|
if prefix == "" {
|
||||||
|
prefix = "/"
|
||||||
|
}
|
||||||
|
hostURL.Path = prefix
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the version to the end of the path
|
// Add the version to the end of the path
|
||||||
@ -211,7 +223,7 @@ func defaultServerUrlFor(config *Config) (*url.URL, error) {
|
|||||||
if host == "" {
|
if host == "" {
|
||||||
host = "localhost"
|
host = "localhost"
|
||||||
}
|
}
|
||||||
return DefaultServerURL(host, version, defaultSecure)
|
return DefaultServerURL(host, config.Prefix, version, defaultSecure)
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultVersionFor is shared between defaultServerUrlFor and RESTClientFor
|
// defaultVersionFor is shared between defaultServerUrlFor and RESTClientFor
|
||||||
|
@ -37,9 +37,9 @@ func TestChecksCodec(t *testing.T) {
|
|||||||
Prefix string
|
Prefix string
|
||||||
Codec runtime.Codec
|
Codec runtime.Codec
|
||||||
}{
|
}{
|
||||||
"v1beta1": {false, "/api/v1beta1/", v1beta1.Codec},
|
"v1beta1": {false, "/v1beta1/", v1beta1.Codec},
|
||||||
"": {false, "/api/v1beta1/", v1beta1.Codec},
|
"": {false, "/v1beta1/", v1beta1.Codec},
|
||||||
"v1beta2": {false, "/api/v1beta2/", v1beta2.Codec},
|
"v1beta2": {false, "/v1beta2/", v1beta2.Codec},
|
||||||
"v1beta3": {true, "", nil},
|
"v1beta3": {true, "", nil},
|
||||||
}
|
}
|
||||||
for version, expected := range testCases {
|
for version, expected := range testCases {
|
||||||
@ -64,31 +64,36 @@ func TestChecksCodec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestValidatesHostParameter(t *testing.T) {
|
func TestValidatesHostParameter(t *testing.T) {
|
||||||
testCases := map[string]struct {
|
testCases := []struct {
|
||||||
|
Host string
|
||||||
|
Prefix string
|
||||||
|
|
||||||
URL string
|
URL string
|
||||||
Err bool
|
Err bool
|
||||||
}{
|
}{
|
||||||
"127.0.0.1": {"http://127.0.0.1/api/v1beta1/", false},
|
{"127.0.0.1", "", "http://127.0.0.1/v1beta1/", false},
|
||||||
"127.0.0.1:8080": {"http://127.0.0.1:8080/api/v1beta1/", false},
|
{"127.0.0.1:8080", "", "http://127.0.0.1:8080/v1beta1/", false},
|
||||||
"foo.bar.com": {"http://foo.bar.com/api/v1beta1/", false},
|
{"foo.bar.com", "", "http://foo.bar.com/v1beta1/", false},
|
||||||
"http://host/prefix": {"http://host/prefix/v1beta1/", false},
|
{"http://host/prefix", "", "http://host/prefix/v1beta1/", false},
|
||||||
"http://host": {"http://host/api/v1beta1/", false},
|
{"http://host", "", "http://host/v1beta1/", false},
|
||||||
"host/server": {"", true},
|
{"http://host", "/", "http://host/v1beta1/", false},
|
||||||
|
{"http://host", "/other", "http://host/other/v1beta1/", false},
|
||||||
|
{"host/server", "", "", true},
|
||||||
}
|
}
|
||||||
for k, expected := range testCases {
|
for i, testCase := range testCases {
|
||||||
c, err := RESTClientFor(&Config{Host: k, Version: "v1beta1"})
|
c, err := RESTClientFor(&Config{Host: testCase.Host, Prefix: testCase.Prefix, Version: "v1beta1"})
|
||||||
switch {
|
switch {
|
||||||
case err == nil && expected.Err:
|
case err == nil && testCase.Err:
|
||||||
t.Errorf("expected error but was nil")
|
t.Errorf("expected error but was nil")
|
||||||
continue
|
continue
|
||||||
case err != nil && !expected.Err:
|
case err != nil && !testCase.Err:
|
||||||
t.Errorf("unexpected error %v", err)
|
t.Errorf("unexpected error %v", err)
|
||||||
continue
|
continue
|
||||||
case err != nil:
|
case err != nil:
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if e, a := expected.URL, c.baseURL.String(); e != a {
|
if e, a := testCase.URL, c.baseURL.String(); e != a {
|
||||||
t.Errorf("%s: expected host %s, got %s", k, e, a)
|
t.Errorf("%d: expected host %s, got %s", i, e, a)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user