Revert "Adding converter functions to convert field label selectors to internal version before matching"

This commit is contained in:
Zach Loafman
2015-02-28 11:42:49 -08:00
parent 0fec31a11e
commit 5f35a67002
13 changed files with 56 additions and 224 deletions

View File

@@ -242,7 +242,7 @@ func (a *APIInstaller) registerResourceHandlers(path string, storage RESTStorage
addParams(route, action.Params)
ws.Route(route)
case "LIST": // List all resources of a kind.
route := ws.GET(action.Path).To(ListResource(lister, ctxFn, action.Namer, codec, a.group.info)).
route := ws.GET(action.Path).To(ListResource(lister, ctxFn, action.Namer, codec)).
Filter(m).
Doc("list objects of kind " + kind).
Operation("list" + kind).

View File

@@ -18,7 +18,6 @@ package apiserver
import (
"net/http"
"net/url"
gpath "path"
"time"
@@ -80,24 +79,8 @@ func GetResource(r RESTGetter, ctxFn ContextFunc, namer ScopeNamer, codec runtim
}
}
func parseSelectorQueryParams(query url.Values, version, apiResource string) (label, field labels.Selector, err error) {
label, err = labels.ParseSelector(query.Get("labels"))
if err != nil {
return nil, nil, err
}
convertToInternalVersionFunc := func(label, value string) (newLabel, newValue string, err error) {
return api.Scheme.ConvertFieldLabel(version, apiResource, label, value)
}
field, err = labels.ParseAndTransformSelector(query.Get("fields"), convertToInternalVersionFunc)
if err != nil {
return nil, nil, err
}
return label, field, nil
}
// ListResource returns a function that handles retrieving a list of resources from a RESTStorage object.
func ListResource(r RESTLister, ctxFn ContextFunc, namer ScopeNamer, codec runtime.Codec, requestInfoResolver *APIRequestInfoResolver) restful.RouteFunction {
func ListResource(r RESTLister, ctxFn ContextFunc, namer ScopeNamer, codec runtime.Codec) restful.RouteFunction {
return func(req *restful.Request, res *restful.Response) {
w := res.ResponseWriter
@@ -109,13 +92,12 @@ func ListResource(r RESTLister, ctxFn ContextFunc, namer ScopeNamer, codec runti
ctx := ctxFn(req)
ctx = api.WithNamespace(ctx, namespace)
requestInfo, err := requestInfoResolver.GetAPIRequestInfo(req.Request)
label, err := labels.ParseSelector(req.Request.URL.Query().Get("labels"))
if err != nil {
errorJSON(err, codec, w)
return
}
label, field, err := parseSelectorQueryParams(req.Request.URL.Query(), requestInfo.APIVersion, requestInfo.Resource)
field, err := labels.ParseSelector(req.Request.URL.Query().Get("fields"))
if err != nil {
errorJSON(err, codec, w)
return

View File

@@ -18,6 +18,7 @@ package apiserver
import (
"net/http"
"net/url"
"path"
"regexp"
"strings"
@@ -26,6 +27,7 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/httplog"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
watchjson "github.com/GoogleCloudPlatform/kubernetes/pkg/watch/json"
@@ -54,6 +56,25 @@ func (h *WatchHandler) setSelfLinkAddName(obj runtime.Object, req *http.Request)
return h.linker.SetSelfLink(obj, newURL.String())
}
func getWatchParams(query url.Values) (label, field labels.Selector, resourceVersion string, err error) {
s, perr := labels.ParseSelector(query.Get("labels"))
if perr != nil {
err = perr
return
}
label = s
s, perr = labels.ParseSelector(query.Get("fields"))
if perr != nil {
err = perr
return
}
field = s
resourceVersion = query.Get("resourceVersion")
return
}
var connectionUpgradeRegex = regexp.MustCompile("(^|.*,\\s*)upgrade($|\\s*,)")
func isWebsocketRequest(req *http.Request) bool {
@@ -96,13 +117,11 @@ func (h *WatchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}
label, field, err := parseSelectorQueryParams(req.URL.Query(), requestInfo.APIVersion, apiResource)
label, field, resourceVersion, err := getWatchParams(req.URL.Query())
if err != nil {
httpCode = errorJSON(err, h.codec, w)
return
}
resourceVersion := req.URL.Query().Get("resourceVersion")
watching, err := watcher.Watch(ctx, label, field, resourceVersion)
if err != nil {
httpCode = errorJSON(err, h.codec, w)

View File

@@ -25,7 +25,6 @@ import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
"golang.org/x/net/websocket"
@@ -165,19 +164,15 @@ func TestWatchHTTP(t *testing.T) {
}
func TestWatchParamParsing(t *testing.T) {
api.Scheme.AddFieldLabelConversionFunc(testVersion, "foo",
func(label, value string) (string, string, error) {
return label, value, nil
})
simpleStorage := &SimpleRESTStorage{}
handler := Handle(map[string]RESTStorage{
"foo": simpleStorage,
}, codec, "/api", testVersion, selfLinker, admissionControl, requestContextMapper, mapper)
}, codec, "/api", "version", selfLinker, admissionControl, requestContextMapper, mapper)
server := httptest.NewServer(handler)
defer server.Close()
dest, _ := url.Parse(server.URL)
dest.Path = "/api/" + testVersion + "/watch/foo"
dest.Path = "/api/version/watch/foo"
table := []struct {
rawQuery string
@@ -199,10 +194,10 @@ func TestWatchParamParsing(t *testing.T) {
fieldSelector: "Host=",
namespace: api.NamespaceDefault,
}, {
rawQuery: "namespace=watchother&fields=id%3dfoo&resourceVersion=1492",
rawQuery: "namespace=watchother&fields=ID%3dfoo&resourceVersion=1492",
resourceVersion: "1492",
labelSelector: "",
fieldSelector: "id=foo",
fieldSelector: "ID=foo",
namespace: "watchother",
}, {
rawQuery: "",
@@ -214,8 +209,8 @@ func TestWatchParamParsing(t *testing.T) {
}
for _, item := range table {
simpleStorage.requestedLabelSelector = labels.Everything()
simpleStorage.requestedFieldSelector = labels.Everything()
simpleStorage.requestedLabelSelector = nil
simpleStorage.requestedFieldSelector = nil
simpleStorage.requestedResourceVersion = "5" // Prove this is set in all cases
simpleStorage.requestedResourceNamespace = ""
dest.RawQuery = item.rawQuery