Removing legacyBehavior param from pkg/client

This commit is contained in:
nikhiljindal 2015-06-15 15:15:55 -07:00
parent 19a7e87c06
commit 3d63c71cbc
8 changed files with 42 additions and 97 deletions

View File

@ -34,26 +34,25 @@ func (f HTTPClientFunc) Do(req *http.Request) (*http.Response, error) {
type FakeRESTClient struct { type FakeRESTClient struct {
Client HTTPClient Client HTTPClient
Codec runtime.Codec Codec runtime.Codec
Legacy bool
Req *http.Request Req *http.Request
Resp *http.Response Resp *http.Response
Err error Err error
} }
func (c *FakeRESTClient) Get() *Request { func (c *FakeRESTClient) Get() *Request {
return NewRequest(c, "GET", &url.URL{Host: "localhost"}, testapi.Version(), c.Codec, c.Legacy, c.Legacy) return NewRequest(c, "GET", &url.URL{Host: "localhost"}, testapi.Version(), c.Codec)
} }
func (c *FakeRESTClient) Put() *Request { func (c *FakeRESTClient) Put() *Request {
return NewRequest(c, "PUT", &url.URL{Host: "localhost"}, testapi.Version(), c.Codec, c.Legacy, c.Legacy) return NewRequest(c, "PUT", &url.URL{Host: "localhost"}, testapi.Version(), c.Codec)
} }
func (c *FakeRESTClient) Post() *Request { func (c *FakeRESTClient) Post() *Request {
return NewRequest(c, "POST", &url.URL{Host: "localhost"}, testapi.Version(), c.Codec, c.Legacy, c.Legacy) return NewRequest(c, "POST", &url.URL{Host: "localhost"}, testapi.Version(), c.Codec)
} }
func (c *FakeRESTClient) Delete() *Request { func (c *FakeRESTClient) Delete() *Request {
return NewRequest(c, "DELETE", &url.URL{Host: "localhost"}, testapi.Version(), c.Codec, c.Legacy, c.Legacy) return NewRequest(c, "DELETE", &url.URL{Host: "localhost"}, testapi.Version(), c.Codec)
} }
func (c *FakeRESTClient) Do(req *http.Request) (*http.Response, error) { func (c *FakeRESTClient) Do(req *http.Request) (*http.Response, error) {

View File

@ -46,11 +46,6 @@ type Config struct {
// a RESTClient directly. When initializing a Client, will be set with the default // a RESTClient directly. When initializing a Client, will be set with the default
// code version. // code version.
Version string Version string
// LegacyBehavior defines whether the RESTClient should follow conventions that
// existed prior to v1beta3 in Kubernetes - namely, namespace (if specified)
// not being part of the path, and resource names allowing mixed case. Set to
// true when using Kubernetes v1beta1 or v1beta2.
LegacyBehavior bool
// Codec specifies the encoding and decoding behavior for runtime.Objects passed // Codec specifies the encoding and decoding behavior for runtime.Objects passed
// to a RESTClient or Client. Required when initializing a RESTClient, optional // to a RESTClient or Client. Required when initializing a RESTClient, optional
// when initializing a Client. // when initializing a Client.
@ -218,7 +213,6 @@ func SetKubernetesDefaults(config *Config) error {
if config.Codec == nil { if config.Codec == nil {
config.Codec = versionInterfaces.Codec config.Codec = versionInterfaces.Codec
} }
config.LegacyBehavior = (version == "v1beta1" || version == "v1beta2")
if config.QPS == 0.0 { if config.QPS == 0.0 {
config.QPS = 5.0 config.QPS = 5.0
} }
@ -245,7 +239,7 @@ func RESTClientFor(config *Config) (*RESTClient, error) {
return nil, err return nil, err
} }
client := NewRESTClient(baseURL, config.Version, config.Codec, config.LegacyBehavior, config.QPS, config.Burst) client := NewRESTClient(baseURL, config.Version, config.Codec, config.QPS, config.Burst)
transport, err := TransportFor(config) transport, err := TransportFor(config)
if err != nil { if err != nil {

View File

@ -272,7 +272,6 @@ func TestSetKubernetesDefaults(t *testing.T) {
Prefix: "/api", Prefix: "/api",
Version: latest.Version, Version: latest.Version,
Codec: latest.Codec, Codec: latest.Codec,
LegacyBehavior: (latest.Version == "v1beta1" || latest.Version == "v1beta2"),
QPS: 5, QPS: 5,
Burst: 10, Burst: 10,
}, },

View File

@ -72,13 +72,6 @@ type Request struct {
baseURL *url.URL baseURL *url.URL
codec runtime.Codec codec runtime.Codec
// If true, add "?namespace=<namespace>" as a query parameter, if false put namespaces/<namespace> in path
// Query parameter is considered legacy behavior
namespaceInQuery bool
// If true, lowercase resource prior to inserting into a path, if false, leave it as is. Preserving
// case is considered legacy behavior.
preserveResourceCase bool
// generic components accessible via method setters // generic components accessible via method setters
path string path string
subpath string subpath string
@ -107,7 +100,7 @@ type Request struct {
// NewRequest creates a new request helper object for accessing runtime.Objects on a server. // NewRequest creates a new request helper object for accessing runtime.Objects on a server.
func NewRequest(client HTTPClient, verb string, baseURL *url.URL, apiVersion string, func NewRequest(client HTTPClient, verb string, baseURL *url.URL, apiVersion string,
codec runtime.Codec, namespaceInQuery bool, preserveResourceCase bool) *Request { codec runtime.Codec) *Request {
metrics.Register() metrics.Register()
return &Request{ return &Request{
client: client, client: client,
@ -115,10 +108,7 @@ func NewRequest(client HTTPClient, verb string, baseURL *url.URL, apiVersion str
baseURL: baseURL, baseURL: baseURL,
path: baseURL.Path, path: baseURL.Path,
apiVersion: apiVersion, apiVersion: apiVersion,
codec: codec, codec: codec,
namespaceInQuery: namespaceInQuery,
preserveResourceCase: preserveResourceCase,
} }
} }
@ -490,15 +480,11 @@ func (r *Request) Body(obj interface{}) *Request {
// URL returns the current working URL. // URL returns the current working URL.
func (r *Request) URL() *url.URL { func (r *Request) URL() *url.URL {
p := r.path p := r.path
if r.namespaceSet && !r.namespaceInQuery && len(r.namespace) > 0 { if r.namespaceSet && len(r.namespace) > 0 {
p = path.Join(p, "namespaces", r.namespace) p = path.Join(p, "namespaces", r.namespace)
} }
if len(r.resource) != 0 { if len(r.resource) != 0 {
resource := r.resource p = path.Join(p, strings.ToLower(r.resource))
if !r.preserveResourceCase {
resource = strings.ToLower(resource)
}
p = path.Join(p, resource)
} }
// Join trims trailing slashes, so preserve r.path's trailing slash for backwards compat if nothing was changed // Join trims trailing slashes, so preserve r.path's trailing slash for backwards compat if nothing was changed
if len(r.resourceName) != 0 || len(r.subpath) != 0 || len(r.subresource) != 0 { if len(r.resourceName) != 0 || len(r.subpath) != 0 || len(r.subresource) != 0 {
@ -518,10 +504,6 @@ func (r *Request) URL() *url.URL {
} }
} }
if r.namespaceSet && r.namespaceInQuery {
query.Set("namespace", r.namespace)
}
// timeout is handled specially here. // timeout is handled specially here.
if r.timeout != 0 { if r.timeout != 0 {
query.Set("timeout", r.timeout.String()) query.Set("timeout", r.timeout.String())

View File

@ -70,14 +70,14 @@ func TestRequestWithErrorWontChange(t *testing.T) {
} }
func TestRequestPreservesBaseTrailingSlash(t *testing.T) { func TestRequestPreservesBaseTrailingSlash(t *testing.T) {
r := &Request{baseURL: &url.URL{}, path: "/path/", namespaceInQuery: true} r := &Request{baseURL: &url.URL{}, path: "/path/"}
if s := r.URL().String(); s != "/path/" { if s := r.URL().String(); s != "/path/" {
t.Errorf("trailing slash should be preserved: %s", s) t.Errorf("trailing slash should be preserved: %s", s)
} }
} }
func TestRequestAbsPathPreservesTrailingSlash(t *testing.T) { func TestRequestAbsPathPreservesTrailingSlash(t *testing.T) {
r := (&Request{baseURL: &url.URL{}, namespaceInQuery: true}).AbsPath("/foo/") r := (&Request{baseURL: &url.URL{}}).AbsPath("/foo/")
if s := r.URL().String(); s != "/foo/" { if s := r.URL().String(); s != "/foo/" {
t.Errorf("trailing slash should be preserved: %s", s) t.Errorf("trailing slash should be preserved: %s", s)
} }
@ -89,7 +89,7 @@ func TestRequestAbsPathPreservesTrailingSlash(t *testing.T) {
} }
func TestRequestAbsPathJoins(t *testing.T) { func TestRequestAbsPathJoins(t *testing.T) {
r := (&Request{baseURL: &url.URL{}, namespaceInQuery: true}).AbsPath("foo/bar", "baz") r := (&Request{baseURL: &url.URL{}}).AbsPath("foo/bar", "baz")
if s := r.URL().String(); s != "foo/bar/baz" { if s := r.URL().String(); s != "foo/bar/baz" {
t.Errorf("trailing slash should be preserved: %s", s) t.Errorf("trailing slash should be preserved: %s", s)
} }
@ -100,20 +100,11 @@ func TestRequestSetsNamespace(t *testing.T) {
baseURL: &url.URL{ baseURL: &url.URL{
Path: "/", Path: "/",
}, },
namespaceInQuery: true,
}).Namespace("foo") }).Namespace("foo")
if r.namespace == "" { if r.namespace == "" {
t.Errorf("namespace should be set: %#v", r) t.Errorf("namespace should be set: %#v", r)
} }
if s := r.URL().String(); s != "?namespace=foo" {
t.Errorf("namespace should be in params: %s", s)
}
r = (&Request{
baseURL: &url.URL{
Path: "/",
},
}).Namespace("foo")
if s := r.URL().String(); s != "namespaces/foo" { if s := r.URL().String(); s != "namespaces/foo" {
t.Errorf("namespace should be in path: %s", s) t.Errorf("namespace should be in path: %s", s)
} }
@ -217,7 +208,7 @@ func TestResultIntoWithErrReturnsErr(t *testing.T) {
func TestURLTemplate(t *testing.T) { func TestURLTemplate(t *testing.T) {
uri, _ := url.Parse("http://localhost") uri, _ := url.Parse("http://localhost")
r := NewRequest(nil, "POST", uri, "test", nil, false, false) r := NewRequest(nil, "POST", uri, "test", nil)
r.Prefix("pre1").Resource("r1").Namespace("ns").Name("nm").Param("p0", "v0") r.Prefix("pre1").Resource("r1").Namespace("ns").Name("nm").Param("p0", "v0")
full := r.URL() full := r.URL()
if full.String() != "http://localhost/pre1/namespaces/ns/r1/nm?p0=v0" { if full.String() != "http://localhost/pre1/namespaces/ns/r1/nm?p0=v0" {
@ -278,7 +269,7 @@ func TestTransformResponse(t *testing.T) {
{Response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(invalid))}, Data: invalid}, {Response: &http.Response{StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader(invalid))}, Data: invalid},
} }
for i, test := range testCases { for i, test := range testCases {
r := NewRequest(nil, "", uri, testapi.Version(), testapi.Codec(), true, true) r := NewRequest(nil, "", uri, testapi.Version(), testapi.Codec())
if test.Response.Body == nil { if test.Response.Body == nil {
test.Response.Body = ioutil.NopCloser(bytes.NewReader([]byte{})) test.Response.Body = ioutil.NopCloser(bytes.NewReader([]byte{}))
} }
@ -638,7 +629,7 @@ func TestRequestUpgrade(t *testing.T) {
Err: true, Err: true,
}, },
{ {
Request: NewRequest(nil, "", uri, testapi.Version(), testapi.Codec(), true, true), Request: NewRequest(nil, "", uri, testapi.Version(), testapi.Codec()),
Config: &Config{ Config: &Config{
Username: "u", Username: "u",
Password: "p", Password: "p",
@ -647,7 +638,7 @@ func TestRequestUpgrade(t *testing.T) {
Err: false, Err: false,
}, },
{ {
Request: NewRequest(nil, "", uri, testapi.Version(), testapi.Codec(), true, true), Request: NewRequest(nil, "", uri, testapi.Version(), testapi.Codec()),
Config: &Config{ Config: &Config{
BearerToken: "b", BearerToken: "b",
}, },

View File

@ -38,11 +38,6 @@ type RESTClient struct {
// A string identifying the version of the API this client is expected to use. // A string identifying the version of the API this client is expected to use.
apiVersion string apiVersion string
// LegacyBehavior controls if URLs should encode the namespace as a query param,
// and if resource case is preserved for supporting older API conventions of
// Kubernetes. Newer clients should leave this false.
LegacyBehavior bool
// Codec is the encoding and decoding scheme that applies to a particular set of // Codec is the encoding and decoding scheme that applies to a particular set of
// REST resources. // REST resources.
Codec runtime.Codec Codec runtime.Codec
@ -59,9 +54,8 @@ type RESTClient struct {
// NewRESTClient creates a new RESTClient. This client performs generic REST functions // NewRESTClient creates a new RESTClient. This client performs generic REST functions
// such as Get, Put, Post, and Delete on specified paths. Codec controls encoding and // such as Get, Put, Post, and Delete on specified paths. Codec controls encoding and
// decoding of responses from the server. If this client should use the older, legacy // decoding of responses from the server.
// API conventions from Kubernetes API v1beta1 and v1beta2, set legacyBehavior true. func NewRESTClient(baseURL *url.URL, apiVersion string, c runtime.Codec, maxQPS float32, maxBurst int) *RESTClient {
func NewRESTClient(baseURL *url.URL, apiVersion string, c runtime.Codec, legacyBehavior bool, maxQPS float32, maxBurst int) *RESTClient {
base := *baseURL base := *baseURL
if !strings.HasSuffix(base.Path, "/") { if !strings.HasSuffix(base.Path, "/") {
base.Path += "/" base.Path += "/"
@ -76,11 +70,7 @@ func NewRESTClient(baseURL *url.URL, apiVersion string, c runtime.Codec, legacyB
return &RESTClient{ return &RESTClient{
baseURL: &base, baseURL: &base,
apiVersion: apiVersion, apiVersion: apiVersion,
Codec: c, Codec: c,
LegacyBehavior: legacyBehavior,
Throttle: throttle, Throttle: throttle,
} }
} }
@ -101,7 +91,7 @@ func (c *RESTClient) Verb(verb string) *Request {
if c.Throttle != nil { if c.Throttle != nil {
c.Throttle.Accept() c.Throttle.Accept()
} }
return NewRequest(c.Client, verb, c.baseURL, c.apiVersion, c.Codec, c.LegacyBehavior, c.LegacyBehavior).Timeout(c.Timeout) return NewRequest(c.Client, verb, c.baseURL, c.apiVersion, c.Codec).Timeout(c.Timeout)
} }
// Post begins a POST request. Short for c.Verb("POST"). // Post begins a POST request. Short for c.Verb("POST").

View File

@ -122,8 +122,6 @@ func TestDoRequestBearer(t *testing.T) {
Host: testServer.URL, Host: testServer.URL,
Version: testapi.Version(), Version: testapi.Version(),
Codec: testapi.Codec(), Codec: testapi.Codec(),
LegacyBehavior: true,
BearerToken: "test", BearerToken: "test",
}) })
if err != nil { if err != nil {
@ -152,8 +150,6 @@ func TestDoRequestWithoutPassword(t *testing.T) {
Host: testServer.URL, Host: testServer.URL,
Version: testapi.Version(), Version: testapi.Version(),
Codec: testapi.Codec(), Codec: testapi.Codec(),
LegacyBehavior: true,
Username: "test", Username: "test",
}) })
if err != nil { if err != nil {
@ -193,8 +189,6 @@ func TestDoRequestSuccess(t *testing.T) {
Host: testServer.URL, Host: testServer.URL,
Version: testapi.Version(), Version: testapi.Version(),
Codec: testapi.Codec(), Codec: testapi.Codec(),
LegacyBehavior: true,
Username: "user", Username: "user",
Password: "pass", Password: "pass",
}) })
@ -238,7 +232,6 @@ func TestDoRequestFailed(t *testing.T) {
Host: testServer.URL, Host: testServer.URL,
Version: testapi.Version(), Version: testapi.Version(),
Codec: testapi.Codec(), Codec: testapi.Codec(),
LegacyBehavior: true,
}) })
if err != nil { if err != nil {
t.Fatalf("unexpected error: %v", err) t.Fatalf("unexpected error: %v", err)
@ -271,8 +264,6 @@ func TestDoRequestCreated(t *testing.T) {
Host: testServer.URL, Host: testServer.URL,
Version: testapi.Version(), Version: testapi.Version(),
Codec: testapi.Codec(), Codec: testapi.Codec(),
LegacyBehavior: true,
Username: "user", Username: "user",
Password: "pass", Password: "pass",
}) })

View File

@ -49,8 +49,7 @@ func getFakeClient(t *testing.T, validURLs []string) (ClientPosterFunc, *httptes
return func(mapping *meta.RESTMapping) (RESTClientPoster, error) { return func(mapping *meta.RESTMapping) (RESTClientPoster, error) {
fakeCodec := testapi.Codec() fakeCodec := testapi.Codec()
fakeUri, _ := url.Parse(server.URL + "/api/" + testapi.Version()) fakeUri, _ := url.Parse(server.URL + "/api/" + testapi.Version())
legacyBehavior := api.PreV1Beta3(testapi.Version()) return client.NewRESTClient(fakeUri, testapi.Version(), fakeCodec, 5, 10), nil
return client.NewRESTClient(fakeUri, testapi.Version(), fakeCodec, legacyBehavior, 5, 10), nil
}, server }, server
} }