Skip to content

Commit

Permalink
Merge pull request #2131 from vincepri/informer-cache-simplify
Browse files Browse the repository at this point in the history
🌱 Simplify cache.objectTypeForListObject
  • Loading branch information
k8s-ci-robot committed Jan 17, 2023
2 parents 5028a59 + bd096f9 commit 319c422
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
44 changes: 18 additions & 26 deletions pkg/cache/informer_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ package cache
import (
"context"
"fmt"
"reflect"
"strings"

apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -97,36 +97,28 @@ func (ip *informerCache) objectTypeForListObject(list client.ObjectList) (*schem
return nil, nil, err
}

// we need the non-list GVK, so chop off the "List" from the end of the kind
if strings.HasSuffix(gvk.Kind, "List") && apimeta.IsListType(list) {
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
}
// We need the non-list GVK, so chop off the "List" from the end of the kind.
gvk.Kind = strings.TrimSuffix(gvk.Kind, "List")

_, isUnstructured := list.(*unstructured.UnstructuredList)
var cacheTypeObj runtime.Object
if isUnstructured {
// Handle unstructured.UnstructuredList.
if _, isUnstructured := list.(*unstructured.UnstructuredList); isUnstructured {
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(gvk)
cacheTypeObj = u
} else {
itemsPtr, err := apimeta.GetItemsPtr(list)
if err != nil {
return nil, nil, err
}
// http://knowyourmeme.com/memes/this-is-fine
elemType := reflect.Indirect(reflect.ValueOf(itemsPtr)).Type().Elem()
if elemType.Kind() != reflect.Ptr {
elemType = reflect.PtrTo(elemType)
}

cacheTypeValue := reflect.Zero(elemType)
var ok bool
cacheTypeObj, ok = cacheTypeValue.Interface().(runtime.Object)
if !ok {
return nil, nil, fmt.Errorf("cannot get cache for %T, its element %T is not a runtime.Object", list, cacheTypeValue.Interface())
}
return &gvk, u, nil
}
// Handle metav1.PartialObjectMetadataList.
if _, isPartialObjectMetadata := list.(*metav1.PartialObjectMetadataList); isPartialObjectMetadata {
pom := &metav1.PartialObjectMetadata{}
pom.SetGroupVersionKind(gvk)
return &gvk, pom, nil
}

// Any other list type should have a corresponding non-list type registered
// in the scheme. Use that to create a new instance of the non-list type.
cacheTypeObj, err := ip.scheme.New(gvk)
if err != nil {
return nil, nil, err
}
return &gvk, cacheTypeObj, nil
}

Expand Down
20 changes: 17 additions & 3 deletions pkg/cache/informer_cache_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
. "github.com/onsi/gomega"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -55,7 +56,21 @@ var _ = Describe("ip.objectTypeForListObject", func() {
referenceUnstructured := &unstructured.Unstructured{}
referenceUnstructured.SetGroupVersionKind(*gvk)
Expect(obj).To(Equal(referenceUnstructured))
})

It("should find the object type for partial object metadata lists", func() {
partialList := &metav1.PartialObjectMetadataList{}
partialList.APIVersion = ("v1")
partialList.Kind = "PodList"

gvk, obj, err := ip.objectTypeForListObject(partialList)
Expect(err).ToNot(HaveOccurred())
Expect(gvk.Group).To(Equal(""))
Expect(gvk.Version).To(Equal("v1"))
Expect(gvk.Kind).To(Equal("Pod"))
referencePartial := &metav1.PartialObjectMetadata{}
referencePartial.SetGroupVersionKind(*gvk)
Expect(obj).To(Equal(referencePartial))
})

It("should find the object type of a list with a slice of literals items field", func() {
Expand All @@ -64,9 +79,8 @@ var _ = Describe("ip.objectTypeForListObject", func() {
Expect(gvk.Group).To(Equal(""))
Expect(gvk.Version).To(Equal("v1"))
Expect(gvk.Kind).To(Equal("Pod"))
var referencePod *corev1.Pod
referencePod := &corev1.Pod{}
Expect(obj).To(Equal(referencePod))

})

It("should find the object type of a list with a slice of pointers items field", func() {
Expand All @@ -88,7 +102,7 @@ var _ = Describe("ip.objectTypeForListObject", func() {
Expect(gvk.Group).To(Equal(itemPointerSliceTypeGroupName))
Expect(gvk.Version).To(Equal(itemPointerSliceTypeVersion))
Expect(gvk.Kind).To(Equal("UnconventionalListType"))
var referenceObject *controllertest.UnconventionalListType
referenceObject := &controllertest.UnconventionalListType{}
Expect(obj).To(Equal(referenceObject))
})
})
Expand Down

0 comments on commit 319c422

Please sign in to comment.