From c30934add25d8e5f501c3464b4da4d389d7763fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Thu, 13 Apr 2023 07:01:11 +0200 Subject: [PATCH] fix: make flag default values configurable (#6883) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- cmd/internal/flag.go | 46 ++++++++++++++++++++++++++++------ cmd/reports-controller/main.go | 6 ++++- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/cmd/internal/flag.go b/cmd/internal/flag.go index ab31fb40f8..73cc888b68 100644 --- a/cmd/internal/flag.go +++ b/cmd/internal/flag.go @@ -70,10 +70,10 @@ func initMetricsFlags() { flag.BoolVar(&disableMetricsExport, "disableMetrics", false, "Set this flag to 'true' to disable metrics.") } -func initKubeconfigFlags() { +func initKubeconfigFlags(qps float64, burst int) { flag.StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.") - flag.Float64Var(&clientRateLimitQPS, "clientRateLimitQPS", 20, "Configure the maximum QPS to the Kubernetes API server from Kyverno. Uses the client default if zero.") - flag.IntVar(&clientRateLimitBurst, "clientRateLimitBurst", 50, "Configure the maximum burst for throttle. Uses the client default if zero.") + flag.Float64Var(&clientRateLimitQPS, "clientRateLimitQPS", qps, "Configure the maximum QPS to the Kubernetes API server from Kyverno. Uses the client default if zero.") + flag.IntVar(&clientRateLimitBurst, "clientRateLimitBurst", burst, "Configure the maximum burst for throttle. Uses the client default if zero.") } func initPolicyExceptionsFlags() { @@ -98,7 +98,39 @@ func initLeaderElectionFlags() { flag.DurationVar(&leaderElectionRetryPeriod, "leaderElectionRetryPeriod", leaderelection.DefaultRetryPeriod, "Configure leader election retry period.") } -func InitFlags(config Configuration) { +type options struct { + clientRateLimitQPS float64 + clientRateLimitBurst int +} + +func newOptions() options { + return options{ + clientRateLimitQPS: 20, + clientRateLimitBurst: 50, + } +} + +type Option = func(*options) + +func WithDefaultQps(qps float64) Option { + return func(o *options) { + o.clientRateLimitQPS = qps + } +} + +func WithDefaultBurst(burst int) Option { + return func(o *options) { + o.clientRateLimitBurst = burst + } +} + +func initFlags(config Configuration, opts ...Option) { + options := newOptions() + for _, o := range opts { + if o != nil { + o(&options) + } + } // logging initLoggingFlags() // profiling @@ -115,7 +147,7 @@ func InitFlags(config Configuration) { } // kubeconfig if config.UsesKubeconfig() { - initKubeconfigFlags() + initKubeconfigFlags(options.clientRateLimitQPS, options.clientRateLimitBurst) } // policy exceptions if config.UsesPolicyExceptions() { @@ -144,8 +176,8 @@ func InitFlags(config Configuration) { } } -func ParseFlags(config Configuration) { - InitFlags(config) +func ParseFlags(config Configuration, opts ...Option) { + initFlags(config, opts...) flag.Parse() } diff --git a/cmd/reports-controller/main.go b/cmd/reports-controller/main.go index eff7216c77..7f3e0c2f9b 100644 --- a/cmd/reports-controller/main.go +++ b/cmd/reports-controller/main.go @@ -188,7 +188,11 @@ func main() { internal.WithFlagSets(flagset), ) // parse flags - internal.ParseFlags(appConfig) + internal.ParseFlags( + appConfig, + internal.WithDefaultQps(300), + internal.WithDefaultBurst(300), + ) // setup ctx, setup, sdown := internal.Setup(appConfig, "kyverno-reports-controller", skipResourceFilters) defer sdown()