Skip to content

Commit bd5262e

Browse files
author
Shawn Hurley
committed
adding test cases for getting and listing unstructured types.
1 parent 550fd22 commit bd5262e

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

pkg/client/client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,12 @@ func (c *client) List(ctx context.Context, opts *ListOptions, obj runtime.Object
148148
if opts != nil {
149149
namespace = opts.Namespace
150150
}
151-
return r.Get().
151+
err = r.Get().
152152
NamespaceIfScoped(namespace, r.isNamespaced()).
153153
Resource(r.resource()).
154154
Body(obj).
155155
VersionedParams(opts.AsListOptions(), c.paramCodec).
156156
Do().
157157
Into(obj)
158+
return err
158159
}

pkg/client/client_cache.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ func (c *clientCache) getResourceByType(obj runtime.Object) (*resourceMeta, erro
133133
// If the object is a list, the resource represents the item's type instead.
134134
func (c *clientCache) getResource(obj runtime.Object) (*resourceMeta, error) {
135135
_, isUnstructured := obj.(*unstructured.Unstructured)
136-
if isUnstructured {
136+
_, isUnstructuredList := obj.(*unstructured.UnstructuredList)
137+
if isUnstructured || isUnstructuredList {
137138
return c.getUnstructuredResourceByGVK(obj)
138139
}
139140
return c.getResourceByType(obj)

pkg/client/client_test.go

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3030
"k8s.io/apimachinery/pkg/runtime"
31+
"k8s.io/apimachinery/pkg/runtime/schema"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
3233

3334
kscheme "k8s.io/client-go/kubernetes/scheme"
@@ -527,23 +528,28 @@ var _ = Describe("Client", func() {
527528
dep, err := clientset.AppsV1().Deployments(ns).Create(dep)
528529
Expect(err).NotTo(HaveOccurred())
529530

530-
By("encoding the Deployment as unstructured")
531-
var u runtime.Unstructured = &unstructured.Unstructured{}
532-
scheme.Convert(dep, u, nil)
533-
534531
cl, err := client.New(cfg, client.Options{})
535532
Expect(err).NotTo(HaveOccurred())
536533
Expect(cl).NotTo(BeNil())
537534

535+
By("encoding the Deployment as unstructured")
536+
var u runtime.Unstructured = &unstructured.Unstructured{}
537+
scheme.Convert(dep, u, nil)
538+
538539
By("fetching the created Deployment")
539-
var actual appsv1.Deployment
540+
var actual unstructured.Unstructured
541+
actual.SetGroupVersionKind(schema.GroupVersionKind{
542+
Group: "apps",
543+
Kind: "Deployment",
544+
Version: "v1",
545+
})
540546
key := client.ObjectKey{Namespace: ns, Name: dep.Name}
541547
err = cl.Get(context.TODO(), key, &actual)
542548
Expect(err).NotTo(HaveOccurred())
543549
Expect(actual).NotTo(BeNil())
544550

545551
By("validating the fetched Deployment equals the created one")
546-
Expect(dep).To(Equal(&actual))
552+
Expect(u).To(Equal(&actual))
547553

548554
close(done)
549555
})
@@ -583,14 +589,19 @@ var _ = Describe("Client", func() {
583589
Expect(cl).NotTo(BeNil())
584590

585591
By("fetching the created Node")
586-
var actual corev1.Node
592+
var actual unstructured.Unstructured
593+
actual.SetGroupVersionKind(schema.GroupVersionKind{
594+
Group: "",
595+
Kind: "Node",
596+
Version: "v1",
597+
})
587598
key := client.ObjectKey{Namespace: ns, Name: node.Name}
588599
err = cl.Get(context.TODO(), key, &actual)
589600
Expect(err).NotTo(HaveOccurred())
590601
Expect(actual).NotTo(BeNil())
591602

592603
By("validating the fetched Node equals the created one")
593-
Expect(node).To(Equal(&actual))
604+
Expect(u).To(Equal(&actual))
594605

595606
close(done)
596607
})
@@ -663,6 +674,38 @@ var _ = Describe("Client", func() {
663674
close(done)
664675
}, serverSideTimeoutSeconds)
665676

677+
It("should fetch unstructured collection of objects", func(done Done) {
678+
By("create an initial object")
679+
_, err := clientset.AppsV1().Deployments(ns).Create(dep)
680+
Expect(err).NotTo(HaveOccurred())
681+
682+
cl, err := client.New(cfg, client.Options{})
683+
Expect(err).NotTo(HaveOccurred())
684+
685+
By("listing all objects of that type in the cluster")
686+
deps := &unstructured.UnstructuredList{}
687+
deps.SetGroupVersionKind(schema.GroupVersionKind{
688+
Group: "apps",
689+
Kind: "DeploymentList",
690+
Version: "v1",
691+
})
692+
err = cl.List(context.Background(), nil, deps)
693+
Expect(err).NotTo(HaveOccurred())
694+
695+
Expect(deps.Items).NotTo(BeEmpty())
696+
hasDep := false
697+
for _, item := range deps.Items {
698+
if item.GetName() == dep.Name && item.GetNamespace() == dep.Namespace {
699+
fmt.Printf("HERE!!!!!!!! ITEM: %v\n\n", item)
700+
hasDep = true
701+
fmt.Printf("HERE hasDep: %v\n\n", hasDep)
702+
break
703+
}
704+
}
705+
Expect(hasDep).To(BeTrue())
706+
close(done)
707+
}, serverSideTimeoutSeconds)
708+
666709
It("should return an empty list if there are no matching objects", func(done Done) {
667710
cl, err := client.New(cfg, client.Options{})
668711
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)