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

client-go do not cache apis which can get from /apis , but actually do not support #1401

Open
WeiYongqiang55 opened this issue Jan 21, 2025 · 1 comment

Comments

@WeiYongqiang55
Copy link

when I use helm(v3.13.3)and client-go( 0.28.4)
when somehow get snapshot.storage.k8s.io/v1 from /apis ,
but when call xxx.snapshot.storage.k8s.io/v1 , k8s return 404

in this case , client-go may call the /apis and other API Server interfaces multiple times, leading to performance degradation, which is unnecessary.

and the reason is that:
client-go cache_discovery.go , when get servergroup details, and k8s return 404, then client-go will not write this 404 servergfroup resourcelist to cachefile

discovery/cached/disk/cached_discovery.go:70

@WeiYongqiang55
Copy link
Author

func (d *CachedDiscoveryClient) ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error) {
filename := filepath.Join(d.cacheDirectory, groupVersion, "serverresources.json")
cachedBytes, err := d.getCachedFile(filename)
// don't fail on errors, we either don't have a file or won't be able to run the cached check. Either way we can fallback.
if err == nil {
cachedResources := &metav1.APIResourceList{}
if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), cachedBytes, cachedResources); err == nil {
klog.V(10).Infof("returning cached discovery info from %v", filename)
return cachedResources, nil
}
}

liveResources, err := d.delegate.ServerResourcesForGroupVersion(groupVersion)
if err != nil {
           // will go here
	klog.V(3).Infof("skipped caching discovery info due to %v", err)
	return liveResources, err
}
if liveResources == nil || len(liveResources.APIResources) == 0 {
	klog.V(3).Infof("skipped caching discovery info, no resources found")
	return liveResources, err
}

if err := d.writeCachedFile(filename, liveResources); err != nil {
	klog.V(1).Infof("failed to write cache to %v due to %v", filename, err)
}

return liveResources, nil

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant