diff --git a/pkg/util/fedinformer/keys/keys.go b/pkg/util/fedinformer/keys/keys.go index 9d5698beca10..019e06638155 100644 --- a/pkg/util/fedinformer/keys/keys.go +++ b/pkg/util/fedinformer/keys/keys.go @@ -74,7 +74,18 @@ func ClusterWideKeyFunc(obj interface{}) (ClusterWideKey, error) { return key, fmt.Errorf("object has no meta: %v", err) } + // When using a typed client, decoding to a versioned struct (not an internal API type), the apiVersion/kind + // information will be dropped. Therefore, the APIVersion/Kind information of runtime.Object needs to be verified. + // See issue: https://github.com/kubernetes/kubernetes/issues/80609 gvk := runtimeObject.GetObjectKind().GroupVersionKind() + if len(gvk.Kind) == 0 { + return key, fmt.Errorf("runtime object has no kind") + } + + if len(gvk.Version) == 0 { + return key, fmt.Errorf("runtime object has no version") + } + key.Group = gvk.Group key.Version = gvk.Version key.Kind = gvk.Kind diff --git a/pkg/util/fedinformer/keys/keys_test.go b/pkg/util/fedinformer/keys/keys_test.go index 14e85b7ba989..d3faa8af6c0c 100644 --- a/pkg/util/fedinformer/keys/keys_test.go +++ b/pkg/util/fedinformer/keys/keys_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -50,6 +51,12 @@ var ( Name: "bar", }, } + deploymentObj = appsv1.Deployment{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: "foo", + Name: "bar", + }, + } ) func TestClusterWideKeyFunc(t *testing.T) { @@ -93,6 +100,11 @@ func TestClusterWideKeyFunc(t *testing.T) { object: nil, expectErr: true, }, + { + name: "non APIVersion and kind runtime object should be error", + object: deploymentObj, + expectErr: true, + }, } for _, test := range tests {