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 when using client.Get() #1517

Closed
kaoet opened this issue May 7, 2021 · 10 comments
Closed

TypeMeta is empty when using client.Get() #1517

kaoet opened this issue May 7, 2021 · 10 comments
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.

Comments

@kaoet
Copy link

kaoet commented May 7, 2021

I use client.New() to build a Client and call its Get() method to get some object. But the returned object has an empty TypeMeta.

package main

import (
	"context"
	"fmt"

	corev1 "k8s.io/api/core/v1"
	"k8s.io/client-go/kubernetes/scheme"
	_ "k8s.io/client-go/plugin/pkg/client/auth"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/config"
)

func main() {
	cli, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme.Scheme})
	if err != nil {
		panic(err)
	}
	ns := &corev1.Namespace{}
	if err := cli.Get(context.Background(), client.ObjectKey{Name: "default"}, ns); err != nil {
		panic(err)
	}
	fmt.Printf("TypeMeta: %+v\n", ns.TypeMeta)
}

Expected output:

TypeMeta: {Kind: Namespace APIVersion: v1}

Actual output:

TypeMeta: {Kind: APIVersion:}
@cndoit18
Copy link

cndoit18 commented May 8, 2021

This is an upstream error, do we need to do a temporary fix?
I can try to complete this thing.
kubernetes/kubernetes#3030
kubernetes/kubernetes#80609

cndoit18 added a commit to cndoit18/controller-runtime that referenced this issue May 12, 2021
Fixes kubernetes-sigs#1517

Signed-off-by: cndoit18 <cndoit18@outlook.com>
@cndoit18
Copy link

@alvaroaleman
Hi, I tried to process it first in controller-runtime and then wait for the upstream to solve
What do you think?

@cndoit18
Copy link

/assign @cndoit18

@alvaroaleman
Copy link
Member

alvaroaleman commented May 12, 2021

tbh I'd prefer to not woraround this downstream and wait for someone to do it properly in upstream. The issue is IMHO not severe enough to warrant yet another downstream workaround, you can get the type info from the scheme. WDYT @vincepri ?

@cndoit18
Copy link

tbh I'd prefer to not woraround this downstream and wait for someone to do it properly in upstream. The issue is IMHO not severe enough to warrant yet another downstream workaround, you can get the type info from the scheme. WDYT @vincepri ?

I agree with this decision. thank you

@cndoit18
Copy link

cndoit18 commented May 20, 2021

You can add GVK in this way

package main

import (
	"context"
	"fmt"

	corev1 "k8s.io/api/core/v1"
	"k8s.io/client-go/kubernetes/scheme"
	_ "k8s.io/client-go/plugin/pkg/client/auth"
	"sigs.k8s.io/controller-runtime/pkg/client"
	"sigs.k8s.io/controller-runtime/pkg/client/config"
)

func main() {
	cli, err := client.New(config.GetConfigOrDie(), client.Options{Scheme: scheme.Scheme})
	if err != nil {
		panic(err)
	}
	ns := &corev1.Namespace{}
	if err := cli.Get(context.Background(), client.ObjectKey{Name: "default"}, ns); err != nil {
		panic(err)
	}

        gvks, unversioned, err := cli.Scheme().ObjectKinds(ns)
	if err != nil {
		panic(err)
	}
	if !unversioned && len(gvks) == 1 {
		ns.SetGroupVersionKind(gvks[0])
	}

	fmt.Printf("TypeMeta: %+v\n", ns.TypeMeta)
}

// output:
// TypeMeta: {Kind:Namespace APIVersion:v1}

@cndoit18
Copy link

tbh I'd prefer to not woraround this downstream and wait for someone to do it properly in upstream. The issue is IMHO not severe enough to warrant yet another downstream workaround, you can get the type info from the scheme. WDYT @vincepri ?

This issue seems to be closed, WDYT @alvaroaleman

@cndoit18 cndoit18 removed their assignment May 24, 2021
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle stale
  • Mark this issue or PR as rotten with /lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/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 Aug 22, 2021
@k8s-triage-robot
Copy link

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues and PRs according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue or PR as fresh with /remove-lifecycle rotten
  • Close this issue or PR with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

@k8s-ci-robot k8s-ci-robot added lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed. and removed lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. labels Sep 21, 2021
@alvaroaleman
Copy link
Member

This issue seems to be closed, WDYT @alvaroaleman

Agree

ack-prow bot pushed a commit to aws-controllers-k8s/runtime that referenced this issue Mar 22, 2023
Description of changes:
Some version of K8s do not reliably return `TypeMeta` information when you call `apiReader.Get()` (see kubernetes/kubernetes#3030 and kubernetes/kubernetes#80609). This is a [known bug](kubernetes-sigs/controller-runtime#1517) in `controller-runtime` that they don't plan on fixing. 

Parts of the code, namely around setting up the user agent, currently rely on these fields - and they are currently being given an empty struct (with empty strings for all values). To work around this bug, this PR has introduced a new `GroupVersionKind()` getter in the `ResourceDescriptor` (replacing the existing `GroupKind`), which returns a static description of the GVK. Then, rather than referencing the `RuntimeObject` `TypeMeta` properties, we can reference this new GVK getter anywhere in the runtime. It also replaces any existing use of `GroupKind` to now use `GroupVersionKind`.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
dtthuynh added a commit to dtthuynh/clusterclaims-controller that referenced this issue Sep 5, 2024
- Upgraded to latest hive api from mce-2.7 branch
- Upgraded to go 1.22
- Changed code due to changes in the behaviour of runtime-controller library
- remove unncessary dependencies from go.mod

See related issues on changes for runtime-controller that needed to be addressed:
- kubernetes-sigs/controller-runtime#2788
- kubernetes-sigs/controller-runtime#1517
dtthuynh added a commit to dtthuynh/clusterclaims-controller that referenced this issue Sep 5, 2024
- Upgraded to latest hive api from mce-2.7 branch
- Upgraded to go 1.22
- Changed code due to changes in the behaviour of runtime-controller library
- remove unncessary dependencies from go.mod

See related issues on changes for runtime-controller that needed to be addressed:
- kubernetes-sigs/controller-runtime#2788
- kubernetes-sigs/controller-runtime#1517
dtthuynh added a commit to dtthuynh/clusterclaims-controller that referenced this issue Sep 5, 2024
- Upgraded to latest hive api from mce-2.7 branch
- Upgraded to go 1.22
- Changed code due to changes in the behaviour of runtime-controller library
- remove unncessary dependencies from go.mod

See related issues on changes for runtime-controller that needed to be addressed:
- kubernetes-sigs/controller-runtime#2788
- kubernetes-sigs/controller-runtime#1517

Signed-off-by: David Huynh <dhuynh@redhat.com>
openshift-merge-bot bot pushed a commit to stolostron/clusterclaims-controller that referenced this issue Sep 5, 2024
- Upgraded to latest hive api from mce-2.7 branch
- Upgraded to go 1.22
- Changed code due to changes in the behaviour of runtime-controller library
- remove unncessary dependencies from go.mod

See related issues on changes for runtime-controller that needed to be addressed:
- kubernetes-sigs/controller-runtime#2788
- kubernetes-sigs/controller-runtime#1517

Signed-off-by: David Huynh <dhuynh@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lifecycle/rotten Denotes an issue or PR that has aged beyond stale and will be auto-closed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants