From 2b5f7f15fe74f0ae73729430ae3b0db852b78a2a Mon Sep 17 00:00:00 2001 From: Dharmit Shah Date: Fri, 1 Jul 2022 17:10:37 +0530 Subject: [PATCH 1/3] Use cached discovery client A cached discovery client refers its cache under `~/.kube/cache` instead of checking with the cluster every time. Signed-off-by: Dharmit Shah --- pkg/kclient/kclient.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/kclient/kclient.go b/pkg/kclient/kclient.go index 0fb8004114f..a5210fce719 100644 --- a/pkg/kclient/kclient.go +++ b/pkg/kclient/kclient.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" + "k8s.io/cli-runtime/pkg/genericclioptions" + "github.com/blang/semver" kerrors "k8s.io/apimachinery/pkg/api/errors" @@ -153,7 +155,8 @@ func NewForConfig(config clientcmd.ClientConfig) (client *Client, err error) { return nil, err } - client.discoveryClient, err = discovery.NewDiscoveryClientForConfig(client.KubeClientConfig) + config_flags := genericclioptions.NewConfigFlags(true) + client.discoveryClient, err = config_flags.ToDiscoveryClient() if err != nil { return nil, err } From 6414968f7eec130e872abeac5d40ef82c5eed840 Mon Sep 17 00:00:00 2001 From: Dharmit Shah Date: Mon, 4 Jul 2022 12:32:30 +0530 Subject: [PATCH 2/3] Have both cached and regular discovery clients Signed-off-by: Dharmit Shah --- pkg/kclient/all.go | 2 +- pkg/kclient/kclient.go | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/kclient/all.go b/pkg/kclient/all.go index 669fc91cf08..6f57286f45d 100644 --- a/pkg/kclient/all.go +++ b/pkg/kclient/all.go @@ -18,7 +18,7 @@ import ( // Code into this file is heavily inspired from https://github.com/ahmetb/kubectl-tree func (c *Client) GetAllResourcesFromSelector(selector string, ns string) ([]unstructured.Unstructured, error) { - apis, err := findAPIs(c.discoveryClient) + apis, err := findAPIs(c.cachedDiscoveryClient) if err != nil { return nil, err } diff --git a/pkg/kclient/kclient.go b/pkg/kclient/kclient.go index a5210fce719..5e205293be5 100644 --- a/pkg/kclient/kclient.go +++ b/pkg/kclient/kclient.go @@ -53,9 +53,10 @@ type Client struct { // DynamicClient interacts with client-go's `dynamic` package. It is used // to dynamically create service from an operator. It can take an arbitrary // yaml and create k8s/OpenShift resource from it. - DynamicClient dynamic.Interface - discoveryClient discovery.DiscoveryInterface - restmapper *restmapper.DeferredDiscoveryRESTMapper + DynamicClient dynamic.Interface + discoveryClient discovery.DiscoveryInterface + cachedDiscoveryClient discovery.CachedDiscoveryInterface + restmapper *restmapper.DeferredDiscoveryRESTMapper supportedResources map[string]bool // Is server side apply supported by cluster @@ -155,8 +156,13 @@ func NewForConfig(config clientcmd.ClientConfig) (client *Client, err error) { return nil, err } + client.discoveryClient, err = discovery.NewDiscoveryClientForConfig(client.KubeClientConfig) + if err != nil { + return nil, err + } + config_flags := genericclioptions.NewConfigFlags(true) - client.discoveryClient, err = config_flags.ToDiscoveryClient() + client.cachedDiscoveryClient, err = config_flags.ToDiscoveryClient() if err != nil { return nil, err } From 5edd7a02f0343631f9f6438128c9b88526a3a93f Mon Sep 17 00:00:00 2001 From: Dharmit Shah Date: Wed, 6 Jul 2022 15:37:33 +0530 Subject: [PATCH 3/3] Docs for odo list's use of cache --- docs/website/docs/command-reference/list.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/website/docs/command-reference/list.md b/docs/website/docs/command-reference/list.md index f26bb959b90..e2017c1fffa 100644 --- a/docs/website/docs/command-reference/list.md +++ b/docs/website/docs/command-reference/list.md @@ -18,3 +18,18 @@ defined in the local Devfile, * `--namespace` - Namespace to list the components from (optional). By default, the current namespace defined in kubeconfig is used * `-o json` - Outputs the list in JSON format. See [JSON output](json-output.md) for more information + +:::tip use of cache + +`odo list` makes use of cache for performance reasons. This is the same cache that is referred by `kubectl` command +when you do `kubectl api-resources --cached=true`. As a result, if you were to install an Operator/CRD on the +Kubernetes cluster, and create a resource from it using odo, you might not see it in the `odo list` output. This +would be the case for 10 minutes timeframe for which the cache is considered valid. Beyond this 10 minutes, the +cache is updated anyway. + +If you would like to invalidate the cache before the 10 minutes timeframe, you could manually delete it by doing: +```shell +rm -rf ~/.kube/cache/discovery/api.crc.testing_6443/ +``` +Above example shows how to invalidate the cache for a CRC cluster. Note that you will have to modify the `api.crc. +testing_6443` part based on the cluster you are working against. \ No newline at end of file