From 81d4b4712acdd341cee5ad57d55094b54f6a9d75 Mon Sep 17 00:00:00 2001 From: Chenzhao Huang <949412843@qq.com> Date: Sun, 14 Jan 2024 10:27:28 -0500 Subject: [PATCH] feat: improve configuration of concurrent daemon pod updater workers (#1907) Signed-off-by: Chenzhao Huang <949412843@qq.com> --- .../app/options/daemonpodupdatercontroller.go | 63 +++++++++++++++++++ cmd/yurt-manager/app/options/options.go | 7 +++ .../controller/apis/config/types.go | 4 ++ .../daemonpodupdater/config/types.go | 22 +++++++ .../daemon_pod_updater_controller.go | 13 +--- 5 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 cmd/yurt-manager/app/options/daemonpodupdatercontroller.go create mode 100644 pkg/yurtmanager/controller/daemonpodupdater/config/types.go diff --git a/cmd/yurt-manager/app/options/daemonpodupdatercontroller.go b/cmd/yurt-manager/app/options/daemonpodupdatercontroller.go new file mode 100644 index 00000000000..f4e088f1305 --- /dev/null +++ b/cmd/yurt-manager/app/options/daemonpodupdatercontroller.go @@ -0,0 +1,63 @@ +/* +Copyright 2024 The OpenYurt Authors. + +Licensed under the Apache License, Version 2.0 (the License); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an AS IS BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package options + +import ( + "github.com/spf13/pflag" + + "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/daemonpodupdater/config" +) + +type DaemonPodUpdaterControllerOptions struct { + *config.DaemonPodUpdaterControllerConfiguration +} + +func NewDaemonPodUpdaterControllerOptions() *DaemonPodUpdaterControllerOptions { + return &DaemonPodUpdaterControllerOptions{ + &config.DaemonPodUpdaterControllerConfiguration{ + ConcurrentDaemonPodUpdaterWorkers: 3, + }, + } +} + +// AddFlags adds flags related to nodePool for yurt-manager to the specified FlagSet. +func (n *DaemonPodUpdaterControllerOptions) AddFlags(fs *pflag.FlagSet) { + if n == nil { + return + } + + fs.Int32Var(&n.ConcurrentDaemonPodUpdaterWorkers, "concurrent-daemonpod-updater-workers", n.ConcurrentDaemonPodUpdaterWorkers, "The number of daemonpodupdater objects that are allowed to reconcile concurrently. Larger number = more responsive daemonpodupdaters, but more CPU (and network) load") +} + +// ApplyTo fills up nodePool config with options. +func (o *DaemonPodUpdaterControllerOptions) ApplyTo(cfg *config.DaemonPodUpdaterControllerConfiguration) error { + if o == nil { + return nil + } + + cfg.ConcurrentDaemonPodUpdaterWorkers = o.ConcurrentDaemonPodUpdaterWorkers + return nil +} + +// Validate checks validation of DaemonPodUpdaterControllerOptions. +func (o *DaemonPodUpdaterControllerOptions) Validate() []error { + if o == nil { + return nil + } + errs := []error{} + return errs +} diff --git a/cmd/yurt-manager/app/options/options.go b/cmd/yurt-manager/app/options/options.go index 4b1eab48bf1..4471eeddbd1 100644 --- a/cmd/yurt-manager/app/options/options.go +++ b/cmd/yurt-manager/app/options/options.go @@ -26,6 +26,7 @@ import ( // YurtManagerOptions is the main context object for the yurt-manager. type YurtManagerOptions struct { Generic *GenericOptions + DaemonPodUpdaterController *DaemonPodUpdaterControllerOptions CsrApproverController *CsrApproverControllerOptions NodePoolController *NodePoolControllerOptions GatewayPickupController *GatewayPickupControllerOptions @@ -42,6 +43,7 @@ func NewYurtManagerOptions() (*YurtManagerOptions, error) { s := YurtManagerOptions{ Generic: NewGenericOptions(), + DaemonPodUpdaterController: NewDaemonPodUpdaterControllerOptions(), CsrApproverController: NewCsrApproverControllerOptions(), NodePoolController: NewNodePoolControllerOptions(), GatewayPickupController: NewGatewayPickupControllerOptions(), @@ -59,6 +61,7 @@ func NewYurtManagerOptions() (*YurtManagerOptions, error) { func (y *YurtManagerOptions) Flags(allControllers, disabledByDefaultControllers []string) cliflag.NamedFlagSets { fss := cliflag.NamedFlagSets{} y.Generic.AddFlags(fss.FlagSet("generic"), allControllers, disabledByDefaultControllers) + y.DaemonPodUpdaterController.AddFlags(fss.FlagSet("daemonpodupdater controller")) y.CsrApproverController.AddFlags(fss.FlagSet("csrapprover controller")) y.NodePoolController.AddFlags(fss.FlagSet("nodepool controller")) y.GatewayPickupController.AddFlags(fss.FlagSet("gateway controller")) @@ -75,6 +78,7 @@ func (y *YurtManagerOptions) Flags(allControllers, disabledByDefaultControllers func (y *YurtManagerOptions) Validate(allControllers []string, controllerAliases map[string]string) error { var errs []error errs = append(errs, y.Generic.Validate(allControllers, controllerAliases)...) + errs = append(errs, y.DaemonPodUpdaterController.Validate()...) errs = append(errs, y.CsrApproverController.Validate()...) errs = append(errs, y.NodePoolController.Validate()...) errs = append(errs, y.GatewayPickupController.Validate()...) @@ -91,6 +95,9 @@ func (y *YurtManagerOptions) ApplyTo(c *config.Config, controllerAliases map[str if err := y.Generic.ApplyTo(&c.ComponentConfig.Generic, controllerAliases); err != nil { return err } + if err := y.DaemonPodUpdaterController.ApplyTo(&c.ComponentConfig.DaemonPodUpdaterController); err != nil { + return err + } if err := y.CsrApproverController.ApplyTo(&c.ComponentConfig.CsrApproverController); err != nil { return err } diff --git a/pkg/yurtmanager/controller/apis/config/types.go b/pkg/yurtmanager/controller/apis/config/types.go index b55289d0f5d..2d8809f3896 100644 --- a/pkg/yurtmanager/controller/apis/config/types.go +++ b/pkg/yurtmanager/controller/apis/config/types.go @@ -22,6 +22,7 @@ import ( "k8s.io/kube-controller-manager/config/v1alpha1" csrapproverconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/csrapprover/config" + daemonpodupdaterconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/daemonpodupdater/config" nodepoolconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/nodepool/config" platformadminconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/platformadmin/config" gatewaypickupconfig "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/raven/gatewaypickup/config" @@ -36,6 +37,9 @@ type YurtManagerConfiguration struct { metav1.TypeMeta Generic GenericConfiguration + // DaemonPodUpdaterControllerConfiguration holds configuration for DaemonPodUpdaterController related features. + DaemonPodUpdaterController daemonpodupdaterconfig.DaemonPodUpdaterControllerConfiguration + // CsrApproverControllerConfiguration holds configuration for CsrApproverController related features. CsrApproverController csrapproverconfig.CsrApproverControllerConfiguration diff --git a/pkg/yurtmanager/controller/daemonpodupdater/config/types.go b/pkg/yurtmanager/controller/daemonpodupdater/config/types.go new file mode 100644 index 00000000000..67a0f159766 --- /dev/null +++ b/pkg/yurtmanager/controller/daemonpodupdater/config/types.go @@ -0,0 +1,22 @@ +/* +Copyright 2024 The OpenYurt Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package config + +// DaemonPodUpdaterControllerConfiguration contains elements describing DaemonPodUpdaterController. +type DaemonPodUpdaterControllerConfiguration struct { + ConcurrentDaemonPodUpdaterWorkers int32 +} diff --git a/pkg/yurtmanager/controller/daemonpodupdater/daemon_pod_updater_controller.go b/pkg/yurtmanager/controller/daemonpodupdater/daemon_pod_updater_controller.go index bb04650cc59..f6ab8c43d20 100644 --- a/pkg/yurtmanager/controller/daemonpodupdater/daemon_pod_updater_controller.go +++ b/pkg/yurtmanager/controller/daemonpodupdater/daemon_pod_updater_controller.go @@ -19,7 +19,6 @@ package daemonpodupdater import ( "context" - "flag" "fmt" "strings" "sync" @@ -55,13 +54,7 @@ import ( podutil "github.com/openyurtio/openyurt/pkg/yurtmanager/controller/util/pod" ) -func init() { - flag.IntVar(&concurrentReconciles, "daemonpodupdater-workers", concurrentReconciles, "Max concurrent workers for Daemonpodupdater controller.") -} - var ( - concurrentReconciles = 3 - // controllerKind contains the schema.GroupVersionKind for this controller type. controllerKind = appsv1.SchemeGroupVersion.WithKind("DaemonSet") ) @@ -103,7 +96,7 @@ func Format(format string, args ...interface{}) string { // and Start it when the Manager is Started. func Add(ctx context.Context, c *appconfig.CompletedConfig, mgr manager.Manager) error { klog.Infof("daemonupdater-controller add controller %s", controllerKind.String()) - return add(mgr, newReconciler(c, mgr)) + return add(mgr, c, newReconciler(c, mgr)) } var _ reconcile.Reconciler = &ReconcileDaemonpodupdater{} @@ -140,9 +133,9 @@ func (r *ReconcileDaemonpodupdater) InjectConfig(cfg *rest.Config) error { } // add adds a new Controller to mgr with r as the reconcile.Reconciler -func add(mgr manager.Manager, r reconcile.Reconciler) error { +func add(mgr manager.Manager, cfg *appconfig.CompletedConfig, r reconcile.Reconciler) error { // Create a new controller - c, err := controller.New(names.DaemonPodUpdaterController, mgr, controller.Options{Reconciler: r, MaxConcurrentReconciles: concurrentReconciles}) + c, err := controller.New(names.DaemonPodUpdaterController, mgr, controller.Options{Reconciler: r, MaxConcurrentReconciles: int(cfg.Config.ComponentConfig.DaemonPodUpdaterController.ConcurrentDaemonPodUpdaterWorkers)}) if err != nil { return err }