diff --git a/app/providers/kubernetes.go b/app/providers/kubernetes.go index 82aff918..c106d5e0 100644 --- a/app/providers/kubernetes.go +++ b/app/providers/kubernetes.go @@ -12,6 +12,8 @@ import ( core_v1 "k8s.io/api/core/v1" "github.com/acouvreur/sablier/app/instance" + providerConfig "github.com/acouvreur/sablier/config" + log "github.com/sirupsen/logrus" autoscalingv1 "k8s.io/api/autoscaling/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/informers" @@ -60,12 +62,18 @@ type KubernetesProvider struct { Client kubernetes.Interface } -func NewKubernetesProvider() (*KubernetesProvider, error) { - config, err := rest.InClusterConfig() +func NewKubernetesProvider(providerConfig providerConfig.Kubernetes) (*KubernetesProvider, error) { + kubeclientConfig, err := rest.InClusterConfig() + + kubeclientConfig.QPS = providerConfig.QPS + kubeclientConfig.Burst = providerConfig.Burst + + log.Debug(fmt.Sprintf("Provider configuration: QPS=%v, Burst=%v", kubeclientConfig.QPS, kubeclientConfig.Burst)) + if err != nil { return nil, err } - client, err := kubernetes.NewForConfig(config) + client, err := kubernetes.NewForConfig(kubeclientConfig) if err != nil { return nil, err } diff --git a/app/providers/provider.go b/app/providers/provider.go index 6d9f4bda..eef5edf8 100644 --- a/app/providers/provider.go +++ b/app/providers/provider.go @@ -28,7 +28,7 @@ func NewProvider(config config.Provider) (Provider, error) { case config.Name == "docker": return NewDockerClassicProvider() case config.Name == "kubernetes": - return NewKubernetesProvider() + return NewKubernetesProvider(config.Kubernetes) } return nil, fmt.Errorf("unimplemented provider %s", config.Name) } diff --git a/cmd/root.go b/cmd/root.go index 747887dc..576b75b4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -46,6 +46,10 @@ It provides an integrations with multiple reverse proxies and different loading // Provider flags startCmd.Flags().StringVar(&conf.Provider.Name, "provider.name", "docker", fmt.Sprintf("Provider to use to manage containers %v", config.GetProviders())) viper.BindPFlag("provider.name", startCmd.Flags().Lookup("provider.name")) + startCmd.Flags().Float32Var(&conf.Provider.Kubernetes.QPS, "provider.kubernetes.qps", 5, fmt.Sprintf("QPS limit for K8S API access client-side throttling")) + viper.BindPFlag("provider.kubernetes.qps", startCmd.Flags().Lookup("provider.kubernetes.qps")) + startCmd.Flags().IntVar(&conf.Provider.Kubernetes.Burst, "provider.kubernetes.burst", 10, fmt.Sprintf("Maximum burst for K8S API acees client-side throttling")) + viper.BindPFlag("provider.kubernetes.burst", startCmd.Flags().Lookup("provider.kubernetes.burst")) // Server flags startCmd.Flags().IntVar(&conf.Server.Port, "server.port", 10000, "The server port to use") viper.BindPFlag("server.port", startCmd.Flags().Lookup("server.port")) diff --git a/cmd/root_test.go b/cmd/root_test.go index e3cdc1e7..5b22fe79 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -80,6 +80,8 @@ func TestPrecedence(t *testing.T) { "--configFile", filepath.Join(testDir, "testdata", "config.yml"), "start", "--provider.name", "cli", + "--provider.kubernetes.qps", "256", + "--provider.kubernetes.burst", "512", "--server.port", "3333", "--server.base-path", "/cli/", "--storage.file", "/tmp/cli.json", diff --git a/cmd/testdata/config.env b/cmd/testdata/config.env index 05468ba8..be4bd084 100644 --- a/cmd/testdata/config.env +++ b/cmd/testdata/config.env @@ -1,4 +1,6 @@ PROVIDER_NAME=envvar +PROVIDER_KUBERNETES_QPS=16 +PROVIDER_KUBERNETES_BURST=32 SERVER_PORT=2222 SERVER_BASE_PATH=/envvar/ STORAGE_FILE=/tmp/envvar.json diff --git a/cmd/testdata/config.yml b/cmd/testdata/config.yml index 683ae900..a23bdfcf 100644 --- a/cmd/testdata/config.yml +++ b/cmd/testdata/config.yml @@ -1,5 +1,8 @@ provider: name: configfile + kubernetes: + qps: 64 + burst: 128 server: port: 1111 base-path: /configfile/ diff --git a/cmd/testdata/config_cli_wanted.json b/cmd/testdata/config_cli_wanted.json index 9827aa55..cea8b249 100644 --- a/cmd/testdata/config_cli_wanted.json +++ b/cmd/testdata/config_cli_wanted.json @@ -7,7 +7,11 @@ "File": "/tmp/cli.json" }, "Provider": { - "Name": "cli" + "Name": "cli", + "Kubernetes": { + "QPS": 256, + "Burst": 512 + } }, "Sessions": { "DefaultDuration": 10800000000000, diff --git a/cmd/testdata/config_env_wanted.json b/cmd/testdata/config_env_wanted.json index 12c031e6..3b5afa7a 100644 --- a/cmd/testdata/config_env_wanted.json +++ b/cmd/testdata/config_env_wanted.json @@ -7,7 +7,11 @@ "File": "/tmp/envvar.json" }, "Provider": { - "Name": "envvar" + "Name": "envvar", + "Kubernetes": { + "QPS": 16, + "Burst": 32 + } }, "Sessions": { "DefaultDuration": 7200000000000, diff --git a/cmd/testdata/config_yaml_wanted.json b/cmd/testdata/config_yaml_wanted.json index cec2578a..bfa9eeb4 100644 --- a/cmd/testdata/config_yaml_wanted.json +++ b/cmd/testdata/config_yaml_wanted.json @@ -7,7 +7,11 @@ "File": "/tmp/configfile.json" }, "Provider": { - "Name": "configfile" + "Name": "configfile", + "Kubernetes": { + "QPS": 64, + "Burst": 128 + } }, "Sessions": { "DefaultDuration": 3600000000000, diff --git a/config/provider.go b/config/provider.go index 4ad3eb8e..9d4e4faa 100644 --- a/config/provider.go +++ b/config/provider.go @@ -7,7 +7,15 @@ import ( // Provider holds the provider description // It can be either docker, swarm or kubernetes type Provider struct { - Name string `mapstructure:"NAME" yaml:"provider,omitempty"` + Name string `mapstructure:"NAME" yaml:"provider,omitempty"` + Kubernetes Kubernetes +} + +type Kubernetes struct { + //QPS limit for K8S API access client-side throttle + QPS float32 `mapstructure:"QPS" yaml:"QPS" default:"5"` + //Maximum burst for client-side throttle + Burst int `mapstructure:"BURST" yaml:"Burst" default:"10"` } var providers = []string{"docker", "swarm", "kubernetes"} @@ -15,6 +23,10 @@ var providers = []string{"docker", "swarm", "kubernetes"} func NewProviderConfig() Provider { return Provider{ Name: "docker", + Kubernetes: Kubernetes{ + QPS: 5, + Burst: 10, + }, } }