Skip to content

Commit

Permalink
chore: refactor scale_handler.go (#4131)
Browse files Browse the repository at this point in the history
* chore: refactor scale_handler.go

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>

* Update pkg/scaling/scalers_builder.go

Co-authored-by: Jorge Turrado Ferrero <Jorge_turrado@hotmail.es>
Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>
Co-authored-by: Jorge Turrado Ferrero <Jorge_turrado@hotmail.es>
  • Loading branch information
zroubalik and JorTurFer committed Jan 17, 2023
1 parent f70718a commit 22a5111
Show file tree
Hide file tree
Showing 7 changed files with 362 additions and 293 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ issues:
linters:
- gomnd
- dupl
- path: scale_handler.go
- path: scalers_builder.go
linters:
- gocyclo
- path: scale_scaledobjects.go
Expand Down
2 changes: 1 addition & 1 deletion CREATE-NEW-SCALER.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ In order to develop a scaler, a developer should do the following:
2. Create the new scaler struct under the `pkg/scalers` folder.
3. Implement the methods defined in the [scaler interface](#scaler-interface) section.
4. Create a constructor according to [this](#constructor).
5. Change the `buildScaler` function in `pkg/scaling/scale_handler.go` by adding another switch case that matches your scaler. Scalers in the switch are ordered alphabetically, please follow the same pattern.
5. Change the `buildScaler` function in `pkg/scaling/scalers_builder.go` by adding another switch case that matches your scaler. Scalers in the switch are ordered alphabetically, please follow the same pattern.
6. Run `make build` from the root of KEDA and your scaler is ready.

If you want to deploy locally
Expand Down
45 changes: 25 additions & 20 deletions pkg/scaling/cache/scalers_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ScalerBuilder struct {
Factory func() (scalers.Scaler, *scalers.ScalerConfig, error)
}

// GetScalers returns array of scalers and scaler config stored in the cache
func (c *ScalersCache) GetScalers() ([]scalers.Scaler, []scalers.ScalerConfig) {
scalersList := make([]scalers.Scaler, 0, len(c.Scalers))
configsList := make([]scalers.ScalerConfig, 0, len(c.Scalers))
Expand All @@ -59,6 +60,7 @@ func (c *ScalersCache) GetScalers() ([]scalers.Scaler, []scalers.ScalerConfig) {
return scalersList, configsList
}

// GetPushScaler returns array of push scalers stored in the cache
func (c *ScalersCache) GetPushScalers() []scalers.PushScaler {
var result []scalers.PushScaler
for _, s := range c.Scalers {
Expand All @@ -69,6 +71,27 @@ func (c *ScalersCache) GetPushScalers() []scalers.PushScaler {
return result
}

// Close closes all scalers in the cache
func (c *ScalersCache) Close(ctx context.Context) {
scalers := c.Scalers
c.Scalers = nil
for _, s := range scalers {
err := s.Scaler.Close(ctx)
if err != nil {
log.Error(err, "error closing scaler", "scaler", s)
}
}
}

// GetMetricSpecForScaling returns metrics specs for all scalers in the cache
func (c *ScalersCache) GetMetricSpecForScaling(ctx context.Context) []v2.MetricSpec {
var spec []v2.MetricSpec
for _, s := range c.Scalers {
spec = append(spec, s.Scaler.GetMetricSpecForScaling(ctx)...)
}
return spec
}

// GetMetricSpecForScalingForScaler returns metrics spec for a scaler identified by the metric name
func (c *ScalersCache) GetMetricSpecForScalingForScaler(ctx context.Context, index int) ([]v2.MetricSpec, error) {
var err error
Expand Down Expand Up @@ -118,6 +141,8 @@ func (c *ScalersCache) GetMetricsAndActivityForScaler(ctx context.Context, index
return metric, activity, time.Since(startTime).Milliseconds(), err
}

// TODO needs refactor - move ScaledJob related methods to scale_handler, the similar way ScaledObject methods are
// refactor logic
func (c *ScalersCache) IsScaledJobActive(ctx context.Context, scaledJob *kedav1alpha1.ScaledJob) (bool, int64, int64) {
var queueLength float64
var maxValue float64
Expand Down Expand Up @@ -202,32 +227,12 @@ func (c *ScalersCache) refreshScaler(ctx context.Context, id int) (scalers.Scale
return ns, nil
}

func (c *ScalersCache) GetMetricSpecForScaling(ctx context.Context) []v2.MetricSpec {
var spec []v2.MetricSpec
for _, s := range c.Scalers {
spec = append(spec, s.Scaler.GetMetricSpecForScaling(ctx)...)
}
return spec
}

func (c *ScalersCache) Close(ctx context.Context) {
scalers := c.Scalers
c.Scalers = nil
for _, s := range scalers {
err := s.Scaler.Close(ctx)
if err != nil {
log.Error(err, "error closing scaler", "scaler", s)
}
}
}

type scalerMetrics struct {
queueLength float64
maxValue float64
isActive bool
}

// TODO needs refactor
func (c *ScalersCache) getScaledJobMetrics(ctx context.Context, scaledJob *kedav1alpha1.ScaledJob) []scalerMetrics {
// TODO this loop should be probably done similar way the ScaledObject loop is done
var scalersMetrics []scalerMetrics
Expand Down
Loading

0 comments on commit 22a5111

Please sign in to comment.