Skip to content

Commit

Permalink
refactor the ApplyTo method of Options
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Apr 10, 2024
1 parent c2c0533 commit d36659a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
48 changes: 24 additions & 24 deletions cmd/controller/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion pkg/controller/annotator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/dynamic/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/dynamic/policyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d36659a

Please sign in to comment.