Fix watch by namespace

pull/6/head
derekwaynecarr 2014-10-30 12:55:17 -04:00
parent 78df01172a
commit dda19071e3
3 changed files with 25 additions and 9 deletions

View File

@ -77,10 +77,11 @@ type SimpleRESTStorage struct {
created *Simple
// These are set when Watch is called
fakeWatch *watch.FakeWatcher
requestedLabelSelector labels.Selector
requestedFieldSelector labels.Selector
requestedResourceVersion string
fakeWatch *watch.FakeWatcher
requestedLabelSelector labels.Selector
requestedFieldSelector labels.Selector
requestedResourceVersion string
requestedResourceNamespace string
// The id requested, and location to return for ResourceLocation
requestedResourceLocationID string
@ -151,6 +152,7 @@ func (storage *SimpleRESTStorage) Watch(ctx api.Context, label, field labels.Sel
storage.requestedLabelSelector = label
storage.requestedFieldSelector = field
storage.requestedResourceVersion = resourceVersion
storage.requestedResourceNamespace = api.Namespace(ctx)
if err := storage.errors["watch"]; err != nil {
return nil, err
}
@ -161,9 +163,9 @@ func (storage *SimpleRESTStorage) Watch(ctx api.Context, label, field labels.Sel
// Implement Redirector.
func (storage *SimpleRESTStorage) ResourceLocation(ctx api.Context, id string) (string, error) {
// validate that the namespace context on the request matches the expected input
requestedResourceNamespace := api.Namespace(ctx)
if storage.expectedResourceNamespace != requestedResourceNamespace {
return "", fmt.Errorf("Expected request namespace %s, but got namespace %s", storage.expectedResourceNamespace, requestedResourceNamespace)
storage.requestedResourceNamespace = api.Namespace(ctx)
if storage.expectedResourceNamespace != storage.requestedResourceNamespace {
return "", fmt.Errorf("Expected request namespace %s, but got namespace %s", storage.expectedResourceNamespace, storage.requestedResourceNamespace)
}
storage.requestedResourceLocationID = id
if err := storage.errors["resourceLocation"]; err != nil {

View File

@ -60,6 +60,10 @@ func isWebsocketRequest(req *http.Request) bool {
// ServeHTTP processes watch requests.
func (h *WatchHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
ctx := api.NewContext()
namespace := req.URL.Query().Get("namespace")
if len(namespace) > 0 {
ctx = api.WithNamespace(ctx, namespace)
}
parts := splitPath(req.URL.Path)
if len(parts) < 1 || req.Method != "GET" {
notFound(w, req)

View File

@ -25,6 +25,7 @@ import (
"testing"
"code.google.com/p/go.net/websocket"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
)
@ -159,27 +160,32 @@ func TestWatchParamParsing(t *testing.T) {
resourceVersion string
labelSelector string
fieldSelector string
namespace string
}{
{
rawQuery: "resourceVersion=1234",
resourceVersion: "1234",
labelSelector: "",
fieldSelector: "",
namespace: api.NamespaceAll,
}, {
rawQuery: "resourceVersion=314159&fields=Host%3D&labels=name%3Dfoo",
rawQuery: "namespace=default&resourceVersion=314159&fields=Host%3D&labels=name%3Dfoo",
resourceVersion: "314159",
labelSelector: "name=foo",
fieldSelector: "Host=",
namespace: api.NamespaceDefault,
}, {
rawQuery: "fields=ID%3dfoo&resourceVersion=1492",
rawQuery: "namespace=watchother&fields=ID%3dfoo&resourceVersion=1492",
resourceVersion: "1492",
labelSelector: "",
fieldSelector: "ID=foo",
namespace: "watchother",
}, {
rawQuery: "",
resourceVersion: "",
labelSelector: "",
fieldSelector: "",
namespace: api.NamespaceAll,
},
}
@ -187,6 +193,7 @@ func TestWatchParamParsing(t *testing.T) {
simpleStorage.requestedLabelSelector = nil
simpleStorage.requestedFieldSelector = nil
simpleStorage.requestedResourceVersion = "5" // Prove this is set in all cases
simpleStorage.requestedResourceNamespace = ""
dest.RawQuery = item.rawQuery
resp, err := http.Get(dest.String())
if err != nil {
@ -194,6 +201,9 @@ func TestWatchParamParsing(t *testing.T) {
continue
}
resp.Body.Close()
if e, a := item.namespace, simpleStorage.requestedResourceNamespace; e != a {
t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a)
}
if e, a := item.resourceVersion, simpleStorage.requestedResourceVersion; e != a {
t.Errorf("%v: expected %v, got %v", item.rawQuery, e, a)
}