Skip to content

Commit 3efc5f3

Browse files
authored
feat(manager): apply QPS and Burst rate limits from environment variables for testing (#513)
Signed-off-by: Marc Nuri <marc@marcnuri.com>
1 parent 3b206c9 commit 3efc5f3

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

pkg/kubernetes/manager.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"os"
8+
"strconv"
79
"strings"
810

911
"github.com/containers/kubernetes-mcp-server/pkg/config"
@@ -96,6 +98,10 @@ func newManager(config *config.StaticConfig, restConfig *rest.Config, clientCmdC
9698
if clientCmdConfig == nil {
9799
return nil, errors.New("clientCmdConfig cannot be nil")
98100
}
101+
102+
// Apply QPS and Burst from environment variables if set (primarily for testing)
103+
applyRateLimitFromEnv(restConfig)
104+
99105
k8s := &Manager{
100106
staticConfig: config,
101107
}
@@ -225,3 +231,21 @@ func (m *Manager) Derived(ctx context.Context) (*Kubernetes, error) {
225231
}
226232
return &Kubernetes{derived}, nil
227233
}
234+
235+
// applyRateLimitFromEnv applies QPS and Burst rate limits from environment variables if set.
236+
// This is primarily useful for tests to avoid client-side rate limiting.
237+
// Environment variables:
238+
// - KUBE_CLIENT_QPS: Sets the QPS (queries per second) limit
239+
// - KUBE_CLIENT_BURST: Sets the burst limit
240+
func applyRateLimitFromEnv(cfg *rest.Config) {
241+
if qpsStr := os.Getenv("KUBE_CLIENT_QPS"); qpsStr != "" {
242+
if qps, err := strconv.ParseFloat(qpsStr, 32); err == nil {
243+
cfg.QPS = float32(qps)
244+
}
245+
}
246+
if burstStr := os.Getenv("KUBE_CLIENT_BURST"); burstStr != "" {
247+
if burst, err := strconv.Atoi(burstStr); err == nil {
248+
cfg.Burst = burst
249+
}
250+
}
251+
}

pkg/mcp/common_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ func TestMain(m *testing.M) {
4646
_ = os.Setenv("KUBECONFIG", "/dev/null") // Avoid interference from existing kubeconfig
4747
_ = os.Setenv("KUBERNETES_SERVICE_HOST", "") // Avoid interference from in-cluster config
4848
_ = os.Setenv("KUBERNETES_SERVICE_PORT", "") // Avoid interference from in-cluster config
49+
// Set high rate limits to avoid client-side throttling in tests
50+
_ = os.Setenv("KUBE_CLIENT_QPS", "1000")
51+
_ = os.Setenv("KUBE_CLIENT_BURST", "2000")
4952
envTestDir, err := store.DefaultStoreDir()
5053
if err != nil {
5154
panic(err)

0 commit comments

Comments
 (0)