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

Adding converter functions to convert field label selectors to internal version before matching #4575

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: 22 additions & 0 deletions pkg/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1291,4 +1291,26 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add:

// TODO: This is evil. We should change bindings to write DesiredState.Host.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I convinced Brian that we don't need to sprinkle TODOs for this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)

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: 22 additions & 0 deletions pkg/api/v1beta2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1206,4 +1206,26 @@ 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",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check this error value :)

(also in v1beta1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. v1beta1 was checking it already :)

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: 44 additions & 0 deletions pkg/api/v1beta3/conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2014 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta3

import (
"fmt"

newer "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
)

func init() {
// Add field conversion funcs.
err := newer.Scheme.AddFieldLabelConversionFunc("v1beta3", "pods",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this converter necessary in v1beta3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. We need a conversion func from all external versions to internal versions.

func(label, value string) (string, string, error) {
switch label {
case "name":
fallthrough
case "Status.Phase":
fallthrough
case "Status.Host":
return label, value, 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)
}
}
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)).
route := ws.GET(action.Path).To(ListResource(lister, ctxFn, action.Namer, codec, a.group.info)).
Filter(m).
Doc("list objects of kind " + kind).
Operation("list" + kind).
Expand Down
24 changes: 21 additions & 3 deletions pkg/apiserver/resthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package apiserver

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

Expand Down Expand Up @@ -79,8 +80,24 @@ 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) restful.RouteFunction {
func ListResource(r RESTLister, ctxFn ContextFunc, namer ScopeNamer, codec runtime.Codec, requestInfoResolver *APIRequestInfoResolver) restful.RouteFunction {
return func(req *restful.Request, res *restful.Response) {
w := res.ResponseWriter

Expand All @@ -92,12 +109,13 @@ func ListResource(r RESTLister, ctxFn ContextFunc, namer ScopeNamer, codec runti
ctx := ctxFn(req)
ctx = api.WithNamespace(ctx, namespace)

label, err := labels.ParseSelector(req.Request.URL.Query().Get("labels"))
requestInfo, err := requestInfoResolver.GetAPIRequestInfo(req.Request)
if err != nil {
errorJSON(err, codec, w)
return
}
field, err := labels.ParseSelector(req.Request.URL.Query().Get("fields"))

label, field, err := parseSelectorQueryParams(req.Request.URL.Query(), requestInfo.APIVersion, requestInfo.Resource)
if err != nil {
errorJSON(err, codec, w)
return
Expand Down
25 changes: 3 additions & 22 deletions pkg/apiserver/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package apiserver

import (
"net/http"
"net/url"
"path"
"regexp"
"strings"
Expand All @@ -27,7 +26,6 @@ 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 @@ -56,25 +54,6 @@ 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 @@ -117,11 +96,13 @@ func (h *WatchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

label, field, resourceVersion, err := getWatchParams(req.URL.Query())
label, field, err := parseSelectorQueryParams(req.URL.Query(), requestInfo.APIVersion, apiResource)
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: 11 additions & 6 deletions pkg/apiserver/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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 @@ -164,15 +165,19 @@ 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", "version", selfLinker, admissionControl, requestContextMapper, mapper)
}, codec, "/api", testVersion, selfLinker, admissionControl, requestContextMapper, mapper)
server := httptest.NewServer(handler)
defer server.Close()

dest, _ := url.Parse(server.URL)
dest.Path = "/api/version/watch/foo"
dest.Path = "/api/" + testVersion + "/watch/foo"

table := []struct {
rawQuery string
Expand All @@ -194,10 +199,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 @@ -209,8 +214,8 @@ func TestWatchParamParsing(t *testing.T) {
}

for _, item := range table {
simpleStorage.requestedLabelSelector = nil
simpleStorage.requestedFieldSelector = nil
simpleStorage.requestedLabelSelector = labels.Everything()
simpleStorage.requestedFieldSelector = labels.Everything()
simpleStorage.requestedResourceVersion = "5" // Prove this is set in all cases
simpleStorage.requestedResourceNamespace = ""
dest.RawQuery = item.rawQuery
Expand Down
25 changes: 17 additions & 8 deletions pkg/client/cache/listwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ 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 @@ -84,17 +93,17 @@ func TestListWatchesCanList(t *testing.T) {
},
// pod with "assigned" field selector.
{
location: buildLocation(buildResourcePath("", api.NamespaceAll, "pods"), buildQueryValues(api.NamespaceAll, url.Values{"fields": []string{"DesiredState.Host="}})),
location: buildLocation(buildResourcePath("", api.NamespaceAll, "pods"), buildQueryValues(api.NamespaceAll, url.Values{"fields": []string{getHostFieldLabel() + "="}})),
resource: "pods",
namespace: api.NamespaceAll,
fieldSelector: labels.Set{"DesiredState.Host": ""}.AsSelector(),
fieldSelector: labels.Set{getHostFieldLabel(): ""}.AsSelector(),
},
// pod in namespace "foo"
{
location: buildLocation(buildResourcePath("", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{"DesiredState.Host="}})),
location: buildLocation(buildResourcePath("", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{getHostFieldLabel() + "="}})),
resource: "pods",
namespace: "foo",
fieldSelector: labels.Set{"DesiredState.Host": ""}.AsSelector(),
fieldSelector: labels.Set{getHostFieldLabel(): ""}.AsSelector(),
},
}
for _, item := range table {
Expand Down Expand Up @@ -138,19 +147,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{"DesiredState.Host="}, "resourceVersion": []string{"0"}})),
location: buildLocation(buildResourcePath("watch", api.NamespaceAll, "pods"), buildQueryValues(api.NamespaceAll, url.Values{"fields": []string{getHostFieldLabel() + "="}, "resourceVersion": []string{"0"}})),
rv: "0",
resource: "pods",
namespace: api.NamespaceAll,
fieldSelector: labels.Set{"DesiredState.Host": ""}.AsSelector(),
fieldSelector: labels.Set{getHostFieldLabel(): ""}.AsSelector(),
},
// pod with namespace foo and assigned field selector
{
location: buildLocation(buildResourcePath("watch", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{"DesiredState.Host="}, "resourceVersion": []string{"0"}})),
location: buildLocation(buildResourcePath("watch", "foo", "pods"), buildQueryValues("foo", url.Values{"fields": []string{getHostFieldLabel() + "="}, "resourceVersion": []string{"0"}})),
rv: "0",
resource: "pods",
namespace: "foo",
fieldSelector: labels.Set{"DesiredState.Host": ""}.AsSelector(),
fieldSelector: labels.Set{getHostFieldLabel(): ""}.AsSelector(),
},
}

Expand Down
11 changes: 10 additions & 1 deletion 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("Status.Host", hostname))
lw := cache.NewListWatchFromClient(client, "pods", api.NamespaceAll, labels.OneTermEqualSelector(getHostFieldLabel(client.APIVersion()), hostname))
newSourceApiserverFromLW(lw, updates)
}

Expand All @@ -51,3 +51,12 @@ 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: 27 additions & 0 deletions pkg/labels/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,21 @@ 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 @@ -808,10 +823,22 @@ func ParseSelector(selector string) (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