diff --git a/pkg/apiserver/apiserver_test.go b/pkg/apiserver/apiserver_test.go index bcb1aab6cdb..6a536f34673 100644 --- a/pkg/apiserver/apiserver_test.go +++ b/pkg/apiserver/apiserver_test.go @@ -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 { diff --git a/pkg/apiserver/watch.go b/pkg/apiserver/watch.go index 9d4c54e052d..9a00c626215 100644 --- a/pkg/apiserver/watch.go +++ b/pkg/apiserver/watch.go @@ -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) diff --git a/pkg/apiserver/watch_test.go b/pkg/apiserver/watch_test.go index 78308a158f5..2e090db3534 100644 --- a/pkg/apiserver/watch_test.go +++ b/pkg/apiserver/watch_test.go @@ -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) }