Skip to content

Commit e4d7379

Browse files
author
Shawn Hurley
committed
Adding test cases for getting and listing unstructured types.
1 parent 1088c68 commit e4d7379

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
@@ -151,13 +151,14 @@ func (c *client) List(ctx context.Context, opts *ListOptions, obj runtime.Object
151151
if opts != nil {
152152
namespace = opts.Namespace
153153
}
154-
return r.Get().
154+
err = r.Get().
155155
NamespaceIfScoped(namespace, r.isNamespaced()).
156156
Resource(r.resource()).
157157
Body(obj).
158158
VersionedParams(opts.AsListOptions(), c.paramCodec).
159159
Do().
160160
Into(obj)
161+
return err
161162
}
162163

163164
// Status implements client.StatusClient

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"
@@ -642,23 +643,28 @@ var _ = Describe("Client", func() {
642643
dep, err := clientset.AppsV1().Deployments(ns).Create(dep)
643644
Expect(err).NotTo(HaveOccurred())
644645

645-
By("encoding the Deployment as unstructured")
646-
var u runtime.Unstructured = &unstructured.Unstructured{}
647-
scheme.Convert(dep, u, nil)
648-
649646
cl, err := client.New(cfg, client.Options{})
650647
Expect(err).NotTo(HaveOccurred())
651648
Expect(cl).NotTo(BeNil())
652649

650+
By("encoding the Deployment as unstructured")
651+
var u runtime.Unstructured = &unstructured.Unstructured{}
652+
scheme.Convert(dep, u, nil)
653+
653654
By("fetching the created Deployment")
654-
var actual appsv1.Deployment
655+
var actual unstructured.Unstructured
656+
actual.SetGroupVersionKind(schema.GroupVersionKind{
657+
Group: "apps",
658+
Kind: "Deployment",
659+
Version: "v1",
660+
})
655661
key := client.ObjectKey{Namespace: ns, Name: dep.Name}
656662
err = cl.Get(context.TODO(), key, &actual)
657663
Expect(err).NotTo(HaveOccurred())
658664
Expect(actual).NotTo(BeNil())
659665

660666
By("validating the fetched Deployment equals the created one")
661-
Expect(dep).To(Equal(&actual))
667+
Expect(u).To(Equal(&actual))
662668

663669
close(done)
664670
})
@@ -698,14 +704,19 @@ var _ = Describe("Client", func() {
698704
Expect(cl).NotTo(BeNil())
699705

700706
By("fetching the created Node")
701-
var actual corev1.Node
707+
var actual unstructured.Unstructured
708+
actual.SetGroupVersionKind(schema.GroupVersionKind{
709+
Group: "",
710+
Kind: "Node",
711+
Version: "v1",
712+
})
702713
key := client.ObjectKey{Namespace: ns, Name: node.Name}
703714
err = cl.Get(context.TODO(), key, &actual)
704715
Expect(err).NotTo(HaveOccurred())
705716
Expect(actual).NotTo(BeNil())
706717

707718
By("validating the fetched Node equals the created one")
708-
Expect(node).To(Equal(&actual))
719+
Expect(u).To(Equal(&actual))
709720

710721
close(done)
711722
})
@@ -778,6 +789,38 @@ var _ = Describe("Client", func() {
778789
close(done)
779790
}, serverSideTimeoutSeconds)
780791

792+
It("should fetch unstructured collection of objects", func(done Done) {
793+
By("create an initial object")
794+
_, err := clientset.AppsV1().Deployments(ns).Create(dep)
795+
Expect(err).NotTo(HaveOccurred())
796+
797+
cl, err := client.New(cfg, client.Options{})
798+
Expect(err).NotTo(HaveOccurred())
799+
800+
By("listing all objects of that type in the cluster")
801+
deps := &unstructured.UnstructuredList{}
802+
deps.SetGroupVersionKind(schema.GroupVersionKind{
803+
Group: "apps",
804+
Kind: "DeploymentList",
805+
Version: "v1",
806+
})
807+
err = cl.List(context.Background(), nil, deps)
808+
Expect(err).NotTo(HaveOccurred())
809+
810+
Expect(deps.Items).NotTo(BeEmpty())
811+
hasDep := false
812+
for _, item := range deps.Items {
813+
if item.GetName() == dep.Name && item.GetNamespace() == dep.Namespace {
814+
fmt.Printf("HERE!!!!!!!! ITEM: %v\n\n", item)
815+
hasDep = true
816+
fmt.Printf("HERE hasDep: %v\n\n", hasDep)
817+
break
818+
}
819+
}
820+
Expect(hasDep).To(BeTrue())
821+
close(done)
822+
}, serverSideTimeoutSeconds)
823+
781824
It("should return an empty list if there are no matching objects", func(done Done) {
782825
cl, err := client.New(cfg, client.Options{})
783826
Expect(err).NotTo(HaveOccurred())

0 commit comments

Comments
 (0)