diff --git a/pkg/client/factory.go b/pkg/client/factory.go index ddaa3ca7..098a3b49 100644 --- a/pkg/client/factory.go +++ b/pkg/client/factory.go @@ -15,6 +15,13 @@ import ( "k8s.io/client-go/rest" ) +const ( + // defaultQPS and defaultBurst are used to configure the rest.Config for + // factory created clients. + defaultQPS float32 = 10000 + defaultBurst int = 100 +) + type Factory struct { impersonate bool tableClientCfg *rest.Config @@ -47,10 +54,41 @@ func (a *addQuery) WrappedRoundTripper() http.RoundTripper { return a.next } -func NewFactory(cfg *rest.Config, impersonate bool) (*Factory, error) { +type factoryOptions struct { + qps float32 + burst int +} + +func defaultFactoryOptions() *factoryOptions { + return &factoryOptions{ + qps: defaultQPS, + burst: defaultBurst, + } +} + +// WithQPSAndBurst configures the rest.Config used for creating the clients in +// the factory with the provided burst and qps configuration. +// +// See https://pkg.go.dev/k8s.io/client-go/rest#Config for more. +func WithQPSAndBurst(qps float32, burst int) FactoryOption { + return func(opts *factoryOptions) { + opts.qps = qps + opts.burst = burst + } +} + +// FactoryOption is an option-func for configuring the newly created factory. +type FactoryOption func(*factoryOptions) + +func NewFactory(cfg *rest.Config, impersonate bool, opts ...FactoryOption) (*Factory, error) { clientCfg := rest.CopyConfig(cfg) - clientCfg.QPS = 10000 - clientCfg.Burst = 100 + options := defaultFactoryOptions() + for _, opt := range opts { + opt(options) + } + + clientCfg.QPS = options.qps + clientCfg.Burst = options.burst watchClientCfg := rest.CopyConfig(clientCfg) watchClientCfg.Timeout = 30 * time.Minute diff --git a/pkg/client/factory_test.go b/pkg/client/factory_test.go new file mode 100644 index 00000000..8de7bf75 --- /dev/null +++ b/pkg/client/factory_test.go @@ -0,0 +1,32 @@ +package client + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "k8s.io/client-go/rest" +) + +func TestWithQPSAndBurst(t *testing.T) { + fo := defaultFactoryOptions() + + assert.Equal(t, defaultQPS, fo.qps) + assert.Equal(t, defaultBurst, fo.burst) + + WithQPSAndBurst(50, 20)(fo) + + assert.Equal(t, float32(50.0), fo.qps) + assert.Equal(t, 20, fo.burst) +} + +func TestNewFactoryWithOptions(t *testing.T) { + cfg := &rest.Config{ + QPS: 5.0, + Burst: 10, + } + f, err := NewFactory(cfg, false, WithQPSAndBurst(50, 20)) + assert.NoError(t, err) + + assert.Equal(t, float32(50.0), f.clientCfg.QPS) + assert.Equal(t, 20, f.clientCfg.Burst) +}