kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
8.99k stars 2.94k forks source link

`list` returns objects that don't exist #1251

Closed rexagod closed 1 year ago

rexagod commented 1 year ago

PTAL at the code snippet below. Currently, if there's CRD installed that has three version defined under one group for one kind, then if we try to list the resources under a GVK combination that does not have any resource under it, it will still fetch resources from the GVK combination (with the versions that do have resources under them) and return that for every such query made.

So effectively, a GV(no resource)K query, will yield all GV(any resource if G and K are the same as the other GVK)K resources.

Snippet to reproduce the issue. ```go package main import ( "context" "os" "github.com/davecgh/go-spew/spew" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/dynamic" "k8s.io/client-go/tools/clientcmd" ) func main() { kubeconfigPath, _ := os.LookupEnv("KUBECONFIG") config, _ := clientcmd.BuildConfigFromFlags("", kubeconfigPath) dynamicClient, _ := dynamic.NewForConfig(config) list, _ := dynamicClient.Resource(schema.GroupVersionResource{ Group: ``, Version: ``, Resource: ``, }).List(context.Background(), metav1.ListOptions{}) spew.Dump(list) } ```
rexagod commented 1 year ago

Does the internal logic respect the version priority, even on specifying a certain version?

liggitt commented 1 year ago

This is working as intended. All instances of a group/resource (regardless of version) are accessible via all versions served for that group/resource. The API server converts instances into the requested version.

That means if I create a v1 instance, and you list v1beta1, you will see the object I created, converted into the v1beta1 representation.

/close

k8s-ci-robot commented 1 year ago

@liggitt: Closing this issue.

In response to [this](https://github.com/kubernetes/client-go/issues/1251#issuecomment-1544083071): >This is working as intended. All instances of a group/resource (regardless of version) are accessible via all versions served for that group/resource. The API server converts instances into the requested version. > >That means if I create a v1 instance, and you list v1beta1, you will see the object I created, converted into the v1beta1 representation. > >/close Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md). If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.