Skip to content

Commit 9904ade

Browse files
committed
- Add unit tests(Ginkgo) for setting values of burst,qps
- Revert variable name to BROKER_K8S prefix
1 parent 8fa4942 commit 9904ade

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

pkg/syncer/broker/config.go

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,14 @@ type brokerSpecification struct {
3131
APIServer string
3232
APIServerToken string
3333
RemoteNamespace string
34-
Insecure bool `default:"false"`
34+
Insecure bool `default:"false"`
3535
Ca string
3636
Secret string
37+
QPS float32 `default:"5"`
38+
Burst int `default:"10"`
3739
}
3840

39-
// clientSpecification defines the client throttling configuration.
40-
// These values are read from environment variables with the prefix "CLIENT_K8S_".
41-
//
42-
// Environment variables:
43-
// - CLIENT_K8S_QPS: Maximum queries per second to the API server (default: 5)
44-
// - CLIENT_K8S_BURST: Maximum burst for throttle to the API server (default: 10)
45-
type clientSpecification struct {
46-
QPS float32 `default:"5"`
47-
Burst int `default:"10"`
48-
}
49-
50-
const (
51-
brokerConfigPrefix = "broker_k8s"
52-
clientConfigPrefix = "submariner_client_k8s"
53-
)
41+
const brokerConfigPrefix = "broker_k8s"
5442

5543
func getBrokerSpecification() (*brokerSpecification, error) {
5644
brokerSpec := brokerSpecification{}
@@ -63,17 +51,6 @@ func getBrokerSpecification() (*brokerSpecification, error) {
6351
return &brokerSpec, nil
6452
}
6553

66-
func getClientSpecification() (*clientSpecification, error) {
67-
clientSpec := clientSpecification{}
68-
69-
err := envconfig.Process(clientConfigPrefix, &clientSpec)
70-
if err != nil {
71-
return nil, errors.Wrap(err, "error processing client env configuration")
72-
}
73-
74-
return &clientSpec, nil
75-
}
76-
7754
func EnvironmentVariable(setting string) string {
7855
// Check the setting is known (ignoring case)
7956
s := reflect.ValueOf(&brokerSpecification{})

pkg/syncer/broker/syncer.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,18 @@ func (c *SyncerConfig) ensureClients() error {
362362
var err error
363363

364364
// Get client QPS/Burst settings from environment variables
365-
clientSpec, err := getClientSpecification()
365+
brokerSpec, err := getBrokerSpecification()
366366
if err != nil {
367367
return err
368368
}
369369

370370
// Apply QPS and Burst settings to local REST config
371-
if c.LocalRestConfig != nil && clientSpec.QPS > 0 {
372-
c.LocalRestConfig.QPS = clientSpec.QPS
371+
if c.LocalRestConfig != nil && brokerSpec.QPS > 0 {
372+
c.LocalRestConfig.QPS = brokerSpec.QPS
373373
}
374374

375-
if c.LocalRestConfig != nil && clientSpec.Burst > 0 {
376-
c.LocalRestConfig.Burst = clientSpec.Burst
375+
if c.LocalRestConfig != nil && brokerSpec.Burst > 0 {
376+
c.LocalRestConfig.Burst = brokerSpec.Burst
377377
}
378378

379379
if c.RestMapper == nil {
@@ -397,12 +397,12 @@ func (c *SyncerConfig) ensureClients() error {
397397
}
398398

399399
// Apply QPS and Burst settings to broker REST config
400-
if c.BrokerRestConfig != nil && clientSpec.QPS > 0 {
401-
c.BrokerRestConfig.QPS = clientSpec.QPS
400+
if c.BrokerRestConfig != nil && brokerSpec.QPS > 0 {
401+
c.BrokerRestConfig.QPS = brokerSpec.QPS
402402
}
403403

404-
if c.BrokerRestConfig != nil && clientSpec.Burst > 0 {
405-
c.BrokerRestConfig.Burst = clientSpec.Burst
404+
if c.BrokerRestConfig != nil && brokerSpec.Burst > 0 {
405+
c.BrokerRestConfig.Burst = brokerSpec.Burst
406406
}
407407

408408
return nil

pkg/syncer/broker/syncer_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ var _ = Describe("Broker Syncer", func() {
7474
os.Unsetenv("BROKER_K8S_REMOTENAMESPACE")
7575
os.Unsetenv("BROKER_K8S_INSECURE")
7676
os.Unsetenv("BROKER_K8S_SECRET")
77+
os.Unsetenv("BROKER_K8S_QPS")
78+
os.Unsetenv("BROKER_K8S_BURST")
7779

7880
expectInitError = false
7981
actualBrokerRestConfig = nil
@@ -688,4 +690,48 @@ var _ = Describe("Broker Syncer", func() {
688690
})
689691
})
690692
})
693+
694+
When("client QPS/Burst environment vars are specified", func() {
695+
BeforeEach(func() {
696+
os.Setenv("BROKER_K8S_QPS", "100")
697+
os.Setenv("BROKER_K8S_BURST", "500")
698+
699+
config.LocalRestConfig = &rest.Config{
700+
Host: "https://local",
701+
}
702+
703+
config.BrokerRestConfig = &rest.Config{
704+
Host: "https://broker",
705+
}
706+
})
707+
708+
It("should apply QPS and Burst to both local and broker rest configs", func() {
709+
Expect(config.LocalRestConfig.QPS).To(Equal(float32(100)))
710+
Expect(config.LocalRestConfig.Burst).To(Equal(500))
711+
Expect(actualBrokerRestConfig.QPS).To(Equal(float32(100)))
712+
Expect(actualBrokerRestConfig.Burst).To(Equal(500))
713+
})
714+
})
715+
716+
When("client QPS/Burst environment vars are not specified", func() {
717+
BeforeEach(func() {
718+
os.Unsetenv("BROKER_K8S_QPS")
719+
os.Unsetenv("BROKER_K8S_BURST")
720+
721+
config.LocalRestConfig = &rest.Config{
722+
Host: "https://local",
723+
}
724+
725+
config.BrokerRestConfig = &rest.Config{
726+
Host: "https://broker",
727+
}
728+
})
729+
730+
It("should apply default QPS (5) and Burst (10) to both local and broker rest configs", func() {
731+
Expect(config.LocalRestConfig.QPS).To(Equal(float32(5)))
732+
Expect(config.LocalRestConfig.Burst).To(Equal(10))
733+
Expect(actualBrokerRestConfig.QPS).To(Equal(float32(5)))
734+
Expect(actualBrokerRestConfig.Burst).To(Equal(10))
735+
})
736+
})
691737
})

0 commit comments

Comments
 (0)