@@ -83,18 +83,51 @@ type PredictKubeScaler struct {
83
83
}
84
84
85
85
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
96
100
}
97
101
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
+ }
98
131
func (s * PredictKubeScaler ) setupClientConn () error {
99
132
clientOpt , err := pc .SetGrpcClientOptions (grpcConf ,
100
133
& libs.Base {
@@ -108,7 +141,7 @@ func (s *PredictKubeScaler) setupClientConn() error {
108
141
Enabled : false ,
109
142
},
110
143
},
111
- pc .InjectPublicClientMetadataInterceptor (s .metadata .apiKey ),
144
+ pc .InjectPublicClientMetadataInterceptor (s .metadata .APIKey ),
112
145
)
113
146
114
147
if ! grpcConf .Conn .Insecure {
@@ -186,7 +219,7 @@ func (s *PredictKubeScaler) GetMetricSpecForScaling(context.Context) []v2.Metric
186
219
Metric : v2.MetricIdentifier {
187
220
Name : GenerateMetricNameWithIndex (s .metadata .triggerIndex , metricName ),
188
221
},
189
- Target : GetMetricTargetMili (s .metricType , s .metadata .threshold ),
222
+ Target : GetMetricTargetMili (s .metricType , s .metadata .Threshold ),
190
223
}
191
224
192
225
metricSpec := v2.MetricSpec {
@@ -211,7 +244,7 @@ func (s *PredictKubeScaler) GetMetricsAndActivity(ctx context.Context, metricNam
211
244
212
245
metric := GenerateMetricInMili (metricName , value )
213
246
214
- return []external_metrics.ExternalMetricValue {metric }, activationValue > s .metadata .activationThreshold , nil
247
+ return []external_metrics.ExternalMetricValue {metric }, activationValue > s .metadata .ActivationThreshold , nil
215
248
}
216
249
217
250
func (s * PredictKubeScaler ) doPredictRequest (ctx context.Context ) (float64 , float64 , error ) {
@@ -257,7 +290,7 @@ func (s *PredictKubeScaler) doQuery(ctx context.Context) ([]*commonproto.Item, e
257
290
Step : s .metadata .stepDuration ,
258
291
}
259
292
260
- val , warns , err := s .api .QueryRange (ctx , s .metadata .query , r )
293
+ val , warns , err := s .api .QueryRange (ctx , s .metadata .Query , r )
261
294
262
295
if len (warns ) > 0 {
263
296
s .logger .V (1 ).Info ("warnings" , warns )
@@ -345,103 +378,17 @@ func (s *PredictKubeScaler) parsePrometheusResult(result model.Value) (out []*co
345
378
}
346
379
347
380
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 )
414
384
}
415
385
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" )
423
388
}
424
389
425
390
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
445
392
}
446
393
447
394
func (s * PredictKubeScaler ) ping (ctx context.Context ) (err error ) {
@@ -454,14 +401,14 @@ func (s *PredictKubeScaler) initPredictKubePrometheusConn(ctx context.Context) (
454
401
// create http.RoundTripper with auth settings from ScalerConfig
455
402
roundTripper , err := authentication .CreateHTTPRoundTripper (
456
403
authentication .FastHTTP ,
457
- s .metadata .prometheusAuth ,
404
+ s .metadata .PrometheusAuth . ToAuthMeta () ,
458
405
)
459
406
if err != nil {
460
407
s .logger .V (1 ).Error (err , "init Prometheus client http transport" )
461
408
return err
462
409
}
463
410
client , err := api .NewClient (api.Config {
464
- Address : s .metadata .prometheusAddress ,
411
+ Address : s .metadata .PrometheusAddress ,
465
412
RoundTripper : roundTripper ,
466
413
})
467
414
if err != nil {
0 commit comments