Skip to content

Commit ee8c3fb

Browse files
committed
add: caching for Discovery Client
1 parent 280e750 commit ee8c3fb

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package discovery
2+
3+
import (
4+
"k8s.io/klog/v2"
5+
"time"
6+
7+
"k8s.io/client-go/rest"
8+
"k8s.io/client-go/discovery"
9+
"k8s.io/client-go/discovery/cached/memory"
10+
)
11+
12+
var GlobalCachedDiscoveryClient discovery.CachedDiscoveryInterface
13+
14+
func InitializeGlobalDiscoveryClient(config *rest.Config) error {
15+
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
16+
if err != nil {
17+
return err
18+
}
19+
GlobalCachedDiscoveryClient = memory.NewMemCacheClient(discoveryClient)
20+
21+
go startDiscoveryRefreshTicker()
22+
23+
return nil
24+
}
25+
26+
func RefreshDiscoveryCache() {
27+
klog.Infof("Invalidating discovery cache")
28+
GlobalCachedDiscoveryClient.Invalidate()
29+
}
30+
31+
func startDiscoveryRefreshTicker() {
32+
ticker := time.NewTicker(5 * time.Hour)
33+
for {
34+
select {
35+
case <-ticker.C:
36+
RefreshDiscoveryCache()
37+
}
38+
}
39+
}

cmd/kar-controllers/app/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package app
1818

1919
import (
2020
"net/http"
21+
"k8s.io/klog/v2"
22+
"github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/discovery"
2123
"strings"
2224

2325
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
@@ -62,6 +64,12 @@ func Run(opt *options.ServerOption) error {
6264
AgentConfigs: strings.Split(opt.AgentConfigs, ","),
6365
}
6466

67+
if err = discovery.InitializeGlobalDiscoveryClient(restConfig); err != nil {
68+
klog.Errorf("Error initializing global discovery client: %s", err)
69+
} else {
70+
klog.Infof("Initializing global discovery client")
71+
}
72+
6573
jobctrl := queuejob.NewJobController(restConfig, mcadConfig, extConfig)
6674
if jobctrl == nil {
6775
return nil

pkg/controller/queuejobresources/genericresource/genericresource.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import (
2424
"runtime/debug"
2525
"strings"
2626
"time"
27-
27+
28+
discoveryCache "github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/discovery"
2829
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
2930
v1 "k8s.io/api/core/v1"
3031
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
@@ -86,8 +87,8 @@ func (gr *GenericResources) Cleanup(aw *arbv1.AppWrapper, awr *arbv1.AppWrapperG
8687
name := ""
8788

8889
namespaced := true
89-
// todo:DELETEME dd := common.KubeClient.Discovery()
90-
dd := gr.clients.Discovery()
90+
91+
dd := discoveryCache.GlobalCachedDiscoveryClient
9192
apigroups, err := restmapper.GetAPIGroupResources(dd)
9293
if err != nil {
9394
klog.Errorf("[Cleanup] Error getting API resources, err=%#v", err)
@@ -206,8 +207,7 @@ func (gr *GenericResources) SyncQueueJob(aw *arbv1.AppWrapper, awr *arbv1.AppWra
206207
}()
207208

208209
namespaced := true
209-
// todo:DELETEME dd := common.KubeClient.Discovery()
210-
dd := gr.clients.Discovery()
210+
dd := discoveryCache.GlobalCachedDiscoveryClient
211211
apigroups, err := restmapper.GetAPIGroupResources(dd)
212212
if err != nil {
213213
klog.Errorf("Error getting API resources, err=%#v", err)
@@ -624,7 +624,7 @@ func getContainerResources(container v1.Container, replicas float64) *clustersta
624624

625625
// returns status of an item present in etcd
626626
func (gr *GenericResources) IsItemCompleted(awgr *arbv1.AppWrapperGenericResource, namespace string, appwrapperName string, genericItemName string) (completed bool) {
627-
dd := gr.clients.Discovery()
627+
dd := discoveryCache.GlobalCachedDiscoveryClient
628628
apigroups, err := restmapper.GetAPIGroupResources(dd)
629629
if err != nil {
630630
klog.Errorf("[IsItemCompleted] Error getting API resources, err=%#v", err)

0 commit comments

Comments
 (0)