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

Created on 10 Oct 2017  路  16Comments  路  Source: kubernetes/client-go

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=

lifecyclrotten

Most helpful comment

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
}

All 16 comments

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

Prevent issues from auto-closing with an /lifecycle frozen comment.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or @fejta.
/lifecycle stale

/remove-lifecycle stale

@smarterclayton i am also seeing this issue. If you know how to fix it ,i would like to take a stab at it

/assign @krmayankk

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

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.

Reported in kubernetes/kubernetes#3030

Fix in progress in kubernetes/kubernetes#63972

Thank you for the update @nikhita!

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

Stale issues rot after 30d of inactivity.
Mark the issue as fresh with /remove-lifecycle rotten.
Rotten issues close after an additional 30d of inactivity.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle rotten

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

@fejta-bot: Closing this issue.

In response to this:

Rotten issues close after 30d of inactivity.
Reopen the issue with /reopen.
Mark the issue as fresh with /remove-lifecycle rotten.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

/reopen

@aermakov-zalando: You can't reopen an issue/PR unless you authored it or you are a collaborator.

In response to this:

/reopen

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

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
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

inetkiller picture inetkiller  路  6Comments

jgrobbel picture jgrobbel  路  3Comments

narasago picture narasago  路  3Comments

h0tbird picture h0tbird  路  3Comments