Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃尡 Simplify cache.objectTypeForListObject #2131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 18 additions & 26 deletions pkg/cache/informer_cache.go
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
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