Merge pull request #3288 from xigang/bugfix/gvk
bugfix: runtime.Object apiVersion/kind is empty
This commit is contained in:
commit
40c202b937
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue