From e365448d66cced4a75b54173fe149e70ceb35e16 Mon Sep 17 00:00:00 2001
From: shahra <shahra@vmware.com>
Date: Wed, 15 Jul 2020 15:53:17 -0700
Subject: [PATCH] Configurable QPS and Burst for k8s clients

---
 cmd/csi-resizer/main.go | 22 +++++++++++++++++++++-
 pkg/util/util.go        | 23 +----------------------
 2 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/cmd/csi-resizer/main.go b/cmd/csi-resizer/main.go
index a655a11cb..eb8c8a604 100644
--- a/cmd/csi-resizer/main.go
+++ b/cmd/csi-resizer/main.go
@@ -20,6 +20,9 @@ import (
 	"context"
 	"flag"
 	"fmt"
+	"k8s.io/client-go/kubernetes"
+	"k8s.io/client-go/rest"
+	"k8s.io/client-go/tools/clientcmd"
 	"os"
 	"time"
 
@@ -54,6 +57,9 @@ var (
 	metricsAddress = flag.String("metrics-address", "", "The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled.")
 	metricsPath    = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
 
+	kubeAPIQPS   = flag.Float64("kube-api-qps", 5, "QPS to use while communicating with the kubernetes apiserver. Defaults to 5.0.")
+	kubeAPIBurst = flag.Int("kube-api-burst", 10, "Burst to use while communicating with the kubernetes apiserver. Defaults to 10.")
+
 	handleVolumeInUseError = flag.Bool("handle-volume-inuse-error", true, "Flag to turn on/off capability to handle volume in use error in resizer controller. Defaults to true if not set.")
 
 	version = "unknown"
@@ -70,7 +76,21 @@ func main() {
 	}
 	klog.Infof("Version : %s", version)
 
-	kubeClient, err := util.NewK8sClient(*master, *kubeConfig)
+	var config *rest.Config
+	var err error
+	if *master != "" || *kubeConfig != "" {
+		config, err = clientcmd.BuildConfigFromFlags(*master, *kubeConfig)
+	} else {
+		config, err = rest.InClusterConfig()
+	}
+	if err != nil {
+		klog.Fatal(err.Error())
+	}
+
+	config.QPS = float32(*kubeAPIQPS)
+	config.Burst = *kubeAPIBurst
+
+	kubeClient, err := kubernetes.NewForConfig(config)
 	if err != nil {
 		klog.Fatal(err.Error())
 	}
diff --git a/pkg/util/util.go b/pkg/util/util.go
index 76617c499..e3eed0ec9 100644
--- a/pkg/util/util.go
+++ b/pkg/util/util.go
@@ -22,7 +22,7 @@ import (
 	"fmt"
 	"regexp"
 
-	v1 "k8s.io/api/core/v1"
+	"k8s.io/api/core/v1"
 	"k8s.io/apimachinery/pkg/api/meta"
 	"k8s.io/apimachinery/pkg/api/resource"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -30,8 +30,6 @@ import (
 	"k8s.io/apimachinery/pkg/types"
 	"k8s.io/apimachinery/pkg/util/strategicpatch"
 	"k8s.io/client-go/kubernetes"
-	"k8s.io/client-go/rest"
-	"k8s.io/client-go/tools/clientcmd"
 )
 
 var knownResizeConditions = map[v1.PersistentVolumeClaimConditionType]bool{
@@ -39,25 +37,6 @@ var knownResizeConditions = map[v1.PersistentVolumeClaimConditionType]bool{
 	v1.PersistentVolumeClaimFileSystemResizePending: true,
 }
 
-// NewK8sClient is an utility function used to create a kubernetes sdk client.
-func NewK8sClient(master, kubeConfig string) (kubernetes.Interface, error) {
-	var config *rest.Config
-	var err error
-	if master != "" || kubeConfig != "" {
-		config, err = clientcmd.BuildConfigFromFlags(master, kubeConfig)
-	} else {
-		config, err = rest.InClusterConfig()
-	}
-	if err != nil {
-		return nil, fmt.Errorf("failed to create config: %v", err)
-	}
-	kubeClient, err := kubernetes.NewForConfig(config)
-	if err != nil {
-		return nil, fmt.Errorf("failed to create client: %v", err)
-	}
-	return kubeClient, nil
-}
-
 // PVCKey returns an unique key of a PVC object,
 func PVCKey(pvc *v1.PersistentVolumeClaim) string {
 	return fmt.Sprintf("%s/%s", pvc.Namespace, pvc.Name)