-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathscaler.go
375 lines (334 loc) · 11.8 KB
/
scaler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
Copyright 2021 The KEDA Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scalers
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"github.com/go-logr/logr"
metrics "github.com/rcrowley/go-metrics"
cast "github.com/spf13/cast"
v2 "k8s.io/api/autoscaling/v2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/kedacore/keda/v2/pkg/scalers/scalersconfig"
)
func init() {
// Disable metrics for kafka client (sarama)
// https://github.com/IBM/sarama/issues/1321
metrics.UseNilMetrics = true
}
// Scaler interface
type Scaler interface {
// GetMetricsAndActivity returns the metric values and activity for a metric Name
GetMetricsAndActivity(ctx context.Context, metricName string) ([]external_metrics.ExternalMetricValue, bool, error)
// GetMetricSpecForScaling returns the metrics based on which this scaler determines that the ScaleTarget scales. This is used to construct the HPA spec that is created for
// this scaled object. The labels used should match the selectors used in GetMetrics
GetMetricSpecForScaling(ctx context.Context) []v2.MetricSpec
// Close any resources that need disposing when scaler is no longer used or destroyed
Close(ctx context.Context) error
}
// PushScaler interface
type PushScaler interface {
Scaler
// Run is the only writer to the active channel and must close it once done.
Run(ctx context.Context, active chan<- bool)
}
var (
// ErrScalerUnsupportedUtilizationMetricType is returned when v2.UtilizationMetricType
// is provided as the metric target type for scaler.
ErrScalerUnsupportedUtilizationMetricType = errors.New("'Utilization' metric type is unsupported for external metrics, allowed values are 'Value' or 'AverageValue'")
// ErrScalerConfigMissingField is returned when a required field is missing from the scaler config.
ErrScalerConfigMissingField = errors.New("missing required field in scaler config")
)
// GetFromAuthOrMeta helps to get a field from Auth or Meta sections
func GetFromAuthOrMeta(config *scalersconfig.ScalerConfig, field string) (string, error) {
var result string
var err error
if config.AuthParams[field] != "" {
result = config.AuthParams[field]
} else if config.TriggerMetadata[field] != "" {
result = config.TriggerMetadata[field]
}
if result == "" {
err = fmt.Errorf("%w: no %s given", ErrScalerConfigMissingField, field)
}
return result, err
}
// GenerateMetricNameWithIndex helps to add the index prefix to the metric name
func GenerateMetricNameWithIndex(triggerIndex int, metricName string) string {
return fmt.Sprintf("s%d-%s", triggerIndex, metricName)
}
// RemoveIndexFromMetricName removes the index prefix from the metric name
func RemoveIndexFromMetricName(triggerIndex int, metricName string) (string, error) {
metricNameSplit := strings.SplitN(metricName, "-", 2)
if len(metricNameSplit) != 2 {
return "", fmt.Errorf("metric name without index prefix")
}
indexPrefix, metricNameWithoutIndex := metricNameSplit[0], metricNameSplit[1]
if indexPrefix != fmt.Sprintf("s%d", triggerIndex) {
return "", fmt.Errorf("metric name contains incorrect index prefix")
}
return metricNameWithoutIndex, nil
}
func InitializeLogger(config *scalersconfig.ScalerConfig, scalerName string) logr.Logger {
return logf.Log.WithName(scalerName).WithValues("type", config.ScalableObjectType, "namespace", config.ScalableObjectNamespace, "name", config.ScalableObjectName)
}
// GetMetricTargetType helps get the metric target type of the scaler
func GetMetricTargetType(config *scalersconfig.ScalerConfig) (v2.MetricTargetType, error) {
switch config.MetricType {
case v2.UtilizationMetricType:
return "", ErrScalerUnsupportedUtilizationMetricType
case "":
// Use AverageValue if no metric type was provided
return v2.AverageValueMetricType, nil
default:
return config.MetricType, nil
}
}
// GetMetricTarget returns a metric target for a valid given metric target type (Value or AverageValue) and value
func GetMetricTarget(metricType v2.MetricTargetType, metricValue int64) v2.MetricTarget {
target := v2.MetricTarget{
Type: metricType,
}
// Construct the target size as a quantity
targetQty := resource.NewQuantity(metricValue, resource.DecimalSI)
if metricType == v2.AverageValueMetricType {
target.AverageValue = targetQty
} else {
target.Value = targetQty
}
return target
}
// GetMetricTargetMili returns a metric target for a valid given metric target type (Value or AverageValue) and value in mili scale
func GetMetricTargetMili(metricType v2.MetricTargetType, metricValue float64) v2.MetricTarget {
target := v2.MetricTarget{
Type: metricType,
}
// Construct the target size as a quantity
metricValueMili := int64(metricValue * 1000)
targetQty := resource.NewMilliQuantity(metricValueMili, resource.DecimalSI)
if metricType == v2.AverageValueMetricType {
target.AverageValue = targetQty
} else {
target.Value = targetQty
}
return target
}
// GenerateMetricInMili returns a externalMetricValue with mili as metric scale
func GenerateMetricInMili(metricName string, value float64) external_metrics.ExternalMetricValue {
valueMili := int64(value * 1000)
return external_metrics.ExternalMetricValue{
MetricName: metricName,
Value: *resource.NewMilliQuantity(valueMili, resource.DecimalSI),
Timestamp: metav1.Now(),
}
}
// Option represents a function type that modifies a configOptions instance.
type Option func(*configOptions)
type configOptions struct {
useMetadata bool // Indicates whether to use metadata.
useAuthentication bool // Indicates whether to use authentication.
useResolvedEnv bool // Indicates whether to use resolved environment variables.
isOptional bool // Indicates whether the configuration is optional.
defaultVal interface{} // Default value for the configuration.
}
// UseMetadata is an Option function that sets the useMetadata field of configOptions.
func UseMetadata(metadata bool) Option {
return func(opt *configOptions) {
opt.useMetadata = metadata
}
}
// UseAuthentication is an Option function that sets the useAuthentication field of configOptions.
func UseAuthentication(auth bool) Option {
return func(opt *configOptions) {
opt.useAuthentication = auth
}
}
// UseResolvedEnv is an Option function that sets the useResolvedEnv field of configOptions.
func UseResolvedEnv(resolvedEnv bool) Option {
return func(opt *configOptions) {
opt.useResolvedEnv = resolvedEnv
}
}
// IsOptional is an Option function that sets the isOptional field of configOptions.
func IsOptional(optional bool) Option {
return func(opt *configOptions) {
opt.isOptional = optional
}
}
// WithDefaultVal is an Option function that sets the defaultVal field of configOptions.
func WithDefaultVal(defaultVal interface{}) Option {
return func(opt *configOptions) {
opt.defaultVal = defaultVal
}
}
// getParameterFromConfigV2 retrieves a parameter value from the provided ScalerConfig object based on the specified parameter name, target type, and optional configuration options.
//
// This method searches for the parameter value in different places within the ScalerConfig object, such as authentication parameters, trigger metadata, and resolved environment variables, based on the provided options.
// It then attempts to convert the found value to the specified target type and returns it.
//
// Parameters:
//
// config: A pointer to a ScalerConfig object from which to retrieve the parameter value.
// parameter: A string representing the name of the parameter to retrieve.
// targetType: A reflect.Type representing the target type to which the parameter value should be converted.
// options: An optional variadic parameter that allows configuring the behavior of the method through Option functions.
//
// Returns:
// - An interface{} representing the retrieved parameter value, converted to the specified target type.
// - An error, if any occurred during the retrieval or conversion process.
//
// Example Usage:
//
// To retrieve a parameter value from a ScalerConfig object, you can call this function with the necessary parameters and options
//
// ```
// val, err := getParameterFromConfigV2(scalerConfig, "parameterName", reflect.TypeOf(int64(0)), UseMetadata(true), UseAuthentication(true))
// if err != nil {
// // Handle error
// }
func getParameterFromConfigV2(config *scalersconfig.ScalerConfig, parameter string, targetType reflect.Type, options ...Option) (interface{}, error) {
opt := &configOptions{defaultVal: ""}
for _, option := range options {
option(opt)
}
foundCount := 0
var foundVal string
var convertedVal interface{}
var foundErr error
if val, ok := config.AuthParams[parameter]; ok && val != "" {
foundCount++
if opt.useAuthentication {
foundVal = val
}
}
if val, ok := config.TriggerMetadata[parameter]; ok && val != "" {
foundCount++
if opt.useMetadata {
foundVal = val
}
}
if envFromVal, envFromOk := config.TriggerMetadata[fmt.Sprintf("%sFromEnv", parameter)]; envFromOk {
if val, ok := config.ResolvedEnv[envFromVal]; ok && val != "" {
foundCount++
if opt.useResolvedEnv {
foundVal = val
}
}
}
convertedVal, foundErr = convertToType(foundVal, targetType)
switch {
case foundCount > 1:
return opt.defaultVal, fmt.Errorf("value for parameter '%s' found in more than one place", parameter)
case foundCount == 1:
if foundErr != nil {
return opt.defaultVal, foundErr
}
return convertedVal, nil
case opt.isOptional:
return opt.defaultVal, nil
default:
return opt.defaultVal, fmt.Errorf("key not found. Either set the correct key or set isOptional to true and set defaultVal")
}
}
func convertToType(input interface{}, targetType reflect.Type) (interface{}, error) {
switch targetType.Kind() {
case reflect.String:
return fmt.Sprintf("%v", input), nil
case reflect.Int:
val, err := cast.ToIntE(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Int8:
val, err := cast.ToInt8E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Int16:
val, err := cast.ToInt16E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Int32:
val, err := cast.ToInt32E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Int64:
val, err := cast.ToInt64E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint:
val, err := cast.ToUintE(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint8:
val, err := cast.ToUint8E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint16:
val, err := cast.ToUint16E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint32:
val, err := cast.ToUint32E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Uint64:
val, err := cast.ToUint64E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Float32:
val, err := cast.ToFloat32E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Float64:
val, err := cast.ToFloat64E(input)
if err != nil {
return nil, err
}
return val, nil
case reflect.Bool:
val, err := cast.ToBoolE(input)
if err != nil {
return nil, err
}
return val, nil
default:
return nil, fmt.Errorf("unsupported target type: %v", targetType)
}
}