Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typemeta is empty in all resource #541

Closed
shsjshentao opened this issue Jan 8, 2019 · 15 comments
Closed

typemeta is empty in all resource #541

shsjshentao opened this issue Jan 8, 2019 · 15 comments
Labels
lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.

Comments

@shsjshentao
Copy link

out, _, err := r.decoder.Decode(r.body, nil, obj)

It is not only CRD and list resource, all resource has this issue.
I have tried 9.0 and 10.0 version.

K8S response is ok and contains "kind" and "apiVersion".
And after decoder, these two parameters has lost.

for json type, i can json.unmarshal to deal with this problem like
client.CoreV1().RESTClient().Get().Resource("pods").Name("nginx-controller-zwdhq").Namespace("default").DoRaw()

However, I want to use protobuf by
config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
Then no way to decode it properly.

Is it a bug and how to decode by protobuf with typemeta?

@liggitt
Copy link
Member

liggitt commented Jan 8, 2019

decoding to go structs drops apiVersion/kind, because the type info is inherent in the object. decoding to unstructured objects (like the dynamic client does) preserves that information

@zacharya
Copy link

Somewhat related, I notice none of the types have struct tags for YAML:
type PodSecurityPolicy struct { metav1.TypeMetajson:",inline" yaml:",inline"// Standard object's metadata. // More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata // +optional metav1.ObjectMetajson:"metadata,omitempty" yaml:"metadata" protobuf:"bytes,1,opt,name=metadata"`

// spec defines the policy enforced.
// +optional
Spec PodSecurityPolicySpec `json:"spec,omitempty" protobuf:"bytes,2,opt,name=spec"`

}
`

I'm trying to read in raw pod security policy in YAML (including kind, metadata, apiVersion) and Unmarshal unto a v1beta1.PodSecurityPolicy. Would it be worth adding struct tags or just use the dynamic client? This seems to work when I modify locally, but not sure if this is a good approach.

@shsjshentao
Copy link
Author

why does decoder clear GVK infos when deserialization?

// Decode does not do conversion. It removes the gvk during deserialization.
func (d DirectDecoder) Decode(data []byte, defaults *schema.GroupVersionKind, into runtime.Object) (runtime.Object, *schema.GroupVersionKind, error) {
	obj, gvk, err := d.Decoder.Decode(data, defaults, into)
	if obj != nil {
		kind := obj.GetObjectKind()
		// clearing the gvk is just a convention of a codec
		kind.SetGroupVersionKind(schema.GroupVersionKind{})
	}
	return obj, gvk, err
}

@urandom
Copy link

urandom commented May 5, 2019

decoding to go structs drops apiVersion/kind, because the type info is inherent in the object. decoding to unstructured objects (like the dynamic client does) preserves that information

Even through it is inherent, the object may later be used in a way so that the type information might not be easily accesible (or at all). Whereas the type meta information was already there and the client is doing extra work to just discard it, for no obvious reason.

@wfernandes
Copy link

wfernandes commented Jun 10, 2019

We are looking for the TypeMeta information for an object to be available as well because we are using that object as the owner reference for other objects being created in turn.

Use Case:

  1. We have a CRD for MyObject.
  2. We get an OnAdd(myObj)
  3. We need to create a Deployment (myDeployment) with OwnerReference pointing to the myObj. The OwnerReference requires the following information: Kind, ApiVersion, Name & UID. We are missing the Kind and ApiVersion from TypeMeta.
  4. If we delete the CRD directly, then we expect the instance myObj to be deleted but we also want myDeployment to be deleted as well.

Referencing @shsjshentao comment above
#541 (comment)
We noticed that the Decode function does clear out the GVK information but does return it. However, in a call to Decode for example here,

out, _, err := r.decoder.Decode(r.body, nil, nil)
we ignore the gvk being returned as well.

If someone can point us in the right direction, we don't mind creating a PR for this.

@wfernandes
Copy link

@liggitt Please let me know if you'd like more clarification on the use-case above. Thanks.

@fejta-bot
Copy link

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

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Oct 1, 2019
@dotbalo
Copy link

dotbalo commented Jul 20, 2020

How to solve it? I can't get the information of typeMeta, but I haven't found a solution.

item, err := clientSet.BatchV1beta1().CronJobs(namespaceName).Get(context.TODO(), resourceName, metav1.GetOptions{})

item.APIVersion is empty...

@miton18
Copy link

miton18 commented Sep 9, 2020

Same issue here

@aghorba
Copy link

aghorba commented Apr 5, 2021

I figure I leave this here in-case anyone runs into this page when searching. I had this same issue when running tests (but connecting to a real cluster). When objects were created the TypeMeta would then be cleared (even if it was explicitly set beforehand). Long story short, I noticed that the client.Client being created in my tests differed from the one created with production code. The change that fixed it for me came down to when setting up the manager:

import ctrl "sigs.k8s.io/controller-runtime"

...

manager, err := ctrl.NewManager(ctrl.GetConfigOrDie(), manager.Options{
    MetricsBindAddress: metricsAddr,
    Scheme: scheme,
})

When I used "ctrl.GetConfigOrDie()" and then "manager.GetClient()" when setting up the reconcilers, this fixed it for me. Previously, I had been using "client.New" from package "sigs.k8s.io/controller-runtime/pkg/client" which is used in the generated scaffolded code from kubebuilder. Not sure why it differs, but hope it helps someone else out there having the same issue.

@zoroyihan
Copy link

Is there anyway to solve this?

@HeGaoYuan
Copy link

@liggitt @caesarxuchao
Relate Issue: #60
Hi, the typemeta is empty problem is very confusing. And I am sorry that your explanations are not easy to understand.
Could you please make a more detailed explanation about why drops apiVersion/kind when decoding to go structs.
Thanks

@dingxiong
Copy link

Any update?

lyarwood added a commit to lyarwood/kubevirt that referenced this issue Apr 6, 2023
…PIVersion

As set out in issue kubevirt#9261 there have been reports of older
VirtualMachineInstancetypeSpecRevision objects being stored in
ControllerRevisions without APIVersion set. This is likely due to a
known client-go issue [1] stripping TypeMeta from returned objects.

Given the limited support for v1alpha1 and the fact that we moved away
from VirtualMachineInstancetypeSpecRevision to complete objects being
captured in ControllerRevisions by v1alpah2 this change simply works
around this issue by ignoring the value of APIVersion. This should allow
the conversion to complete correctly until we upgrade these
ControllerRevisions in the future.

[1] kubernetes/client-go#541

Signed-off-by: Lee Yarwood <lyarwood@redhat.com>
lyarwood added a commit to lyarwood/kubevirt that referenced this issue Apr 11, 2023
…PIVersion

As set out in issue kubevirt#9261 there have been reports of older
VirtualMachineInstancetypeSpecRevision objects being stored in
ControllerRevisions without APIVersion set. This is likely due to a
known client-go issue [1] stripping TypeMeta from returned objects.

Given the limited support for v1alpha1 and the fact that we moved away
from VirtualMachineInstancetypeSpecRevision to complete objects being
captured in ControllerRevisions by v1alpah2 this change simply works
around this issue by ignoring the value of APIVersion. This should allow
the conversion to complete correctly until we upgrade these
ControllerRevisions in the future.

[1] kubernetes/client-go#541

Signed-off-by: Lee Yarwood <lyarwood@redhat.com>
lyarwood added a commit to lyarwood/kubevirt that referenced this issue Apr 11, 2023
…PIVersion

As set out in issue kubevirt#9261 there have been reports of older
VirtualMachineInstancetypeSpecRevision objects being stored in
ControllerRevisions without APIVersion set. This is likely due to a
known client-go issue [1] stripping TypeMeta from returned objects.

Given the limited support for v1alpha1 and the fact that we moved away
from VirtualMachineInstancetypeSpecRevision to complete objects being
captured in ControllerRevisions by v1alpah2 this change simply works
around this issue by ignoring the value of APIVersion. This should allow
the conversion to complete correctly until we upgrade these
ControllerRevisions in the future.

[1] kubernetes/client-go#541

Signed-off-by: Lee Yarwood <lyarwood@redhat.com>
lyarwood added a commit to lyarwood/kubevirt that referenced this issue Apr 17, 2023
…PIVersion

As set out in issue kubevirt#9261 there have been reports of older
VirtualMachineInstancetypeSpecRevision objects being stored in
ControllerRevisions without APIVersion set. This is likely due to a
known client-go issue [1] stripping TypeMeta from returned objects.

Given the limited support for v1alpha1 and the fact that we moved away
from VirtualMachineInstancetypeSpecRevision to complete objects being
captured in ControllerRevisions by v1alpah2 this change simply works
around this issue by ignoring the value of APIVersion. This should allow
the conversion to complete correctly until we upgrade these
ControllerRevisions in the future.

[1] kubernetes/client-go#541

Signed-off-by: Lee Yarwood <lyarwood@redhat.com>
lyarwood added a commit to lyarwood/kubevirt that referenced this issue Apr 21, 2023
…PIVersion

As set out in issue kubevirt#9261 there have been reports of older
VirtualMachineInstancetypeSpecRevision objects being stored in
ControllerRevisions without APIVersion set. This is likely due to a
known client-go issue [1] stripping TypeMeta from returned objects.

Given the limited support for v1alpha1 and the fact that we moved away
from VirtualMachineInstancetypeSpecRevision to complete objects being
captured in ControllerRevisions by v1alpah2 this change simply works
around this issue by ignoring the value of APIVersion. This should allow
the conversion to complete correctly until we upgrade these
ControllerRevisions in the future.

[1] kubernetes/client-go#541

Signed-off-by: Lee Yarwood <lyarwood@redhat.com>
0xFelix added a commit to 0xFelix/kubevirt that referenced this issue May 5, 2023
…PIVersion

As set out in issue kubevirt#9261 there have been reports of older
VirtualMachineInstancetypeSpecRevision objects being stored in
ControllerRevisions without APIVersion set. This is likely due to a
known client-go issue [1] stripping TypeMeta from returned objects.

Given the limited support for v1alpha1 and the fact that we moved away
from VirtualMachineInstancetypeSpecRevision to complete objects being
captured in ControllerRevisions by v1alpah2 this change simply works
around this issue by ignoring the value of APIVersion. This should allow
the conversion to complete correctly until we upgrade these
ControllerRevisions in the future.

[1] kubernetes/client-go#541

Conflicts:
  pkg/instancetype/instancetype_test.go

NOTE:
The conflict was due to refactorings that were not applied to this
branch. (e.g. renaming of variables)

Co-authored-by: Felix Matouschek <fmatouschek@redhat.com>
Signed-off-by: Lee Yarwood <lyarwood@redhat.com>
fossedihelm added a commit to fossedihelm/kubevirt that referenced this issue Mar 15, 2024
Generated client common methods does not return the
GroupVersionKind of the resource.
This is caused by kubernetes/client-go#541
and causes the goveralls job to constantly fails.
While working of how to permanently fix it, let's reintroduce the
explicit groupVersionKind set.

Signed-off-by: fossedihelm <ffossemo@redhat.com>
fossedihelm added a commit to fossedihelm/kubevirt that referenced this issue Apr 26, 2024
Generated client common methods does not return the
GroupVersionKind of the resource.
This is caused by kubernetes/client-go#541
and causes the goveralls job to constantly fails.
While working of how to permanently fix it, let's reintroduce the
explicit groupVersionKind set.

Signed-off-by: fossedihelm <ffossemo@redhat.com>
(cherry picked from commit ae56ae3)
fossedihelm added a commit to fossedihelm/kubevirt that referenced this issue Apr 30, 2024
Generated client common methods does not return the
GroupVersionKind of the resource.
This is caused by kubernetes/client-go#541
and causes the goveralls job to constantly fails.
While working of how to permanently fix it, let's reintroduce the
explicit groupVersionKind set.

Signed-off-by: fossedihelm <ffossemo@redhat.com>
(cherry picked from commit ae56ae3)
@caozhuozi
Copy link

@liggitt
is this a good design if we consider polymorphism?
if we use generics or other abstract techniques for API objects in our code, we don't know what the exact go structs are.

@caozhuozi
Copy link

caozhuozi commented May 12, 2024

For those who stumbled upon this post. A quick and easy solution maybe:

There is a util function GVKForObject incontoller-runtime package that can automatically infer the GVK info based on scheme: https://github.com/kubernetes-sigs/controller-runtime/blob/v0.11.0/pkg/client/apiutil/apimachinery.go#L76.

Or use the low level functions in scheme package of apimachinery like scheme.ObjectKinds(obj) for more complex situations.

petr-muller added a commit to petr-muller/oc that referenced this issue May 18, 2024
Previous code used `GroupVersionKind` method on actual resources to determine group/kind strings for them, but client-go unfortunately drops `TypeMeta` on resources processed by typed client (kubernetes/client-go#1328, kubernetes/client-go#541). Fortunately we know what types we build insights for, so we can use appropriate strings from client code for groups and hardcode kinds ourselves.
openshift-cherrypick-robot pushed a commit to openshift-cherrypick-robot/oc that referenced this issue Jun 4, 2024
Previous code used `GroupVersionKind` method on actual resources to determine group/kind strings for them, but client-go unfortunately drops `TypeMeta` on resources processed by typed client (kubernetes/client-go#1328, kubernetes/client-go#541). Fortunately we know what types we build insights for, so we can use appropriate strings from client code for groups and hardcode kinds ourselves.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale.
Projects
None yet
Development

No branches or pull requests