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

View File

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

View File

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