Skip to content

Commit a825451

Browse files
authored
Refactor predictkube scaler config (#6282)
Signed-off-by: wangrushen <[email protected]>
1 parent 5062262 commit a825451

File tree

1 file changed

+55
-108
lines changed

1 file changed

+55
-108
lines changed

pkg/scalers/predictkube_scaler.go

Lines changed: 55 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,51 @@ type PredictKubeScaler struct {
8383
}
8484

8585
type predictKubeMetadata struct {
86-
predictHorizon time.Duration
87-
historyTimeWindow time.Duration
88-
stepDuration time.Duration
89-
apiKey string
90-
prometheusAddress string
91-
prometheusAuth *authentication.AuthMeta
92-
query string
93-
threshold float64
94-
activationThreshold float64
95-
triggerIndex int
86+
PrometheusAddress string `keda:"name=prometheusAddress, order=triggerMetadata"`
87+
PrometheusAuth *authentication.Config `keda:"optional"`
88+
Query string `keda:"name=query, order=triggerMetadata"`
89+
PredictHorizon string `keda:"name=predictHorizon, order=triggerMetadata"`
90+
QueryStep string `keda:"name=queryStep, order=triggerMetadata"`
91+
HistoryTimeWindow string `keda:"name=historyTimeWindow, order=triggerMetadata"`
92+
APIKey string `keda:"name=apiKey, order=authParams"`
93+
Threshold float64 `keda:"name=threshold, order=triggerMetadata, optional"`
94+
ActivationThreshold float64 `keda:"name=activationThreshold, order=triggerMetadata, optional"`
95+
96+
predictHorizon time.Duration
97+
historyTimeWindow time.Duration
98+
stepDuration time.Duration
99+
triggerIndex int
96100
}
97101

102+
func (p *predictKubeMetadata) Validate() error {
103+
validate := validator.New()
104+
err := validate.Var(p.PrometheusAddress, "url")
105+
if err != nil {
106+
return fmt.Errorf("invalid prometheusAddress")
107+
}
108+
109+
p.predictHorizon, err = str2duration.ParseDuration(p.PredictHorizon)
110+
if err != nil {
111+
return fmt.Errorf("predictHorizon parsing error %w", err)
112+
}
113+
114+
p.stepDuration, err = str2duration.ParseDuration(p.QueryStep)
115+
if err != nil {
116+
return fmt.Errorf("queryStep parsing error %w", err)
117+
}
118+
119+
p.historyTimeWindow, err = str2duration.ParseDuration(p.HistoryTimeWindow)
120+
if err != nil {
121+
return fmt.Errorf("historyTimeWindow parsing error %w", err)
122+
}
123+
124+
err = validate.Var(p.APIKey, "jwt")
125+
if err != nil {
126+
return fmt.Errorf("invalid apiKey")
127+
}
128+
129+
return nil
130+
}
98131
func (s *PredictKubeScaler) setupClientConn() error {
99132
clientOpt, err := pc.SetGrpcClientOptions(grpcConf,
100133
&libs.Base{
@@ -108,7 +141,7 @@ func (s *PredictKubeScaler) setupClientConn() error {
108141
Enabled: false,
109142
},
110143
},
111-
pc.InjectPublicClientMetadataInterceptor(s.metadata.apiKey),
144+
pc.InjectPublicClientMetadataInterceptor(s.metadata.APIKey),
112145
)
113146

114147
if !grpcConf.Conn.Insecure {
@@ -186,7 +219,7 @@ func (s *PredictKubeScaler) GetMetricSpecForScaling(context.Context) []v2.Metric
186219
Metric: v2.MetricIdentifier{
187220
Name: GenerateMetricNameWithIndex(s.metadata.triggerIndex, metricName),
188221
},
189-
Target: GetMetricTargetMili(s.metricType, s.metadata.threshold),
222+
Target: GetMetricTargetMili(s.metricType, s.metadata.Threshold),
190223
}
191224

192225
metricSpec := v2.MetricSpec{
@@ -211,7 +244,7 @@ func (s *PredictKubeScaler) GetMetricsAndActivity(ctx context.Context, metricNam
211244

212245
metric := GenerateMetricInMili(metricName, value)
213246

214-
return []external_metrics.ExternalMetricValue{metric}, activationValue > s.metadata.activationThreshold, nil
247+
return []external_metrics.ExternalMetricValue{metric}, activationValue > s.metadata.ActivationThreshold, nil
215248
}
216249

217250
func (s *PredictKubeScaler) doPredictRequest(ctx context.Context) (float64, float64, error) {
@@ -257,7 +290,7 @@ func (s *PredictKubeScaler) doQuery(ctx context.Context) ([]*commonproto.Item, e
257290
Step: s.metadata.stepDuration,
258291
}
259292

260-
val, warns, err := s.api.QueryRange(ctx, s.metadata.query, r)
293+
val, warns, err := s.api.QueryRange(ctx, s.metadata.Query, r)
261294

262295
if len(warns) > 0 {
263296
s.logger.V(1).Info("warnings", warns)
@@ -345,103 +378,17 @@ func (s *PredictKubeScaler) parsePrometheusResult(result model.Value) (out []*co
345378
}
346379

347380
func parsePredictKubeMetadata(config *scalersconfig.ScalerConfig) (result *predictKubeMetadata, err error) {
348-
validate := validator.New()
349-
meta := predictKubeMetadata{}
350-
351-
if val, ok := config.TriggerMetadata["query"]; ok {
352-
if len(val) == 0 {
353-
return nil, fmt.Errorf("no query given")
354-
}
355-
356-
meta.query = val
357-
} else {
358-
return nil, fmt.Errorf("no query given")
359-
}
360-
361-
if val, ok := config.TriggerMetadata["prometheusAddress"]; ok {
362-
err = validate.Var(val, "url")
363-
if err != nil {
364-
return nil, fmt.Errorf("invalid prometheusAddress")
365-
}
366-
367-
meta.prometheusAddress = val
368-
} else {
369-
return nil, fmt.Errorf("no prometheusAddress given")
370-
}
371-
372-
if val, ok := config.TriggerMetadata["predictHorizon"]; ok {
373-
predictHorizon, err := str2duration.ParseDuration(val)
374-
if err != nil {
375-
return nil, fmt.Errorf("predictHorizon parsing error %w", err)
376-
}
377-
meta.predictHorizon = predictHorizon
378-
} else {
379-
return nil, fmt.Errorf("no predictHorizon given")
380-
}
381-
382-
if val, ok := config.TriggerMetadata["queryStep"]; ok {
383-
stepDuration, err := str2duration.ParseDuration(val)
384-
if err != nil {
385-
return nil, fmt.Errorf("queryStep parsing error %w", err)
386-
}
387-
meta.stepDuration = stepDuration
388-
} else {
389-
return nil, fmt.Errorf("no queryStep given")
390-
}
391-
392-
if val, ok := config.TriggerMetadata["historyTimeWindow"]; ok {
393-
historyTimeWindow, err := str2duration.ParseDuration(val)
394-
if err != nil {
395-
return nil, fmt.Errorf("historyTimeWindow parsing error %w", err)
396-
}
397-
meta.historyTimeWindow = historyTimeWindow
398-
} else {
399-
return nil, fmt.Errorf("no historyTimeWindow given")
400-
}
401-
402-
if val, ok := config.TriggerMetadata["threshold"]; ok {
403-
threshold, err := strconv.ParseFloat(val, 64)
404-
if err != nil {
405-
return nil, fmt.Errorf("threshold parsing error %w", err)
406-
}
407-
meta.threshold = threshold
408-
} else {
409-
if config.AsMetricSource {
410-
meta.threshold = 0
411-
} else {
412-
return nil, fmt.Errorf("no threshold given")
413-
}
381+
meta := &predictKubeMetadata{}
382+
if err := config.TypedConfig(meta); err != nil {
383+
return nil, fmt.Errorf("error parsing arango metadata: %w", err)
414384
}
415385

416-
meta.activationThreshold = 0
417-
if val, ok := config.TriggerMetadata["activationThreshold"]; ok {
418-
activationThreshold, err := strconv.ParseFloat(val, 64)
419-
if err != nil {
420-
return nil, fmt.Errorf("activationThreshold parsing error %w", err)
421-
}
422-
meta.activationThreshold = activationThreshold
386+
if !config.AsMetricSource && meta.Threshold == 0 {
387+
return nil, fmt.Errorf("no threshold given")
423388
}
424389

425390
meta.triggerIndex = config.TriggerIndex
426-
427-
if val, ok := config.AuthParams["apiKey"]; ok {
428-
err = validate.Var(val, "jwt")
429-
if err != nil {
430-
return nil, fmt.Errorf("invalid apiKey")
431-
}
432-
433-
meta.apiKey = val
434-
} else {
435-
return nil, fmt.Errorf("no api key given")
436-
}
437-
438-
// parse auth configs from ScalerConfig
439-
auth, err := authentication.GetAuthConfigs(config.TriggerMetadata, config.AuthParams)
440-
if err != nil {
441-
return nil, err
442-
}
443-
meta.prometheusAuth = auth
444-
return &meta, nil
391+
return meta, nil
445392
}
446393

447394
func (s *PredictKubeScaler) ping(ctx context.Context) (err error) {
@@ -454,14 +401,14 @@ func (s *PredictKubeScaler) initPredictKubePrometheusConn(ctx context.Context) (
454401
// create http.RoundTripper with auth settings from ScalerConfig
455402
roundTripper, err := authentication.CreateHTTPRoundTripper(
456403
authentication.FastHTTP,
457-
s.metadata.prometheusAuth,
404+
s.metadata.PrometheusAuth.ToAuthMeta(),
458405
)
459406
if err != nil {
460407
s.logger.V(1).Error(err, "init Prometheus client http transport")
461408
return err
462409
}
463410
client, err := api.NewClient(api.Config{
464-
Address: s.metadata.prometheusAddress,
411+
Address: s.metadata.PrometheusAddress,
465412
RoundTripper: roundTripper,
466413
})
467414
if err != nil {

0 commit comments

Comments
 (0)