Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Brouwer <rickbrouwer@gmail.com>
  • Loading branch information
rickbrouwer committed Sep 17, 2024
1 parent 83b73eb commit 5d2d7a5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
17 changes: 13 additions & 4 deletions pkg/scalers/cron_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scalers
import (
"context"
"fmt"
"strings"
"time"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -35,7 +36,6 @@ type cronMetadata struct {
}

func (m *cronMetadata) Validate() error {

parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
if _, err := parser.Parse(m.Start); err != nil {
return fmt.Errorf("error parsing start schedule: %w", err)
Expand Down Expand Up @@ -98,10 +98,19 @@ func (s *cronScaler) Close(context.Context) error {
return nil
}

func parseCronTimeFormat(s string) string {
s = strings.ReplaceAll(s, " ", "")
s = strings.ReplaceAll(s, "*", "x")
s = strings.ReplaceAll(s, "/", "Sl")
s = strings.ReplaceAll(s, "?", "Qm")
return s
}

// GetMetricSpecForScaling returns the metric spec for the HPA
func (s *cronScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
externalMetric := &v2.ExternalMetricSource{
Metric: v2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.TriggerIndex, kedautil.NormalizeString(fmt.Sprintf("cron-%s-%s-%s", s.metadata.Timezone, s.metadata.Start, s.metadata.End))),
Name: GenerateMetricNameWithIndex(s.metadata.TriggerIndex, kedautil.NormalizeString(fmt.Sprintf("cron-%s-%s-%s", s.metadata.Timezone, parseCronTimeFormat(s.metadata.Start), parseCronTimeFormat(s.metadata.End)))),
},
Target: GetMetricTarget(s.metricType, s.metadata.DesiredReplicas),
}
Expand All @@ -112,7 +121,7 @@ func (s *cronScaler) GetMetricSpecForScaling(context.Context) []v2.MetricSpec {
func (s *cronScaler) GetMetricsAndActivity(_ context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error) {
location, err := time.LoadLocation(s.metadata.Timezone)
if err != nil {
return nil, false, fmt.Errorf("unable to load timezone: %w", err)
return []external_metrics.ExternalMetricValue{}, false, fmt.Errorf("unable to load timezone: %w", err)
}

currentTime := time.Now().In(location)
Expand All @@ -131,7 +140,7 @@ func (s *cronScaler) GetMetricsAndActivity(_ context.Context, metricName string)
isWithinInterval = currentTime.After(nextStartTime) || currentTime.Before(nextEndTime)
}

metricValue := float64(0)
metricValue := float64(1)
if isWithinInterval {
metricValue = float64(s.metadata.DesiredReplicas)
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/scalers/cron_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/go-logr/logr"
"github.com/robfig/cron/v3"
"github.com/stretchr/testify/assert"

"github.com/kedacore/keda/v2/pkg/scalers/scalersconfig"
Expand Down Expand Up @@ -121,7 +122,18 @@ func TestCronGetMetricSpecForScaling(t *testing.T) {
if err != nil {
t.Fatal("Could not parse metadata:", err)
}
mockCronScaler := cronScaler{"", meta, logr.Discard()}

parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
startSchedule, _ := parser.Parse(meta.Start)
endSchedule, _ := parser.Parse(meta.End)

mockCronScaler := cronScaler{
metricType: "",
metadata: meta,
logger: logr.Discard(),
startSchedule: startSchedule,
endSchedule: endSchedule,
}

metricSpec := mockCronScaler.GetMetricSpecForScaling(context.Background())
metricName := metricSpec[0].External.Metric.Name
Expand Down

0 comments on commit 5d2d7a5

Please sign in to comment.