Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions pkg/kubernetes/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"os"
"strconv"
"strings"

"github.com/containers/kubernetes-mcp-server/pkg/config"
Expand Down Expand Up @@ -96,6 +98,10 @@ func newManager(config *config.StaticConfig, restConfig *rest.Config, clientCmdC
if clientCmdConfig == nil {
return nil, errors.New("clientCmdConfig cannot be nil")
}

// Apply QPS and Burst from environment variables if set (primarily for testing)
applyRateLimitFromEnv(restConfig)

k8s := &Manager{
staticConfig: config,
}
Expand Down Expand Up @@ -225,3 +231,21 @@ func (m *Manager) Derived(ctx context.Context) (*Kubernetes, error) {
}
return &Kubernetes{derived}, nil
}

// applyRateLimitFromEnv applies QPS and Burst rate limits from environment variables if set.
// This is primarily useful for tests to avoid client-side rate limiting.
// Environment variables:
// - KUBE_CLIENT_QPS: Sets the QPS (queries per second) limit
// - KUBE_CLIENT_BURST: Sets the burst limit
func applyRateLimitFromEnv(cfg *rest.Config) {
if qpsStr := os.Getenv("KUBE_CLIENT_QPS"); qpsStr != "" {
if qps, err := strconv.ParseFloat(qpsStr, 32); err == nil {
cfg.QPS = float32(qps)
}
}
if burstStr := os.Getenv("KUBE_CLIENT_BURST"); burstStr != "" {
if burst, err := strconv.Atoi(burstStr); err == nil {
cfg.Burst = burst
}
}
}
3 changes: 3 additions & 0 deletions pkg/mcp/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func TestMain(m *testing.M) {
_ = os.Setenv("KUBECONFIG", "/dev/null") // Avoid interference from existing kubeconfig
_ = os.Setenv("KUBERNETES_SERVICE_HOST", "") // Avoid interference from in-cluster config
_ = os.Setenv("KUBERNETES_SERVICE_PORT", "") // Avoid interference from in-cluster config
// Set high rate limits to avoid client-side throttling in tests
_ = os.Setenv("KUBE_CLIENT_QPS", "1000")
_ = os.Setenv("KUBE_CLIENT_BURST", "2000")
envTestDir, err := store.DefaultStoreDir()
if err != nil {
panic(err)
Expand Down