Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#2952 from k8s-infra-cherrypick-rob…
Browse files Browse the repository at this point in the history
…ot/cherry-pick-2949-to-release-0.18

[release-0.18] 🐛  Fakeclient: preserve TypeMeta for PartialObjectMeta resources
  • Loading branch information
k8s-ci-robot authored Sep 21, 2024
2 parents 0c9f296 + bca7ccc commit b61de79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,10 @@ func (c *fakeClient) Get(ctx context.Context, key client.ObjectKey, obj client.O
return err
}

if _, isUnstructured := obj.(runtime.Unstructured); isUnstructured {
_, isUnstructured := obj.(runtime.Unstructured)
_, isPartialObject := obj.(*metav1.PartialObjectMetadata)

if isUnstructured || isPartialObject {
gvk, err := apiutil.GVKForObject(obj, c.scheme)
if err != nil {
return err
Expand Down
27 changes: 27 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,33 @@ var _ = Describe("Fake client", func() {
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})

It("should be able to retrieve objects by PartialObjectMetadata", func() {
By("Creating a Resource")
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
}
err := cl.Create(context.Background(), secret)
Expect(err).ToNot(HaveOccurred())

By("Fetching the resource using a PartialObjectMeta")
partialObjMeta := &metav1.PartialObjectMetadata{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "bar",
},
}
partialObjMeta.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret"))

err = cl.Get(context.Background(), client.ObjectKeyFromObject(partialObjMeta), partialObjMeta)
Expect(err).ToNot(HaveOccurred())

Expect(partialObjMeta.Kind).To(Equal("Secret"))
Expect(partialObjMeta.APIVersion).To(Equal("v1"))
})

It("should support filtering by labels and their values", func() {
By("Listing deployments with a particular label and value")
list := &appsv1.DeploymentList{}
Expand Down

0 comments on commit b61de79

Please sign in to comment.