Skip to content

Fix non-cache client list #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ func (c *client) List(ctx context.Context, opts *ListOptions, obj runtime.Object
if err != nil {
return err
}
namespace := ""
if opts != nil {
namespace = opts.Namespace
}
return r.Get().
NamespaceIfScoped(opts.Namespace, r.isNamespaced()).
NamespaceIfScoped(namespace, r.isNamespaced()).
Resource(r.resource()).
Body(obj).
VersionedParams(opts.AsListOptions(), c.paramCodec).
Expand Down
11 changes: 10 additions & 1 deletion pkg/client/client_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package client

import (
"reflect"
"strings"
"sync"

"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -49,11 +50,18 @@ type clientCache struct {
}

// newResource maps obj to a Kubernetes Resource and constructs a client for that Resource.
// If the object is a list, the resource represents the item's type instead.
func (c *clientCache) newResource(obj runtime.Object) (*resourceMeta, error) {
gvk, err := apiutil.GVKForObject(obj, c.scheme)
if err != nil {
return nil, err
}

if strings.HasSuffix(gvk.Kind, "List") && meta.IsListType(obj) {
// if this was a list, treat it as a request for the item's resource
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
}

client, err := apiutil.RESTClientForGVK(gvk, c.config, c.codecs)
if err != nil {
return nil, err
Expand All @@ -65,7 +73,8 @@ func (c *clientCache) newResource(obj runtime.Object) (*resourceMeta, error) {
return &resourceMeta{Interface: client, mapping: mapping, gvk: gvk}, nil
}

// getResource returns a raw rest.Client for the given object type.
// getResource returns the resource meta information for the given type of object.
// If the object is a list, the resource represents the item's type instead.
func (c *clientCache) getResource(obj runtime.Object) (*resourceMeta, error) {
typ := reflect.TypeOf(obj)

Expand Down
21 changes: 19 additions & 2 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ package client_test

import (
"context"

"fmt"

"sync/atomic"

. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -353,7 +351,26 @@ var _ = Describe("Client", func() {

Describe("List", func() {
It("should fetch collection of objects", func() {
By("creating an initial object")
dep, err := clientset.AppsV1().Deployments(ns).Create(dep)
Expect(err).NotTo(HaveOccurred())

cl, err := client.New(cfg, client.Options{})
Expect(err).NotTo(HaveOccurred())

By("listing all objects of that type in the cluster")
deps := &appsv1.DeploymentList{}
Expect(cl.List(context.Background(), nil, deps)).NotTo(HaveOccurred())

Expect(deps.Items).NotTo(BeEmpty())
hasDep := false
for _, item := range deps.Items {
if item.Name == dep.Name && item.Namespace == dep.Namespace {
hasDep = true
break
}
}
Expect(hasDep).To(BeTrue())
})

It("should return an empty list if there are no matching objects", func() {
Expand Down
10 changes: 7 additions & 3 deletions pkg/client/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,14 @@ func (o *ListOptions) SetFieldSelector(selRaw string) error {
// This may mutate the Raw field.
func (o *ListOptions) AsListOptions() *metav1.ListOptions {
if o == nil {
return nil
return &metav1.ListOptions{}
}
if o.LabelSelector != nil {
o.Raw.LabelSelector = o.LabelSelector.String()
}
if o.FieldSelector != nil {
o.Raw.FieldSelector = o.FieldSelector.String()
}
o.Raw.LabelSelector = o.LabelSelector.String()
o.Raw.FieldSelector = o.FieldSelector.String()
return o.Raw
}

Expand Down