Merge pull request #3288 from xigang/bugfix/gvk

bugfix: runtime.Object apiVersion/kind is empty
This commit is contained in:
karmada-bot 2023-03-17 15:27:21 +08:00 committed by GitHub
commit 40c202b937
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 0 deletions

View File

@ -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

View File

@ -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 {