Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass-through strategy plugin #433

Merged
merged 6 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions plugins/builtin/strategy/pass-thru/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad-autoscaler/plugins"
passthru "github.com/hashicorp/nomad-autoscaler/plugins/builtin/strategy/pass-thru/plugin"
)

func main() {
plugins.Serve(factory)
}

// factory returns a new instance of the PassThru Strategy plugin.
func factory(log hclog.Logger) interface{} {
return passthru.NewPassThruPlugin(log)
}
97 changes: 97 additions & 0 deletions plugins/builtin/strategy/pass-thru/plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package plugin

import (
"fmt"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad-autoscaler/plugins"
"github.com/hashicorp/nomad-autoscaler/plugins/base"
"github.com/hashicorp/nomad-autoscaler/plugins/strategy"
"github.com/hashicorp/nomad-autoscaler/sdk"
)

const (
// pluginName is the unique name of the this plugin amongst strategy
// plugins.
pluginName = "pass-thru"
)

var (
PluginID = plugins.PluginID{
Name: pluginName,
PluginType: sdk.PluginTypeStrategy,
}

PluginConfig = &plugins.InternalPluginConfig{
Factory: func(l hclog.Logger) interface{} { return NewPassThruPlugin(l) },
}

pluginInfo = &base.PluginInfo{
Name: pluginName,
PluginType: sdk.PluginTypeStrategy,
}
)

// Assert that StrategyPlugin meets the strategy.Strategy interface.
var _ strategy.Strategy = (*StrategyPlugin)(nil)

// StrategyPlugin is the None implementation of the strategy.Strategy
// interface.
type StrategyPlugin struct {
logger hclog.Logger
}

// NewTargetValuePlugin returns the None implementation of the
// strategy.Strategy interface.
func NewPassThruPlugin(log hclog.Logger) strategy.Strategy {
return &StrategyPlugin{
logger: log,
}
}

// SetConfig satisfies the SetConfig function on the base.Base interface.
func (s *StrategyPlugin) SetConfig(config map[string]string) error {
return nil
}

// PluginInfo satisfies the PluginInfo function on the base.Base interface.
func (s *StrategyPlugin) PluginInfo() (*base.PluginInfo, error) {
return pluginInfo, nil
}

// Run satisfies the Run function on the strategy.Strategy interface.
func (s *StrategyPlugin) Run(eval *sdk.ScalingCheckEvaluation, count int64) (*sdk.ScalingCheckEvaluation, error) {

// Use only the latest value for now.
metric := eval.Metrics[len(eval.Metrics)-1]


// TODO: compare count > metric --> direction
// Identify the direction of scaling, if any.
eval.Action.Direction = s.calculateDirection(count, metric.Value)
if eval.Action.Direction == sdk.ScaleDirectionNone {
return eval, nil
}


eval.Action.Count = int64(metric.Value)
eval.Action.Reason = fmt.Sprintf("scaling %s because metric is %d", eval.Action.Direction, eval.Action.Count)

return eval, nil
}

// calculateDirection is used to calculate the direction of scaling that should
// occur, if any at all. It takes into account the current task group count in
// order to correctly account for 0 counts.
//
// The input factor value is padded by e, such that no action will be taken if
// factor is within [1-e; 1+e].
func (s *StrategyPlugin) calculateDirection(count int64, metric float64) sdk.ScaleDirection {
if metric > float64(count) {
return sdk.ScaleDirectionUp
} else if metric < float64(count) {
return sdk.ScaleDirectionDown
} else {
return sdk.ScaleDirectionNone
}
}
Loading