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) }