Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Adding converter functions to convert field label selectors to i... #4928

Merged
merged 1 commit into from
Feb 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 0 additions & 22 deletions pkg/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1291,26 +1291,4 @@ func init() {
// If one of the conversion functions is malformed, detect it immediately.
panic(err)
}

// Add field conversion funcs.
err = newer.Scheme.AddFieldLabelConversionFunc("v1beta1", "pods",
func(label, value string) (string, string, error) {
switch label {
case "name":
return "name", value, nil
case "DesiredState.Host":
return "Status.Host", value, nil
case "DesiredState.Status":
podStatus := PodStatus(value)
var internalValue newer.PodPhase
newer.Scheme.Convert(&podStatus, &internalValue)
return "Status.Phase", string(internalValue), nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
})
if err != nil {
// If one of the conversion functions is malformed, detect it immediately.
panic(err)
}
}
22 changes: 0 additions & 22 deletions pkg/api/v1beta2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1206,26 +1206,4 @@ func init() {
// If one of the conversion functions is malformed, detect it immediately.
panic(err)
}

// Add field conversion funcs.
err = newer.Scheme.AddFieldLabelConversionFunc("v1beta2", "pods",
func(label, value string) (string, string, error) {
switch label {
case "name":
return "name", value, nil
case "DesiredState.Host":
return "Status.Host", value, nil
case "DesiredState.Status":
podStatus := PodStatus(value)
var internalValue newer.PodPhase
newer.Scheme.Convert(&podStatus, &internalValue)
return "Status.Phase", string(internalValue), nil
default:
return "", "", fmt.Errorf("field label not supported: %s", label)
}
})
if err != nil {
// If one of the conversion functions is malformed, detect it immediately.
panic(err)
}
}
44 changes: 0 additions & 44 deletions pkg/api/v1beta3/conversion.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/apiserver/api_installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
24 changes: 3 additions & 21 deletions pkg/apiserver/resthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package apiserver

import (
"net/http"
"net/url"
gpath "path"
"time"

Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
25 changes: 22 additions & 3 deletions pkg/apiserver/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package apiserver

import (
"net/http"
"net/url"
"path"
"regexp"
"strings"
Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 6 additions & 11 deletions pkg/apiserver/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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: "",
Expand All @@ -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
Expand Down
25 changes: 8 additions & 17 deletions pkg/client/cache/listwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@ func buildResourcePath(prefix, namespace, resource string) string {
return path.Join(base, resource)
}

func getHostFieldLabel() string {
switch testapi.Version() {
case "v1beta1", "v1beta2":
return "DesiredState.Host"
default:
return "Status.Host"
}
}

// buildQueryValues is a convenience function for knowing if a namespace should be in a query param or not
func buildQueryValues(namespace string, query url.Values) url.Values {
v := url.Values{}
Expand Down Expand Up @@ -93,17 +84,17 @@ func TestListWatchesCanList(t *testing.T) {
},
// pod with "assigned" field selector.
{
location: buildLocation(buildResourcePath("", api.NamespaceAll, "pods"), buildQueryValues(api.NamespaceAll, url.Values{"fields": []string{getHostFieldLabel() + "="}})),
location: buildLocation(buildResourcePath("", api.NamespaceAll, "pods"), buildQueryValues(api.NamespaceAll, url.Values{"fields": []string{"DesiredState.Host="}})),
resource: "pods",
namespace: api.NamespaceAll,
fieldSelector: labels.Set{getHostFieldLabel(): ""}.AsSelector(),
fieldSelector: labels.Set{"DesiredState.Host": ""}.AsSelector(),
},
// pod in namespace "foo"
{
location: buildLocation(buildResourcePath("", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{getHostFieldLabel() + "="}})),
location: buildLocation(buildResourcePath("", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{"DesiredState.Host="}})),
resource: "pods",
namespace: "foo",
fieldSelector: labels.Set{getHostFieldLabel(): ""}.AsSelector(),
fieldSelector: labels.Set{"DesiredState.Host": ""}.AsSelector(),
},
}
for _, item := range table {
Expand Down Expand Up @@ -147,19 +138,19 @@ func TestListWatchesCanWatch(t *testing.T) {
},
// pod with "assigned" field selector.
{
location: buildLocation(buildResourcePath("watch", api.NamespaceAll, "pods"), buildQueryValues(api.NamespaceAll, url.Values{"fields": []string{getHostFieldLabel() + "="}, "resourceVersion": []string{"0"}})),
location: buildLocation(buildResourcePath("watch", api.NamespaceAll, "pods"), buildQueryValues(api.NamespaceAll, url.Values{"fields": []string{"DesiredState.Host="}, "resourceVersion": []string{"0"}})),
rv: "0",
resource: "pods",
namespace: api.NamespaceAll,
fieldSelector: labels.Set{getHostFieldLabel(): ""}.AsSelector(),
fieldSelector: labels.Set{"DesiredState.Host": ""}.AsSelector(),
},
// pod with namespace foo and assigned field selector
{
location: buildLocation(buildResourcePath("watch", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{getHostFieldLabel() + "="}, "resourceVersion": []string{"0"}})),
location: buildLocation(buildResourcePath("watch", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{"DesiredState.Host="}, "resourceVersion": []string{"0"}})),
rv: "0",
resource: "pods",
namespace: "foo",
fieldSelector: labels.Set{getHostFieldLabel(): ""}.AsSelector(),
fieldSelector: labels.Set{"DesiredState.Host": ""}.AsSelector(),
},
}

Expand Down
11 changes: 1 addition & 10 deletions pkg/kubelet/config/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

// NewSourceApiserver creates a config source that watches and pulls from the apiserver.
func NewSourceApiserver(client *client.Client, hostname string, updates chan<- interface{}) {
lw := cache.NewListWatchFromClient(client, "pods", api.NamespaceAll, labels.OneTermEqualSelector(getHostFieldLabel(client.APIVersion()), hostname))
lw := cache.NewListWatchFromClient(client, "pods", api.NamespaceAll, labels.OneTermEqualSelector("Status.Host", hostname))
newSourceApiserverFromLW(lw, updates)
}

Expand All @@ -51,12 +51,3 @@ func newSourceApiserverFromLW(lw cache.ListerWatcher, updates chan<- interface{}
}
cache.NewReflector(lw, &api.Pod{}, cache.NewUndeltaStore(send, cache.MetaNamespaceKeyFunc)).Run()
}

func getHostFieldLabel(apiVersion string) string {
switch apiVersion {
case "v1beta1", "v1beta2":
return "DesiredState.Host"
default:
return "Status.Host"
}
}
27 changes: 0 additions & 27 deletions pkg/labels/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,21 +800,6 @@ func SelectorFromSetParse(ls Set) (Selector, error) {
// ParseSelector takes a string representing a selector and returns an
// object suitable for matching, or an error.
func ParseSelector(selector string) (Selector, error) {
return parseSelector(selector,
func(lhs, rhs string) (newLhs, newRhs string, err error) {
return lhs, rhs, nil
})
}

// Parses the selector and runs them through the given TransformFunc.
func ParseAndTransformSelector(selector string, fn TransformFunc) (Selector, error) {
return parseSelector(selector, fn)
}

// Function to transform selectors.
type TransformFunc func(label, value string) (newLabel, newValue string, err error)

func parseSelector(selector string, fn TransformFunc) (Selector, error) {
parts := strings.Split(selector, ",")
sort.StringSlice(parts).Sort()
var items []Selector
Expand All @@ -823,22 +808,10 @@ func parseSelector(selector string, fn TransformFunc) (Selector, error) {
continue
}
if lhs, rhs, ok := try(part, "!="); ok {
lhs, rhs, err := fn(lhs, rhs)
if err != nil {
return nil, err
}
items = append(items, &notHasTerm{label: lhs, value: rhs})
} else if lhs, rhs, ok := try(part, "=="); ok {
lhs, rhs, err := fn(lhs, rhs)
if err != nil {
return nil, err
}
items = append(items, &hasTerm{label: lhs, value: rhs})
} else if lhs, rhs, ok := try(part, "="); ok {
lhs, rhs, err := fn(lhs, rhs)
if err != nil {
return nil, err
}
items = append(items, &hasTerm{label: lhs, value: rhs})
} else {
return nil, fmt.Errorf("invalid selector: '%s'; can't understand '%s'", selector, part)
Expand Down