Merge pull request #6494 from lavalamp/fix2

compute query parameter names in the client.
This commit is contained in:
Rohit Jnagal 2015-04-06 18:44:45 -07:00
commit 0400339d01
19 changed files with 91 additions and 70 deletions

View File

@ -17,7 +17,6 @@ limitations under the License.
package cache
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
@ -41,10 +40,20 @@ type ListWatch struct {
// NewListWatchFromClient creates a new ListWatch from the specified client, resource, namespace and field selector.
func NewListWatchFromClient(c *client.Client, resource string, namespace string, fieldSelector fields.Selector) *ListWatch {
listFunc := func() (runtime.Object, error) {
return c.Get().Namespace(namespace).Resource(resource).FieldsSelectorParam(api.FieldSelectorQueryParam(c.APIVersion()), fieldSelector).Do().Get()
return c.Get().
Namespace(namespace).
Resource(resource).
FieldsSelectorParam(fieldSelector).
Do().
Get()
}
watchFunc := func(resourceVersion string) (watch.Interface, error) {
return c.Get().Prefix("watch").Namespace(namespace).Resource(resource).FieldsSelectorParam(api.FieldSelectorQueryParam(c.APIVersion()), fieldSelector).Param("resourceVersion", resourceVersion).Watch()
return c.Get().
Prefix("watch").
Namespace(namespace).
Resource(resource).
FieldsSelectorParam(fieldSelector).
Param("resourceVersion", resourceVersion).Watch()
}
return &ListWatch{ListFunc: listFunc, WatchFunc: watchFunc}
}

View File

@ -63,7 +63,7 @@ func (c *endpoints) List(selector labels.Selector) (result *api.EndpointsList, e
err = c.r.Get().
Namespace(c.ns).
Resource("endpoints").
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), selector).
LabelsSelectorParam(selector).
Do().
Into(result)
return
@ -83,8 +83,8 @@ func (c *endpoints) Watch(label labels.Selector, field fields.Selector, resource
Namespace(c.ns).
Resource("endpoints").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.r.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -104,8 +104,8 @@ func (e *events) List(label labels.Selector, field fields.Selector) (*api.EventL
err := e.client.Get().
NamespaceIfScoped(e.namespace, len(e.namespace) > 0).
Resource("events").
LabelsSelectorParam(api.LabelSelectorQueryParam(e.client.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(e.client.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Do().
Into(result)
return result, err
@ -130,8 +130,8 @@ func (e *events) Watch(label labels.Selector, field fields.Selector, resourceVer
NamespaceIfScoped(e.namespace, len(e.namespace) > 0).
Resource("events").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(e.client.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(e.client.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -57,7 +57,7 @@ func newLimitRanges(c *Client, namespace string) *limitRanges {
// List takes a selector, and returns the list of limitRanges that match that selector.
func (c *limitRanges) List(selector labels.Selector) (result *api.LimitRangeList, err error) {
result = &api.LimitRangeList{}
err = c.r.Get().Namespace(c.ns).Resource("limitRanges").LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), selector).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("limitRanges").LabelsSelectorParam(selector).Do().Into(result)
return
}
@ -98,7 +98,7 @@ func (c *limitRanges) Watch(label labels.Selector, field fields.Selector, resour
Namespace(c.ns).
Resource("limitRanges").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.r.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -62,8 +62,8 @@ func (c *namespaces) List(label labels.Selector, field fields.Selector) (*api.Na
result := &api.NamespaceList{}
err := c.r.Get().
Resource("namespaces").
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.r.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Do().Into(result)
return result, err
}
@ -119,7 +119,7 @@ func (c *namespaces) Watch(label labels.Selector, field fields.Selector, resourc
Prefix("watch").
Resource("namespaces").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.r.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -101,7 +101,7 @@ func (c *nodes) Watch(label labels.Selector, field fields.Selector, resourceVers
Namespace(api.NamespaceAll).
Resource(c.resourceName()).
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.r.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -57,8 +57,8 @@ func (c *persistentVolumeClaims) List(label labels.Selector, field fields.Select
err = c.client.Get().
Namespace(c.namespace).
Resource("persistentVolumeClaims").
LabelsSelectorParam(api.LabelSelectorQueryParam(c.client.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.client.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Do().
Into(result)
@ -97,7 +97,7 @@ func (c *persistentVolumeClaims) Watch(label labels.Selector, field fields.Selec
Namespace(c.namespace).
Resource("persistentVolumeClaims").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.client.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.client.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -52,8 +52,8 @@ func (c *persistentVolumes) List(label labels.Selector, field fields.Selector) (
result = &api.PersistentVolumeList{}
err = c.client.Get().
Resource("persistentVolumes").
LabelsSelectorParam(api.LabelSelectorQueryParam(c.client.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.client.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Do().
Into(result)
@ -91,7 +91,7 @@ func (c *persistentVolumes) Watch(label labels.Selector, field fields.Selector,
Prefix("watch").
Resource("persistentVolumes").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.client.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.client.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -57,7 +57,7 @@ func newPods(c *Client, namespace string) *pods {
// List takes a selector, and returns the list of pods that match that selector.
func (c *pods) List(selector labels.Selector) (result *api.PodList, err error) {
result = &api.PodList{}
err = c.r.Get().Namespace(c.ns).Resource("pods").LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), selector).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("pods").LabelsSelectorParam(selector).Do().Into(result)
return
}
@ -94,8 +94,8 @@ func (c *pods) Watch(label labels.Selector, field fields.Selector, resourceVersi
Namespace(c.ns).
Resource("pods").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label).
FieldsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -52,7 +52,7 @@ func newReplicationControllers(c *Client, namespace string) *replicationControll
// List takes a selector, and returns the list of replication controllers that match that selector.
func (c *replicationControllers) List(selector labels.Selector) (result *api.ReplicationControllerList, err error) {
result = &api.ReplicationControllerList{}
err = c.r.Get().Namespace(c.ns).Resource("replicationControllers").LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), selector).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("replicationControllers").LabelsSelectorParam(selector).Do().Into(result)
return
}
@ -89,7 +89,7 @@ func (c *replicationControllers) Watch(label labels.Selector, field fields.Selec
Namespace(c.ns).
Resource("replicationControllers").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.r.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -110,10 +110,11 @@ func NewRequest(client HTTPClient, verb string, baseURL *url.URL, apiVersion str
codec runtime.Codec, namespaceInQuery bool, preserveResourceCase bool) *Request {
metrics.Register()
return &Request{
client: client,
verb: verb,
baseURL: baseURL,
path: baseURL.Path,
client: client,
verb: verb,
baseURL: baseURL,
path: baseURL.Path,
apiVersion: apiVersion,
codec: codec,
namespaceInQuery: namespaceInQuery,
@ -247,6 +248,7 @@ func (r *Request) RequestURI(uri string) *Request {
return r
}
/*
// ParseSelectorParam parses the given string as a resource selector.
// This is a convenience function so you don't have to first check that it's a
// validly formatted selector.
@ -275,28 +277,28 @@ func (r *Request) ParseSelectorParam(paramName, item string) *Request {
return r
}
return r.setParam(paramName, selector)
}
}*/
// FieldsSelectorParam adds the given selector as a query parameter with the name paramName.
func (r *Request) FieldsSelectorParam(paramName string, s fields.Selector) *Request {
func (r *Request) FieldsSelectorParam(s fields.Selector) *Request {
if r.err != nil {
return r
}
if s.Empty() {
return r
}
return r.setParam(paramName, s.String())
return r.setParam(api.FieldSelectorQueryParam(r.apiVersion), s.String())
}
// LabelsSelectorParam adds the given selector as a query parameter
func (r *Request) LabelsSelectorParam(paramName string, s labels.Selector) *Request {
func (r *Request) LabelsSelectorParam(s labels.Selector) *Request {
if r.err != nil {
return r
}
if s.Empty() {
return r
}
return r.setParam(paramName, s.String())
return r.setParam(api.LabelSelectorQueryParam(r.apiVersion), s.String())
}
// UintParam creates a query parameter with the given value.

View File

@ -47,10 +47,13 @@ import (
)
func TestRequestWithErrorWontChange(t *testing.T) {
original := Request{err: errors.New("test")}
original := Request{
err: errors.New("test"),
apiVersion: testapi.Version(),
}
r := original
changed := r.Param("foo", "bar").
LabelsSelectorParam(api.LabelSelectorQueryParam(testapi.Version()), labels.Set{"a": "b"}.AsSelector()).
LabelsSelectorParam(labels.Set{"a": "b"}.AsSelector()).
UintParam("uint", 1).
AbsPath("/abs").
Prefix("test").
@ -749,7 +752,7 @@ func TestDoRequestNewWayReader(t *testing.T) {
Resource("bar").
Name("baz").
Prefix("foo").
LabelsSelectorParam(api.LabelSelectorQueryParam(c.APIVersion()), labels.Set{"name": "foo"}.AsSelector()).
LabelsSelectorParam(labels.Set{"name": "foo"}.AsSelector()).
Timeout(time.Second).
Body(bytes.NewBuffer(reqBodyExpected)).
Do().Get()
@ -789,7 +792,7 @@ func TestDoRequestNewWayObj(t *testing.T) {
Suffix("baz").
Name("bar").
Resource("foo").
LabelsSelectorParam(api.LabelSelectorQueryParam(c.APIVersion()), labels.Set{"name": "foo"}.AsSelector()).
LabelsSelectorParam(labels.Set{"name": "foo"}.AsSelector()).
Timeout(time.Second).
Body(reqObj).
Do().Get()

View File

@ -56,7 +56,7 @@ func newResourceQuotas(c *Client, namespace string) *resourceQuotas {
// List takes a selector, and returns the list of resourceQuotas that match that selector.
func (c *resourceQuotas) List(selector labels.Selector) (result *api.ResourceQuotaList, err error) {
result = &api.ResourceQuotaList{}
err = c.r.Get().Namespace(c.ns).Resource("resourceQuotas").LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), selector).Do().Into(result)
err = c.r.Get().Namespace(c.ns).Resource("resourceQuotas").LabelsSelectorParam(selector).Do().Into(result)
return
}
@ -100,7 +100,7 @@ func (c *resourceQuotas) Watch(label labels.Selector, field fields.Selector, res
Namespace(c.ns).
Resource("resourceQuotas").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.r.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -69,8 +69,8 @@ func (s *secrets) List(label labels.Selector, field fields.Selector) (*api.Secre
err := s.client.Get().
Namespace(s.namespace).
Resource("secrets").
LabelsSelectorParam(api.LabelSelectorQueryParam(s.client.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(s.client.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Do().
Into(result)
@ -97,8 +97,8 @@ func (s *secrets) Watch(label labels.Selector, field fields.Selector, resourceVe
Namespace(s.namespace).
Resource("secrets").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(s.client.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(s.client.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -61,7 +61,7 @@ func (c *services) List(selector labels.Selector) (result *api.ServiceList, err
err = c.r.Get().
Namespace(c.ns).
Resource("services").
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), selector).
LabelsSelectorParam(selector).
Do().
Into(result)
return
@ -100,7 +100,7 @@ func (c *services) Watch(label labels.Selector, field fields.Selector, resourceV
Namespace(c.ns).
Resource("services").
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(c.r.APIVersion()), label).
FieldsSelectorParam(api.FieldSelectorQueryParam(c.r.APIVersion()), field).
LabelsSelectorParam(label).
FieldsSelectorParam(field).
Watch()
}

View File

@ -22,6 +22,8 @@ import (
"strings"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
)
@ -187,12 +189,12 @@ func TestDeleteMultipleSelector(t *testing.T) {
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
switch p, m := req.URL.Path, req.Method; {
case p == "/namespaces/test/pods" && m == "GET":
if req.URL.Query().Get("labels") != "a=b" {
if req.URL.Query().Get(api.LabelSelectorQueryParam(testapi.Version())) != "a=b" {
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
}
return &http.Response{StatusCode: 200, Body: objBody(codec, pods)}, nil
case p == "/namespaces/test/services" && m == "GET":
if req.URL.Query().Get("labels") != "a=b" {
if req.URL.Query().Get(api.LabelSelectorQueryParam(testapi.Version())) != "a=b" {
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
}
return &http.Response{StatusCode: 200, Body: objBody(codec, svc)}, nil

View File

@ -28,6 +28,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
@ -280,7 +281,7 @@ func TestGetMultipleTypeObjectsWithSelector(t *testing.T) {
tf.Client = &client.FakeRESTClient{
Codec: codec,
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
if req.URL.Query().Get("labels") != "a=b" {
if req.URL.Query().Get(api.LabelSelectorQueryParam(testapi.Version())) != "a=b" {
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
}
switch req.URL.Path {
@ -412,7 +413,7 @@ func TestWatchSelector(t *testing.T) {
tf.Client = &client.FakeRESTClient{
Codec: codec,
Client: client.HTTPClientFunc(func(req *http.Request) (*http.Response, error) {
if req.URL.Query().Get("labels") != "a=b" {
if req.URL.Query().Get(api.LabelSelectorQueryParam(testapi.Version())) != "a=b" {
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
}
switch req.URL.Path {

View File

@ -31,6 +31,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util/errors"
@ -364,9 +365,10 @@ func TestResourceByNameAndEmptySelector(t *testing.T) {
func TestSelector(t *testing.T) {
pods, svc := testData()
labelKey := api.LabelSelectorQueryParam(testapi.Version())
b := NewBuilder(latest.RESTMapper, api.Scheme, fakeClientWith(t, map[string]string{
"/namespaces/test/pods?labels=a%3Db": runtime.EncodeOrDie(latest.Codec, pods),
"/namespaces/test/services?labels=a%3Db": runtime.EncodeOrDie(latest.Codec, svc),
"/namespaces/test/pods?" + labelKey + "=a%3Db": runtime.EncodeOrDie(latest.Codec, pods),
"/namespaces/test/services?" + labelKey + "=a%3Db": runtime.EncodeOrDie(latest.Codec, svc),
})).
SelectorParam("a=b").
NamespaceParam("test").
@ -576,8 +578,9 @@ func TestSingularObject(t *testing.T) {
func TestListObject(t *testing.T) {
pods, _ := testData()
labelKey := api.LabelSelectorQueryParam(testapi.Version())
b := NewBuilder(latest.RESTMapper, api.Scheme, fakeClientWith(t, map[string]string{
"/namespaces/test/pods?labels=a%3Db": runtime.EncodeOrDie(latest.Codec, pods),
"/namespaces/test/pods?" + labelKey + "=a%3Db": runtime.EncodeOrDie(latest.Codec, pods),
})).
SelectorParam("a=b").
NamespaceParam("test").
@ -608,9 +611,10 @@ func TestListObject(t *testing.T) {
func TestListObjectWithDifferentVersions(t *testing.T) {
pods, svc := testData()
labelKey := api.LabelSelectorQueryParam(testapi.Version())
obj, err := NewBuilder(latest.RESTMapper, api.Scheme, fakeClientWith(t, map[string]string{
"/namespaces/test/pods?labels=a%3Db": runtime.EncodeOrDie(latest.Codec, pods),
"/namespaces/test/services?labels=a%3Db": runtime.EncodeOrDie(latest.Codec, svc),
"/namespaces/test/pods?" + labelKey + "=a%3Db": runtime.EncodeOrDie(latest.Codec, pods),
"/namespaces/test/services?" + labelKey + "=a%3Db": runtime.EncodeOrDie(latest.Codec, svc),
})).
SelectorParam("a=b").
NamespaceParam("test").

View File

@ -17,7 +17,6 @@ limitations under the License.
package resource
import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
@ -30,7 +29,7 @@ import (
type Helper struct {
// The name of this resource as the server would recognize it
Resource string
// A RESTClient capable of mutating this resource
// A RESTClient capable of mutating this resource.
RESTClient RESTClient
// A codec for decoding and encoding objects of this resource type.
Codec runtime.Codec
@ -62,11 +61,12 @@ func (m *Helper) Get(namespace, name string) (runtime.Object, error) {
Get()
}
// TODO: add field selector
func (m *Helper) List(namespace, apiVersion string, selector labels.Selector) (runtime.Object, error) {
return m.RESTClient.Get().
NamespaceIfScoped(namespace, m.NamespaceScoped).
Resource(m.Resource).
LabelsSelectorParam(api.LabelSelectorQueryParam(apiVersion), selector).
LabelsSelectorParam(selector).
Do().
Get()
}
@ -77,8 +77,8 @@ func (m *Helper) Watch(namespace, resourceVersion, apiVersion string, labelSelec
NamespaceIfScoped(namespace, m.NamespaceScoped).
Resource(m.Resource).
Param("resourceVersion", resourceVersion).
LabelsSelectorParam(api.LabelSelectorQueryParam(apiVersion), labelSelector).
FieldsSelectorParam(api.FieldSelectorQueryParam(apiVersion), fieldSelector).
LabelsSelectorParam(labelSelector).
FieldsSelectorParam(fieldSelector).
Watch()
}