mirror of
https://github.com/kubernetes/client-go.git
synced 2025-07-03 10:16:40 +00:00
avoid panic when subresource is a list
Kubernetes-commit: 2e3b937dcdb02cab74213a5608cc4dac39c6423e
This commit is contained in:
parent
51f5488cb9
commit
fae3dcdbf6
@ -64,6 +64,12 @@ func getObject(version, kind, name string) *unstructured.Unstructured {
|
||||
}
|
||||
}
|
||||
|
||||
func getObjectFromJSON(b []byte) *unstructured.Unstructured {
|
||||
obj := &unstructured.Unstructured{}
|
||||
_ = obj.UnmarshalJSON(b) // can ignore parse error because the comparison will fail
|
||||
return obj
|
||||
}
|
||||
|
||||
func getClientServer(h func(http.ResponseWriter, *http.Request)) (Interface, *httptest.Server, error) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(h))
|
||||
cl, err := NewForConfig(&restclient.Config{
|
||||
@ -359,6 +365,15 @@ func TestGet(t *testing.T) {
|
||||
resp: getJSON("vTest", "srTest", "namespaced_subresource_get"),
|
||||
want: getObject("vTest", "srTest", "namespaced_subresource_get"),
|
||||
},
|
||||
{
|
||||
resource: "rtest",
|
||||
subresource: []string{"srtest"},
|
||||
namespace: "nstest",
|
||||
name: "namespaced_subresource_get_list",
|
||||
path: "/apis/gtest/vtest/namespaces/nstest/rtest/namespaced_subresource_get_list/srtest",
|
||||
resp: getListJSON("vTest", "srTest", getJSON("vTest", "srTest", "a1")),
|
||||
want: getObjectFromJSON(getListJSON("vTest", "srTest", getJSON("vTest", "srTest", "a1"))),
|
||||
},
|
||||
}
|
||||
for _, tc := range tcs {
|
||||
resource := schema.GroupVersionResource{Group: "gtest", Version: "vtest", Resource: tc.resource}
|
||||
|
@ -289,11 +289,12 @@ func (c *dynamicResourceClient) Get(ctx context.Context, name string, opts metav
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uncastObj, err := runtime.Decode(unstructured.UnstructuredJSONScheme, retBytes)
|
||||
obj := &unstructured.Unstructured{}
|
||||
err = runtime.DecodeInto(unstructured.UnstructuredJSONScheme, retBytes, obj)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return uncastObj.(*unstructured.Unstructured), nil
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
func (c *dynamicResourceClient) List(ctx context.Context, opts metav1.ListOptions) (*unstructured.UnstructuredList, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user