Merge pull request #2082 from lavalamp/fix
Fix self linking of objects returned in lists.
This commit is contained in:
@@ -62,7 +62,25 @@ func (h *RESTHandler) setSelfLink(obj runtime.Object, req *http.Request) error {
|
||||
newURL.Path = path.Join(h.canonicalPrefix, req.URL.Path)
|
||||
newURL.RawQuery = ""
|
||||
newURL.Fragment = ""
|
||||
return h.selfLinker.SetSelfLink(obj, newURL.String())
|
||||
err := h.selfLinker.SetSelfLink(obj, newURL.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !runtime.IsListType(obj) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set self-link of objects in the list.
|
||||
items, err := runtime.ExtractList(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for i := range items {
|
||||
if err := h.setSelfLinkAddName(items[i], req); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return runtime.SetList(obj, items)
|
||||
}
|
||||
|
||||
// Like setSelfLink, but appends the object's name.
|
||||
|
||||
@@ -23,6 +23,11 @@ import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
||||
)
|
||||
|
||||
func IsListType(obj Object) bool {
|
||||
_, err := GetItemsPtr(obj)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
// GetItemsPtr returns a pointer to the list object's Items member.
|
||||
// If 'list' doesn't have an Items member, it's not really a list type
|
||||
// and an error will be returned.
|
||||
|
||||
@@ -26,6 +26,21 @@ import (
|
||||
"github.com/google/gofuzz"
|
||||
)
|
||||
|
||||
func TestIsList(t *testing.T) {
|
||||
tests := []struct {
|
||||
obj runtime.Object
|
||||
isList bool
|
||||
}{
|
||||
{&api.PodList{}, true},
|
||||
{&api.Pod{}, false},
|
||||
}
|
||||
for _, item := range tests {
|
||||
if e, a := item.isList, runtime.IsListType(item.obj); e != a {
|
||||
t.Errorf("%v: Expected %v, got %v", reflect.TypeOf(item.obj), e, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractList(t *testing.T) {
|
||||
pl := &api.PodList{
|
||||
Items: []api.Pod{
|
||||
|
||||
Reference in New Issue
Block a user