Skip to content

Commit

Permalink
perf: use podLister to optimize k8s calls
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Sep 28, 2020
1 parent 61d552f commit 9554845
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/daemon/cniserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
kubeInformerFactory.Start(stopCh)
kubeovnInformerFactory.Start(stopCh)
go ctl.Run(stopCh)
go daemon.RunServer(config)
go daemon.RunServer(config, ctl)
if err := mvCNIConf(); err != nil {
klog.Fatalf("failed to mv cni conf, %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ type cniServerHandler struct {
Config *Configuration
KubeClient kubernetes.Interface
KubeOvnClient clientset.Interface
Controller *Controller
}

func createCniServerHandler(config *Configuration) *cniServerHandler {
csh := &cniServerHandler{KubeClient: config.KubeClient, KubeOvnClient: config.KubeOvnClient, Config: config}
func createCniServerHandler(config *Configuration, controller *Controller) *cniServerHandler {
csh := &cniServerHandler{KubeClient: config.KubeClient, KubeOvnClient: config.KubeOvnClient, Config: config, Controller: controller}
return csh
}

Expand All @@ -46,7 +47,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
var pod *v1.Pod
var err error
for i := 0; i < 15; i++ {
pod, err = csh.KubeClient.CoreV1().Pods(podRequest.PodNamespace).Get(podRequest.PodName, metav1.GetOptions{})
pod, err = csh.Controller.podsLister.Pods(podRequest.PodNamespace).Get(podRequest.PodName)
if err != nil {
errMsg := fmt.Errorf("get pod %s/%s failed %v", podRequest.PodNamespace, podRequest.PodName, err)
klog.Error(errMsg)
Expand Down Expand Up @@ -177,7 +178,7 @@ func (csh cniServerHandler) handleDel(req *restful.Request, resp *restful.Respon
}
return
}
pod, err := csh.KubeClient.CoreV1().Pods(podRequest.PodNamespace).Get(podRequest.PodName, metav1.GetOptions{})
pod, err := csh.Controller.podsLister.Pods(podRequest.PodNamespace).Get(podRequest.PodName)
if err != nil && !k8serrors.IsNotFound(err) {
errMsg := fmt.Errorf("parse del request failed %v", err)
klog.Error(errMsg)
Expand Down
4 changes: 2 additions & 2 deletions pkg/daemon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var requestLogString = "[%s] Incoming %s %s %s request"
var responseLogString = "[%s] Outgoing response %s %s with %d status code in %vms"

// RunServer runs the cniserver
func RunServer(config *Configuration) {
func RunServer(config *Configuration, controller *Controller) {
nodeName = config.NodeName
csh := createCniServerHandler(config)
csh := createCniServerHandler(config, controller)
server := http.Server{
Handler: createHandler(csh),
}
Expand Down

0 comments on commit 9554845

Please sign in to comment.