client-go: object return by client-go informer don't have api Kind info

When I use informer to watch cluster resource infos, I find runtime.Object return by informer don’t include object Kind info. fmt.Println(obj.GetObjectKind().GetVersionKind().String()) print /, Kind=

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Comments: 28 (13 by maintainers)

Commits related to this issue

Most upvoted comments

For those of us still looking for a concise way to add TypeMeta information to runtime objects, here you go!

// addTypeInformationToObject adds TypeMeta information to a runtime.Object based upon the loaded scheme.Scheme
// inspired by: https://github.com/kubernetes/cli-runtime/blob/v0.19.2/pkg/printers/typesetter.go#L41
func addTypeInformationToObject(obj runtime.Object) error {
    gvks, _, err := scheme.Scheme.ObjectKinds(obj)
    if err != nil {
        return fmt.Errorf("missing apiVersion or kind and cannot assign it; %w", err)
    }

    for _, gvk := range gvks {
        if len(gvk.Kind) == 0 {
            continue
        }
        if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal {
            continue
        }
        obj.GetObjectKind().SetGroupVersionKind(gvk)
        break
    }

    return nil
}

Reported in kubernetes/kubernetes#3030

Fix in progress in kubernetes/kubernetes#63972

The k8s REST API does not provide the Kind value. kubectl (or one of its dependencies) inserts the Kind information. Perhaps the code that figures the Kind info can be moved to client-go? Just for example, see get cmd and how it inserts the List value.

I don’t think the Kind is returned either with List

Thank you for the update @nikhita!