Merge pull request #18476 from caesarxuchao/fix-query-convert

Fix queryparams convert
This commit is contained in:
Filip Grzadkowski 2015-12-14 13:22:27 +01:00
commit 4ef062c22f
2 changed files with 41 additions and 0 deletions

View File

@ -22,6 +22,7 @@ import (
) )
import ( import (
"net/http"
"net/url" "net/url"
"testing" "testing"
@ -29,6 +30,7 @@ import (
"k8s.io/kubernetes/pkg/api/testapi" "k8s.io/kubernetes/pkg/api/testapi"
"k8s.io/kubernetes/pkg/api/unversioned" "k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/apis/extensions" "k8s.io/kubernetes/pkg/apis/extensions"
"k8s.io/kubernetes/pkg/labels"
) )
func getDeploymentsResoureName() string { func getDeploymentsResoureName() string {
@ -175,3 +177,35 @@ func TestDeploymentWatch(t *testing.T) {
_, err := c.Setup(t).Deployments(api.NamespaceAll).Watch(unversioned.ListOptions{}) _, err := c.Setup(t).Deployments(api.NamespaceAll).Watch(unversioned.ListOptions{})
c.Validate(t, nil, err) c.Validate(t, nil, err)
} }
func TestListDeploymentsLabels(t *testing.T) {
ns := api.NamespaceDefault
labelSelectorQueryParamName := unversioned.LabelSelectorQueryParam(testapi.Extensions.GroupVersion().String())
c := &simple.Client{
Request: simple.Request{
Method: "GET",
Path: testapi.Extensions.ResourcePath("deployments", ns, ""),
Query: simple.BuildQueryValues(url.Values{labelSelectorQueryParamName: []string{"foo=bar,name=baz"}})},
Response: simple.Response{
StatusCode: http.StatusOK,
Body: &extensions.DeploymentList{
Items: []extensions.Deployment{
{
ObjectMeta: api.ObjectMeta{
Labels: map[string]string{
"foo": "bar",
"name": "baz",
},
},
},
},
},
},
}
c.Setup(t)
c.QueryValidator[labelSelectorQueryParamName] = simple.ValidateLabels
selector := labels.Set{"foo": "bar", "name": "baz"}.AsSelector()
options := unversioned.ListOptions{LabelSelector: unversioned.LabelSelector{selector}}
receivedPodList, err := c.Deployments(ns).List(options)
c.Validate(t, receivedPodList, err)
}

View File

@ -22,6 +22,7 @@ import (
"reflect" "reflect"
"strings" "strings"
"k8s.io/kubernetes/pkg/api/unversioned"
"k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/runtime"
) )
@ -144,6 +145,12 @@ func convertStruct(result url.Values, st reflect.Type, sv reflect.Value) {
addListOfParams(result, tag, omitempty, field) addListOfParams(result, tag, omitempty, field)
} }
case isStructKind(kind) && !(zeroValue(field) && omitempty): case isStructKind(kind) && !(zeroValue(field) && omitempty):
if selector, ok := field.Interface().(unversioned.LabelSelector); ok {
addParam(result, tag, omitempty, reflect.ValueOf(selector.Selector.String()))
}
if selector, ok := field.Interface().(unversioned.FieldSelector); ok {
addParam(result, tag, omitempty, reflect.ValueOf(selector.Selector.String()))
}
convertStruct(result, ft, field) convertStruct(result, ft, field)
} }
} }