From 52f5565dea8441ba21ed3cf9e68ffc40d70d5046 Mon Sep 17 00:00:00 2001 From: shahra Date: Wed, 15 Jul 2020 15:53:17 -0700 Subject: [PATCH] Configurable QPS and Burst for k8s clients. This change also introduces a new client for leader election. --- cmd/csi-resizer/main.go | 28 ++++++++++++++++++++++++++-- pkg/util/util.go | 23 +---------------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/cmd/csi-resizer/main.go b/cmd/csi-resizer/main.go index a655a11cb..0d7740229 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()) } @@ -102,7 +122,11 @@ func main() { run(context.TODO()) } else { lockName := "external-resizer-" + util.SanitizeName(resizerName) - le := leaderelection.NewLeaderElection(kubeClient, lockName, run) + leKubeClient, err := kubernetes.NewForConfig(config) + if err != nil { + klog.Fatal(err.Error()) + } + le := leaderelection.NewLeaderElection(leKubeClient, lockName, run) if *leaderElectionNamespace != "" { le.WithNamespace(*leaderElectionNamespace) 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)