Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Customize the client rate limiters of the discovery package
- Loading branch information
1 parent
a347999
commit 3f2c296
Showing
5 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package restcfg contains utility functions to deal with rest configs. | ||
package restcfg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package restcfg | ||
|
||
import ( | ||
"flag" | ||
|
||
"k8s.io/client-go/rest" | ||
) | ||
|
||
const ( | ||
// DefaultQPS -> The default QPS value assigned to client-go clients. | ||
DefaultQPS = uint(100) | ||
// DefaultBurst -> The default burst value assigned to client-go clients. | ||
DefaultBurst = uint(100) | ||
) | ||
|
||
var ( | ||
qps = DefaultQPS | ||
burst = DefaultBurst | ||
) | ||
|
||
// InitFlags initializes the flags to configure the rate limiter parameters. | ||
func InitFlags(flagset *flag.FlagSet) { | ||
if flagset == nil { | ||
flagset = flag.CommandLine | ||
} | ||
|
||
flagset.UintVar(&qps, "client-qps", qps, "The maximum number of queries per second performed towards the API server.") | ||
flagset.UintVar(&burst, "client-max-burst", qps, "The maximum burst of requests in excess of the rate limit towards the API server.") | ||
} | ||
|
||
// SetRateLimiter configures the rate limiting parameters of the given rest configuration | ||
// to the values obtained from the command line parameters. | ||
func SetRateLimiter(cfg *rest.Config) *rest.Config { | ||
return SetRateLimiterWithCustomParamenters(cfg, float32(qps), int(burst)) | ||
} | ||
|
||
// SetRateLimiterWithCustomParamenters configures the rate limiting parameters of the given rest configuration. | ||
func SetRateLimiterWithCustomParamenters(cfg *rest.Config, qps float32, burst int) *rest.Config { | ||
cfg.QPS = qps | ||
cfg.Burst = burst | ||
return cfg | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package restcfg_test | ||
|
||
import ( | ||
"flag" | ||
"strconv" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
utilruntime "k8s.io/apimachinery/pkg/util/runtime" | ||
"k8s.io/client-go/rest" | ||
|
||
"github.com/liqotech/liqo/pkg/utils/restcfg" | ||
) | ||
|
||
var _ = Describe("The rate limiting utility functions", func() { | ||
|
||
var ( | ||
cfg rest.Config | ||
output *rest.Config | ||
) | ||
|
||
const ( | ||
qps = 67 | ||
burst = 89 | ||
) | ||
|
||
Describe("the SetRateLimiter function", func() { | ||
Context("configuring the rate limiting parameters", func() { | ||
var fs flag.FlagSet | ||
|
||
BeforeEach(func() { | ||
fs = *flag.NewFlagSet("test-flags", flag.PanicOnError) | ||
restcfg.InitFlags(&fs) | ||
}) | ||
JustBeforeEach(func() { output = restcfg.SetRateLimiter(&cfg) }) | ||
|
||
When("using the default configuration", func() { | ||
It("should return a pointer to the original object", func() { Expect(output).To(BeIdenticalTo(&cfg)) }) | ||
It("should set the default QPS value", func() { Expect(cfg.QPS).To(BeNumerically("==", restcfg.DefaultQPS)) }) | ||
It("should set the default burst value", func() { Expect(cfg.Burst).To(BeNumerically("==", restcfg.DefaultBurst)) }) | ||
}) | ||
|
||
When("specifying a custom configuration", func() { | ||
BeforeEach(func() { | ||
utilruntime.Must(fs.Set("client-qps", strconv.FormatInt(qps, 10))) | ||
utilruntime.Must(fs.Set("client-max-burst", strconv.FormatInt(burst, 10))) | ||
}) | ||
|
||
It("should return a pointer to the original object", func() { Expect(output).To(BeIdenticalTo(&cfg)) }) | ||
It("should set the desired QPS value", func() { Expect(cfg.QPS).To(BeNumerically("==", qps)) }) | ||
It("should set the desired burst value", func() { Expect(cfg.Burst).To(BeNumerically("==", burst)) }) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("the SetRateLimiterWithCustomParamenters function", func() { | ||
Context("configuring the rate limiting parameters", func() { | ||
JustBeforeEach(func() { output = restcfg.SetRateLimiterWithCustomParamenters(&cfg, qps, burst) }) | ||
|
||
It("should return a pointer to the original object", func() { Expect(output).To(BeIdenticalTo(&cfg)) }) | ||
It("should set the desired QPS value", func() { Expect(cfg.QPS).To(BeNumerically("==", qps)) }) | ||
It("should set the desired burst value", func() { Expect(cfg.Burst).To(BeNumerically("==", burst)) }) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package restcfg_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestRestcfg(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "RestConfig Suite") | ||
} |