Skip to content

Commit

Permalink
add: caching for Discovery Client
Browse files Browse the repository at this point in the history
  • Loading branch information
VanillaSpoon committed Nov 7, 2023
1 parent 280e750 commit ee8c3fb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
39 changes: 39 additions & 0 deletions cmd/kar-controllers/app/discovery/discovery.go
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()
}
}
}
8 changes: 8 additions & 0 deletions cmd/kar-controllers/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}

jobctrl := queuejob.NewJobController(restConfig, mcadConfig, extConfig)
if jobctrl == nil {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (
"runtime/debug"
"strings"
"time"


discoveryCache "github.com/project-codeflare/multi-cluster-app-dispatcher/cmd/kar-controllers/app/discovery"
arbv1 "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/apis/controller/v1beta1"
v1 "k8s.io/api/core/v1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
Expand Down Expand Up @@ -86,8 +87,8 @@ func (gr *GenericResources) Cleanup(aw *arbv1.AppWrapper, awr *arbv1.AppWrapperG
name := ""

namespaced := true
// todo:DELETEME dd := common.KubeClient.Discovery()
dd := gr.clients.Discovery()

dd := discoveryCache.GlobalCachedDiscoveryClient
apigroups, err := restmapper.GetAPIGroupResources(dd)
if err != nil {
klog.Errorf("[Cleanup] Error getting API resources, err=%#v", err)
Expand Down Expand Up @@ -206,8 +207,7 @@ func (gr *GenericResources) SyncQueueJob(aw *arbv1.AppWrapper, awr *arbv1.AppWra
}()

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

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

0 comments on commit ee8c3fb

Please sign in to comment.