From d36659a3eb166877da98ef7ffc38203903d1ba7b Mon Sep 17 00:00:00 2001 From: googs1025 Date: Wed, 10 Apr 2024 20:04:00 +0800 Subject: [PATCH] refactor the ApplyTo method of Options --- cmd/controller/app/options/options.go | 48 +++++++++++++------------- pkg/controller/annotator/controller.go | 2 +- pkg/plugins/dynamic/plugins.go | 6 ++-- pkg/plugins/dynamic/policyfile.go | 4 +-- pkg/utils/utils.go | 2 +- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/cmd/controller/app/options/options.go b/cmd/controller/app/options/options.go index 02e125a..03ca543 100644 --- a/cmd/controller/app/options/options.go +++ b/cmd/controller/app/options/options.go @@ -79,31 +79,13 @@ func (o *Options) Flags(flag *pflag.FlagSet) error { func (o *Options) ApplyTo(c *controllerappconfig.Config) error { c.AnnotatorConfig = o.AnnotatorConfiguration c.LeaderElection = o.LeaderElection - return nil -} - -// Validate validates the options and config before launching Annotator. -func (o *Options) Validate() error { - return nil -} + c.HealthPort = o.healthPort -// Config returns an Annotator config object. -func (o *Options) Config() (*controllerappconfig.Config, error) { var kubeconfig *rest.Config var err error - - if err := o.Validate(); err != nil { - return nil, err - } - - c := &controllerappconfig.Config{} - if err := o.ApplyTo(c); err != nil { - return nil, err - } - c.Policy, err = dynamicscheduler.LoadPolicyFromFile(o.PolicyConfigPath) if err != nil { - return nil, err + return err } if o.kubeconfig == "" { @@ -113,24 +95,42 @@ func (o *Options) Config() (*controllerappconfig.Config, error) { kubeconfig, err = clientcmd.BuildConfigFromFlags(o.master, o.kubeconfig) } if err != nil { - return nil, err + return err } c.KubeClient, err = clientset.NewForConfig(rest.AddUserAgent(kubeconfig, ControllerUserAgent)) if err != nil { - return nil, err + return err } c.LeaderElectionClient = clientset.NewForConfigOrDie(rest.AddUserAgent(kubeconfig, "leader-election")) c.PromClient, err = prometheus.NewPromClient(o.PrometheusAddr) if err != nil { - return nil, err + return err } c.KubeInformerFactory = NewInformerFactory(c.KubeClient, 0) - c.HealthPort = o.healthPort + return nil +} + +// Validate validates the options and config before launching Annotator. +func (o *Options) Validate() error { + return nil +} + +// Config returns an Annotator config object. +func (o *Options) Config() (*controllerappconfig.Config, error) { + + if err := o.Validate(); err != nil { + return nil, err + } + + c := &controllerappconfig.Config{} + if err := o.ApplyTo(c); err != nil { + return nil, err + } return c, nil } diff --git a/pkg/controller/annotator/controller.go b/pkg/controller/annotator/controller.go index 14919dc..2c7566f 100644 --- a/pkg/controller/annotator/controller.go +++ b/pkg/controller/annotator/controller.go @@ -34,7 +34,7 @@ type Controller struct { bindingRecords *BindingRecords } -// NewController returns a Node Annotator object. +// NewNodeAnnotator returns a Node Annotator object. func NewNodeAnnotator( nodeInformer coreinformers.NodeInformer, eventInformer coreinformers.EventInformer, diff --git a/pkg/plugins/dynamic/plugins.go b/pkg/plugins/dynamic/plugins.go index 5faee64..6925b0b 100644 --- a/pkg/plugins/dynamic/plugins.go +++ b/pkg/plugins/dynamic/plugins.go @@ -22,7 +22,7 @@ const ( Name = "Dynamic" ) -// Dynamic-scheduler is a real load-aware scheduler plugin. +// DynamicScheduler is a real load-aware scheduler plugin. type DynamicScheduler struct { handle framework.Handle schedulerPolicy *policy.DynamicSchedulerPolicy @@ -90,7 +90,7 @@ func (ds *DynamicScheduler) Score(ctx context.Context, state *framework.CycleSta score = score - int(hotValue*10) - finalScore := utils.NormalizeScore(int64(score),framework.MaxNodeScore,framework.MinNodeScore) + finalScore := utils.NormalizeScore(int64(score), framework.MaxNodeScore, framework.MinNodeScore) klog.V(4).Infof("[crane] Node[%s]'s final score is %d, while score is %d and hot value is %f", node.Name, finalScore, score, hotValue) @@ -105,7 +105,7 @@ func (ds *DynamicScheduler) ScoreExtensions() framework.ScoreExtensions { func NewDynamicScheduler(plArgs runtime.Object, h framework.Handle) (framework.Plugin, error) { args, ok := plArgs.(*config.DynamicArgs) if !ok { - return nil, fmt.Errorf("want args to be of type DynamicArgs, got %T.", plArgs) + return nil, fmt.Errorf("want args to be of type DynamicArgs, got %T", plArgs) } schedulerPolicy, err := LoadPolicyFromFile(args.PolicyConfigPath) diff --git a/pkg/plugins/dynamic/policyfile.go b/pkg/plugins/dynamic/policyfile.go index 3cc252f..4a9a1ff 100644 --- a/pkg/plugins/dynamic/policyfile.go +++ b/pkg/plugins/dynamic/policyfile.go @@ -2,14 +2,14 @@ package dynamic import ( "fmt" - "io/ioutil" + "os" "github.com/gocrane/crane-scheduler/pkg/plugins/apis/policy" "github.com/gocrane/crane-scheduler/pkg/plugins/apis/policy/scheme" ) func LoadPolicyFromFile(file string) (*policy.DynamicSchedulerPolicy, error) { - data, err := ioutil.ReadFile(file) + data, err := os.ReadFile(file) if err != nil { return nil, err } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 22a15f1..13d70ea 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -54,7 +54,7 @@ func GetSystemNamespace() string { return ns } -// NormalizaScore nornalize the score in range [min, max] +// NormalizeScore nornalize the score in range [min, max] func NormalizeScore(value, max, min int64) int64 { if value < min { value = min