-
Notifications
You must be signed in to change notification settings - Fork 62
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
add: caching for Discovery Client #682
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package discovery | ||
|
||
import ( | ||
"k8s.io/klog/v2" | ||
"time" | ||
|
||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/discovery" | ||
"k8s.io/client-go/discovery/cached/memory" | ||
) | ||
|
||
var GlobalCachedDiscoveryClient discovery.CachedDiscoveryInterface | ||
|
||
func InitializeGlobalDiscoveryClient(config *rest.Config) error { | ||
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config) | ||
if err != nil { | ||
return err | ||
} | ||
GlobalCachedDiscoveryClient = memory.NewMemCacheClient(discoveryClient) | ||
|
||
go startDiscoveryRefreshTicker() | ||
|
||
return nil | ||
} | ||
|
||
func RefreshDiscoveryCache() { | ||
klog.Infof("Invalidating discovery cache") | ||
GlobalCachedDiscoveryClient.Invalidate() | ||
} | ||
|
||
func startDiscoveryRefreshTicker() { | ||
ticker := time.NewTicker(5 * time.Hour) | ||
for { | ||
select { | ||
case <-ticker.C: | ||
RefreshDiscoveryCache() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,6 +18,8 @@ package app | |||||
|
||||||
import ( | ||||||
"net/http" | ||||||
"k8s.io/klog/v2" | ||||||
"github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/discovery" | ||||||
"strings" | ||||||
|
||||||
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" | ||||||
|
@@ -62,6 +64,12 @@ func Run(opt *options.ServerOption) error { | |||||
AgentConfigs: strings.Split(opt.AgentConfigs, ","), | ||||||
} | ||||||
|
||||||
if err = discovery.InitializeGlobalDiscoveryClient(restConfig); err != nil { | ||||||
klog.Errorf("Error initializing global discovery client: %s", err) | ||||||
} else { | ||||||
klog.Infof("Initializing global discovery client") | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think adding the log level here makes sense as this is informational, so can be filtered out at other log levels.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey Fiona, The refresh timeframe set to 5 hours is an arbitrary number, I am aiming for a timeframe which both promotes the efficiency of the cache, whist not holding cache for too long, therefor I chose to half that of the controller-runtime resync period of 10 hours. Although I am open for discussion if you have any suggestions towards the timeframe :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just thinking about it, and how it would work. From what I understand the client only initializes once, and then refreshes after 5 hours. If resources are created soon after a refresh, will they then only be available for manipulation after the next refresh? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you query for an object type which doesn't exist in the cache it would cause a cache miss and invalidation triggering queries to the k8 discovery client to repopulate the cache. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks Kevin. So if a new object of an object type that does exist is created, this will be discoverable? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but the discovery client cache doesn't cache individual k8 objects. As I understand it, the Discovery Client is the piece of k8 which resolves names such as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for explaining! |
||||||
} | ||||||
|
||||||
jobctrl := queuejob.NewJobController(restConfig, mcadConfig, extConfig) | ||||||
if jobctrl == nil { | ||||||
return nil | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should have some error handling or logging in case any errors arise when attempting to invalidate the discovery cache? WDYT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invalidate
doesn't return anything so I don't think this is possibleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha, you're right! Thanks Kevin.