Skip to content

Commit 3695f13

Browse files
func: set all the checks functions and types as private, remove the check for nil policy
1 parent 5b0e999 commit 3695f13

File tree

7 files changed

+28
-32
lines changed

7 files changed

+28
-32
lines changed

policy/check.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type checkRunner struct {
4141
}
4242

4343
// NewCheckHandler returns a new checkHandler instance.
44-
func NewCheckRunner(config *CheckRunnerConfig, c *sdk.ScalingPolicyCheck) *checkRunner {
44+
func newCheckRunner(config *CheckRunnerConfig, c *sdk.ScalingPolicyCheck) *checkRunner {
4545
return &checkRunner{
4646
log: config.Log,
4747
check: c,
@@ -131,7 +131,7 @@ func (ch *checkRunner) runStrategy(ctx context.Context, currentCount int64, ms s
131131
return *runResp.Action, nil
132132
}
133133

134-
// QueryMetrics wraps the apm.Query call to provide operational functionality.
134+
// queryMetrics wraps the apm.Query call to provide operational functionality.
135135
func (ch *checkRunner) queryMetrics(ctx context.Context) (sdk.TimestampedMetrics, error) {
136136
ch.log.Debug("querying source", "query", ch.check.Query, "source", ch.check.Source)
137137

@@ -177,11 +177,11 @@ func (ch *checkRunner) queryMetrics(ctx context.Context) (sdk.TimestampedMetrics
177177
return ms, nil
178178
}
179179

180-
func (ch *checkRunner) Group() string {
180+
func (ch *checkRunner) group() string {
181181
return ch.check.Group
182182
}
183183

184-
func (ch *checkRunner) RunCheckAndCapCount(ctx context.Context, currentCount int64) (sdk.ScalingAction, error) {
184+
func (ch *checkRunner) runCheckAndCapCount(ctx context.Context, currentCount int64) (sdk.ScalingAction, error) {
185185
ch.log.Debug("received policy check for evaluation")
186186

187187
metrics, err := ch.queryMetrics(ctx)

policy/check_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func TestCheckHandler_getNewCountFromMetrics(t *testing.T) {
148148
},
149149
}
150150

151-
runner := NewCheckRunner(&CheckRunnerConfig{
151+
runner := newCheckRunner(&CheckRunnerConfig{
152152
Log: hclog.NewNullLogger(),
153153
StrategyRunner: sr,
154154
Policy: tt.policy,
@@ -226,7 +226,7 @@ func TestCheckHandler_runAPMQuery(t *testing.T) {
226226
},
227227
}
228228

229-
handler := NewCheckRunner(&CheckRunnerConfig{
229+
handler := newCheckRunner(&CheckRunnerConfig{
230230
Log: hclog.NewNullLogger(),
231231
MetricsGetter: mockLooker,
232232
Policy: &sdk.ScalingPolicy{

policy/handler.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ type limiter interface {
6161
}
6262

6363
type checker interface {
64-
RunCheckAndCapCount(ctx context.Context, currentCount int64) (sdk.ScalingAction, error)
65-
Group() string
64+
runCheckAndCapCount(ctx context.Context, currentCount int64) (sdk.ScalingAction, error)
65+
group() string
6666
}
6767

6868
// Handler monitors a policy for changes and controls when them are sent for
@@ -107,39 +107,40 @@ type Handler struct {
107107
}
108108

109109
type HandlerConfig struct {
110-
UpdatesChan chan *sdk.ScalingPolicy
111-
ErrChan chan<- error
112-
Policy *sdk.ScalingPolicy
113-
Log hclog.Logger
114-
TargetController targetpkg.Controller
115-
Limiter *Limiter
116-
DependencyGetter dependencyGetter
110+
UpdatesChan chan *sdk.ScalingPolicy
111+
ErrChan chan<- error
112+
Policy *sdk.ScalingPolicy
113+
Log hclog.Logger
114+
TargetController targetpkg.Controller
115+
Limiter *Limiter
116+
DependencyGetter dependencyGetter
117+
118+
// Ent only field
117119
HistoricalAPMGetter HistoricalAPMGetter
118120
EvaluateAfter time.Duration
119121
}
120122

121123
func NewPolicyHandler(config HandlerConfig) (*Handler, error) {
122-
123124
h := &Handler{
124125
log: config.Log,
125126
mutators: []Mutator{
126127
NomadAPMMutator{},
127128
},
128129
pm: config.DependencyGetter,
129-
historicalAPMGetter: config.HistoricalAPMGetter,
130-
evaluateAfter: config.EvaluateAfter,
131130
targetController: config.TargetController,
132131
updatesCh: config.UpdatesChan,
133132
policy: config.Policy,
134133
errChn: config.ErrChan,
135134
limiter: config.Limiter,
136135
stateLock: sync.RWMutex{},
137136
state: StateIdle,
137+
historicalAPMGetter: config.HistoricalAPMGetter,
138+
evaluateAfter: config.EvaluateAfter,
138139
}
139140

140141
err := h.loadCheckRunners()
141142
if err != nil {
142-
return nil, fmt.Errorf("unable to load the checks for the handler: %w", err)
143+
return nil, fmt.Errorf("failed to load check handlers: %w", err)
143144
}
144145

145146
currentStatus, err := h.runTargetStatus()
@@ -174,7 +175,8 @@ func NewPolicyHandler(config HandlerConfig) (*Handler, error) {
174175
return h, nil
175176
}
176177

177-
// Convert the last event string.
178+
// checkForOutOfBandEvents tries to determine if there has been any recent
179+
// scaling events in case of the autoscaler going offline.
178180
func checkForOutOfBandEvents(status *sdk.TargetStatus) (int64, error) {
179181
// If the target status includes a last event meta key, check for cooldown
180182
// due to out-of-band events. This is also useful if the Autoscaler has
@@ -208,7 +210,7 @@ func (h *Handler) loadCheckRunners() error {
208210
return fmt.Errorf("failed to get APM for strategy %s: %w", check.Strategy.Name, err)
209211
}
210212

211-
runner := NewCheckRunner(&CheckRunnerConfig{
213+
runner := newCheckRunner(&CheckRunnerConfig{
212214
Log: h.log.Named("check_handler").With("check", check.Name,
213215
"source", check.Source, "strategy", check.Strategy.Name),
214216
StrategyRunner: s,
@@ -437,13 +439,13 @@ func (h *Handler) calculateNewCount(ctx context.Context, currentCount int64) (sd
437439
checkGroups := make(map[string][]checkResult)
438440

439441
for _, ch := range h.checkRunners {
440-
action, err := ch.RunCheckAndCapCount(ctx, currentCount)
442+
action, err := ch.runCheckAndCapCount(ctx, currentCount)
441443
if err != nil {
442444
return sdk.ScalingAction{}, fmt.Errorf("failed get count from metrics: %v", err)
443445

444446
}
445447

446-
g := ch.Group()
448+
g := ch.group()
447449
checkGroups[g] = append(checkGroups[g], checkResult{
448450
action: &action,
449451
handler: ch,

policy/handler_oss.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type noopVerticalCheckRunner struct {
2020
policy *sdk.ScalingPolicy
2121
}
2222

23-
func (nv *noopVerticalCheckRunner) RunCheckAndCapCount(_ context.Context, currentCount int64) (sdk.ScalingAction, error) {
23+
func (nv *noopVerticalCheckRunner) runCheckAndCapCount(_ context.Context, currentCount int64) (sdk.ScalingAction, error) {
2424
a := sdk.ScalingAction{
2525
Direction: sdk.ScaleDirectionNone,
2626
Count: currentCount,
@@ -31,7 +31,7 @@ func (nv *noopVerticalCheckRunner) RunCheckAndCapCount(_ context.Context, curren
3131
return a, nil
3232
}
3333

34-
func (nv *noopVerticalCheckRunner) Group() string {
34+
func (nv *noopVerticalCheckRunner) group() string {
3535
return ""
3636
}
3737

policy/handler_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ func Test_pickWinnerActionFromGroups(t *testing.T) {
290290
must.Eq(t, tt.wantAction.Direction, result.action.Direction)
291291
must.Eq(t, tt.wantAction.Count, result.action.Count)
292292
must.NotNil(t, result.handler)
293-
//must.Eq(t, tt.wantHandler.check.Name, result.handler.check.Name)
294293
}
295294
})
296295
}

sdk/policy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type ScalingPolicy struct {
8787
// Validate applies validation rules that are independent of policy source.
8888
func (p *ScalingPolicy) Validate() error {
8989
if p == nil {
90-
return errors.New("empty policy")
90+
return nil
9191
}
9292

9393
var result *multierror.Error

sdk/policy_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ func TestScalingPolicy_Validate(t *testing.T) {
1616
policy *ScalingPolicy
1717
expectedError string
1818
}{
19-
{
20-
name: "nil policy",
21-
policy: nil,
22-
expectedError: "empty policy",
23-
},
2419
{
2520
name: "no type",
2621
policy: &ScalingPolicy{

0 commit comments

Comments
 (0)