mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-17 15:13:08 +00:00
Always set ?namespace in query if specified
Revise our code to only call Request.Namespace() if a namespace *should* be present. For root scoped resources, namespace should be ignored. For namespaced resources, it is an error to have Namespace=="".
This commit is contained in:
8
pkg/client/cache/listwatch_test.go
vendored
8
pkg/client/cache/listwatch_test.go
vendored
@@ -42,7 +42,7 @@ func buildResourcePath(prefix, namespace, resource string) string {
|
||||
base := path.Join("/api", testapi.Version(), prefix)
|
||||
if len(namespace) > 0 {
|
||||
if !(testapi.Version() == "v1beta1" || testapi.Version() == "v1beta2") {
|
||||
base = path.Join(base, "ns", namespace)
|
||||
base = path.Join(base, "namespaces", namespace)
|
||||
}
|
||||
}
|
||||
return path.Join(base, resource)
|
||||
@@ -58,10 +58,8 @@ func buildQueryValues(namespace string, query url.Values) url.Values {
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(namespace) > 0 {
|
||||
if testapi.Version() == "v1beta1" || testapi.Version() == "v1beta2" {
|
||||
v.Set("namespace", namespace)
|
||||
}
|
||||
if testapi.Version() == "v1beta1" || testapi.Version() == "v1beta2" {
|
||||
v.Set("namespace", namespace)
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
@@ -66,7 +66,7 @@ func (e *events) Create(event *api.Event) (*api.Event, error) {
|
||||
}
|
||||
result := &api.Event{}
|
||||
err := e.client.Post().
|
||||
Namespace(event.Namespace).
|
||||
NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
|
||||
Resource("events").
|
||||
Body(event).
|
||||
Do().
|
||||
@@ -85,7 +85,7 @@ func (e *events) Update(event *api.Event) (*api.Event, error) {
|
||||
}
|
||||
result := &api.Event{}
|
||||
err := e.client.Put().
|
||||
Namespace(event.Namespace).
|
||||
NamespaceIfScoped(event.Namespace, len(event.Namespace) > 0).
|
||||
Resource("events").
|
||||
Name(event.Name).
|
||||
Body(event).
|
||||
@@ -98,7 +98,7 @@ func (e *events) Update(event *api.Event) (*api.Event, error) {
|
||||
func (e *events) List(label, field labels.Selector) (*api.EventList, error) {
|
||||
result := &api.EventList{}
|
||||
err := e.client.Get().
|
||||
Namespace(e.namespace).
|
||||
NamespaceIfScoped(e.namespace, len(e.namespace) > 0).
|
||||
Resource("events").
|
||||
SelectorParam("labels", label).
|
||||
SelectorParam("fields", field).
|
||||
@@ -115,7 +115,7 @@ func (e *events) Get(name string) (*api.Event, error) {
|
||||
|
||||
result := &api.Event{}
|
||||
err := e.client.Get().
|
||||
Namespace(e.namespace).
|
||||
NamespaceIfScoped(e.namespace, len(e.namespace) > 0).
|
||||
Resource("events").
|
||||
Name(name).
|
||||
Do().
|
||||
@@ -127,7 +127,7 @@ func (e *events) Get(name string) (*api.Event, error) {
|
||||
func (e *events) Watch(label, field labels.Selector, resourceVersion string) (watch.Interface, error) {
|
||||
return e.client.Get().
|
||||
Prefix("watch").
|
||||
Namespace(e.namespace).
|
||||
NamespaceIfScoped(e.namespace, len(e.namespace) > 0).
|
||||
Resource("events").
|
||||
Param("resourceVersion", resourceVersion).
|
||||
SelectorParam("labels", label).
|
||||
|
@@ -62,7 +62,9 @@ func TestEventCreate(t *testing.T) {
|
||||
}
|
||||
timeStamp := util.Now()
|
||||
event := &api.Event{
|
||||
//namespace: namespace{"default"},
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: api.NamespaceDefault,
|
||||
},
|
||||
InvolvedObject: *objReference,
|
||||
FirstTimestamp: timeStamp,
|
||||
LastTimestamp: timeStamp,
|
||||
@@ -80,7 +82,7 @@ func TestEventCreate(t *testing.T) {
|
||||
response, err := c.Setup().Events("").Create(event)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%#v should be nil.", err)
|
||||
t.Fatalf("%v should be nil.", err)
|
||||
}
|
||||
|
||||
if e, a := *objReference, response.InvolvedObject; !reflect.DeepEqual(e, a) {
|
||||
@@ -99,6 +101,9 @@ func TestEventGet(t *testing.T) {
|
||||
}
|
||||
timeStamp := util.Now()
|
||||
event := &api.Event{
|
||||
ObjectMeta: api.ObjectMeta{
|
||||
Namespace: "other",
|
||||
},
|
||||
InvolvedObject: *objReference,
|
||||
FirstTimestamp: timeStamp,
|
||||
LastTimestamp: timeStamp,
|
||||
@@ -116,7 +121,7 @@ func TestEventGet(t *testing.T) {
|
||||
response, err := c.Setup().Events("").Get("1")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%#v should be nil.", err)
|
||||
t.Fatalf("%v should be nil.", err)
|
||||
}
|
||||
|
||||
if e, r := event.InvolvedObject, response.InvolvedObject; !reflect.DeepEqual(e, r) {
|
||||
|
@@ -181,6 +181,14 @@ func (r *Request) Namespace(namespace string) *Request {
|
||||
return r
|
||||
}
|
||||
|
||||
// NamespaceIfScoped is a convenience function to set a namespace if scoped is true
|
||||
func (r *Request) NamespaceIfScoped(namespace string, scoped bool) *Request {
|
||||
if scoped {
|
||||
return r.Namespace(namespace)
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// AbsPath overwrites an existing path with the segments provided. Trailing slashes are preserved
|
||||
// when a single segment is passed.
|
||||
func (r *Request) AbsPath(segments ...string) *Request {
|
||||
@@ -320,7 +328,7 @@ func (r *Request) finalURL() string {
|
||||
query.Add(key, value)
|
||||
}
|
||||
|
||||
if r.namespaceSet && r.namespaceInQuery && len(r.namespace) > 0 {
|
||||
if r.namespaceSet && r.namespaceInQuery {
|
||||
query.Add("namespace", r.namespace)
|
||||
}
|
||||
|
||||
@@ -427,6 +435,14 @@ func (r *Request) Do() Result {
|
||||
return Result{err: &RequestConstructionError{r.err}}
|
||||
}
|
||||
|
||||
// TODO: added to catch programmer errors (invoking operations with an object with an empty namespace)
|
||||
if (r.verb == "GET" || r.verb == "PUT" || r.verb == "DELETE") && r.namespaceSet && len(r.resourceName) > 0 && len(r.namespace) == 0 {
|
||||
return Result{err: &RequestConstructionError{fmt.Errorf("an empty namespace may not be set when a resource name is provided")}}
|
||||
}
|
||||
if (r.verb == "POST") && r.namespaceSet && len(r.namespace) == 0 {
|
||||
return Result{err: &RequestConstructionError{fmt.Errorf("an empty namespace may not be set during creation")}}
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(r.verb, r.finalURL(), r.body)
|
||||
if err != nil {
|
||||
return Result{err: &RequestConstructionError{err}}
|
||||
|
Reference in New Issue
Block a user