Skip to content

Commit 36fab0f

Browse files
committed
Preserve TypeMeta for PartialObjectMeta resources
This updates the fake client to retain the PartialObjectMeta TypeMeta when getting resources.
1 parent 2eb879f commit 36fab0f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

pkg/client/fake/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,10 @@ func (c *fakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.O
508508
return err
509509
}
510510

511-
if _, isUnstructured := obj.(runtime.Unstructured); isUnstructured {
511+
_, isUnstructured := obj.(runtime.Unstructured)
512+
_, isPartialObject := obj.(*metav1.PartialObjectMetadata)
513+
514+
if isUnstructured || isPartialObject {
512515
gvk, err := apiutil.GVKForObject(obj, c.scheme)
513516
if err != nil {
514517
return err

pkg/client/fake/client_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,31 @@ var _ = Describe("Fake client", func() {
339339
Expect(apierrors.IsNotFound(err)).To(BeTrue())
340340
})
341341

342+
It("should be able to retrieve objects by PartialObjectMetadata", func() {
343+
By("Creating a Resource")
344+
secret := &corev1.Secret{
345+
ObjectMeta: metav1.ObjectMeta{
346+
Name: "foo",
347+
Namespace: "bar",
348+
},
349+
}
350+
err := cl.Create(context.Background(), secret)
351+
Expect(err).ToNot(HaveOccurred())
352+
353+
partialObjMeta := &metav1.PartialObjectMetadata{
354+
ObjectMeta: metav1.ObjectMeta{
355+
Name: "foo",
356+
Namespace: "bar",
357+
},
358+
}
359+
partialObjMeta.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret"))
360+
361+
It("should retain the metadata for the partial object")
362+
err = cl.Get(context.Background(), client.ObjectKeyFromObject(partialObjMeta), partialObjMeta)
363+
Expect(err).ToNot(HaveOccurred())
364+
Expect(partialObjMeta.Kind).To(Equal("Secret"))
365+
})
366+
342367
It("should support filtering by labels and their values", func() {
343368
By("Listing deployments with a particular label and value")
344369
list := &appsv1.DeploymentList{}

0 commit comments

Comments
 (0)