kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
9.12k stars 2.95k forks source link

unable to expand resource shortcut for resource provided by extension apiserver #1365

Open Diaphteiros opened 4 months ago

Diaphteiros commented 4 months ago

I'm trying to write a kubectl-style CLI tool for improving my ops activities. I'm working with a k8s cluster that runs the Gardener extension apiserver, which - among others - provides the shoots resource (api group core.gardener.cloud, version v1beta1).

I create a new shortcut expander by using NewShortcutExpander and then call KindFor(schema.GroupVersionResource{Resource: myResource}) to resolve the shortcut in myResource. For resources provided by CRDs, the shortcut resolution works fine, but if I try to resolve shoot, I eventually end up here.

At this point, the resource variable contains what I initially put into the KindFor method, which looks like this:

{
  Group: ""
  Version: ""
  Resource: "shoot"
}

The content of the apiRes variable in the loop looks like this

{
  Name: "shoots"
  SingularName: "shoot"
  Namespaced: true
  Group: ""
  Version: ""
  Kind: "Shoot"
}

while the gv variable of the loop holds the correct group/version information:

{
  Group: "core.gardener.cloud"
  Version: "v1beta1"
}

However, in the linked code line, resource is returned, which contains only the resource name and no group or version. This is then fed into the rest mapper, which will never succeed with an empty group.

What I'm confused about is that in the mentioned code line, all required information is there. Shouldn't this part look like this instead?

if resource.Resource == apiRes.Name || resource.Resource == apiRes.SingularName {
  resource.Group = gv.Group
  resource.Version = gv.Version
  return resource
}

Is it ever correct/useful to return the unmodified value of resource?

Or am I using the shortcut expander wrongly here?

What I'm effectively trying to do is to resolve shoot from a CLI like myCLI get shoot foo -n bar to Group: core.gardener.cloud, Version: v1beta1, Kind: Shoot.

k8s-triage-robot commented 1 month ago

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

This bot triages un-triaged issues according to the following rules:

You can:

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

/lifecycle stale

k8s-triage-robot commented 2 weeks ago

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

This bot triages un-triaged issues according to the following rules:

You can:

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

/lifecycle rotten