From 6f534125a7f2454e5f2d3e04a5d0904e49a8104a Mon Sep 17 00:00:00 2001 From: DangPeng Liu Date: Mon, 20 Jun 2022 16:22:24 +0800 Subject: [PATCH] update default qps for webhook (#141) Signed-off-by: ldpliu --- pkg/cmd/webhook/webhook.go | 57 ++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 9 deletions(-) diff --git a/pkg/cmd/webhook/webhook.go b/pkg/cmd/webhook/webhook.go index 39e26a3ad..aea565458 100644 --- a/pkg/cmd/webhook/webhook.go +++ b/pkg/cmd/webhook/webhook.go @@ -5,27 +5,27 @@ import ( admissionserver "github.com/openshift/generic-admission-server/pkg/cmd/server" "github.com/spf13/cobra" + "github.com/spf13/pflag" genericapiserver "k8s.io/apiserver/pkg/server" utilfeature "k8s.io/apiserver/pkg/util/feature" "open-cluster-management.io/work/pkg/webhook" ) func NewAdmissionHook() *cobra.Command { - o := admissionserver.NewAdmissionServerOptions(os.Stdout, os.Stderr, &webhook.ManifestWorkAdmissionHook{}) - + ops := NewOptions() cmd := &cobra.Command{ Use: "webhook", Short: "Start Managed Cluster Admission Server", RunE: func(c *cobra.Command, args []string) error { stopCh := genericapiserver.SetupSignalHandler() - if err := o.Complete(); err != nil { + if err := ops.ServerOptions.Complete(); err != nil { return err } - if err := o.Validate(args); err != nil { + if err := ops.ServerOptions.Validate(args); err != nil { return err } - if err := o.RunAdmissionServer(stopCh); err != nil { + if err := ops.RunAdmissionServer(ops.ServerOptions, stopCh); err != nil { return err } return nil @@ -33,11 +33,50 @@ func NewAdmissionHook() *cobra.Command { } flags := cmd.Flags() + ops.AddFlags(flags) + return cmd +} + +// Config contains the server (the webhook) cert and key. +type Options struct { + QPS float32 + Burst int + ServerOptions *admissionserver.AdmissionServerOptions +} + +// NewOptions constructs a new set of default options for webhook. +func NewOptions() *Options { + return &Options{ + QPS: 100.0, + Burst: 200, + ServerOptions: admissionserver.NewAdmissionServerOptions(os.Stdout, os.Stderr, &webhook.ManifestWorkAdmissionHook{}), + } +} + +func (c *Options) AddFlags(fs *pflag.FlagSet) { + fs.Float32Var(&c.QPS, "max-qps", c.QPS, + "Maximum QPS to the hub server from this webhook.") + fs.IntVar(&c.Burst, "max-burst", c.Burst, + "Maximum burst for throttle.") + featureGate := utilfeature.DefaultMutableFeatureGate - featureGate.AddFlag(flags) - o.RecommendedOptions.FeatureGate = featureGate + featureGate.AddFlag(fs) - o.RecommendedOptions.AddFlags(cmd.Flags()) + c.ServerOptions.RecommendedOptions.FeatureGate = featureGate + c.ServerOptions.RecommendedOptions.AddFlags(fs) +} - return cmd +// change the default QPS and Butst, so rewrite this func +func (c *Options) RunAdmissionServer(o *admissionserver.AdmissionServerOptions, stopCh <-chan struct{}) error { + config, err := o.Config() + if err != nil { + return err + } + config.RestConfig.QPS = c.QPS + config.RestConfig.Burst = c.Burst + server, err := config.Complete().New() + if err != nil { + return err + } + return server.GenericAPIServer.PrepareRun().Run(stopCh) }