Skip to content

Commit 6c2f3d0

Browse files
committed
pkg/client/fake/client.go: get gvk without potential panic
1 parent 0f0740d commit 6c2f3d0

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

pkg/client/fake/client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"os"
23+
"strings"
2324

2425
"k8s.io/apimachinery/pkg/api/meta"
2526
"k8s.io/apimachinery/pkg/runtime"
@@ -78,8 +79,15 @@ func (c *fakeClient) Get(ctx context.Context, key client.ObjectKey, obj runtime.
7879
}
7980

8081
func (c *fakeClient) List(ctx context.Context, opts *client.ListOptions, list runtime.Object) error {
81-
gvk := opts.Raw.TypeMeta.GroupVersionKind()
82+
gvk, err := apiutil.GVKForObject(list, scheme.Scheme)
83+
if err != nil {
84+
return err
85+
}
86+
if strings.HasSuffix(gvk.Kind, "List") {
87+
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
88+
}
8289
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
90+
8391
o, err := c.tracker.List(gvr, gvk, opts.Namespace)
8492
if err != nil {
8593
return err

0 commit comments

Comments
 (0)