-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathmetrics_api_scaler.go
335 lines (284 loc) · 9.05 KB
/
metrics_api_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
package scalers
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
neturl "net/url"
"github.com/tidwall/gjson"
"k8s.io/api/autoscaling/v2beta2"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/metrics/pkg/apis/external_metrics"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"github.com/kedacore/keda/v2/pkg/scalers/authentication"
kedautil "github.com/kedacore/keda/v2/pkg/util"
)
type metricsAPIScaler struct {
metadata *metricsAPIScalerMetadata
client *http.Client
}
type metricsAPIScalerMetadata struct {
targetValue int
url string
valueLocation string
// apiKeyAuth
enableAPIKeyAuth bool
method string // way of providing auth key, either "header" (default) or "query"
// keyParamName is either header key or query param used for passing apikey
// default header is "X-API-KEY", defaul query param is "api_key"
keyParamName string
apiKey string
// base auth
enableBaseAuth bool
username string
password string // +optional
// client certification
enableTLS bool
cert string
key string
ca string
// bearer
enableBearerAuth bool
bearerToken string
scalerIndex int
}
const (
methodValueQuery = "query"
)
var httpLog = logf.Log.WithName("metrics_api_scaler")
// NewMetricsAPIScaler creates a new HTTP scaler
func NewMetricsAPIScaler(config *ScalerConfig) (Scaler, error) {
meta, err := parseMetricsAPIMetadata(config)
if err != nil {
return nil, fmt.Errorf("error parsing metric API metadata: %s", err)
}
httpClient := kedautil.CreateHTTPClient(config.GlobalHTTPTimeout, false)
if meta.enableTLS || len(meta.ca) > 0 {
config, err := kedautil.NewTLSConfig(meta.cert, meta.key, meta.ca)
if err != nil {
return nil, err
}
httpClient.Transport = &http.Transport{TLSClientConfig: config}
}
return &metricsAPIScaler{
metadata: meta,
client: httpClient,
}, nil
}
func parseMetricsAPIMetadata(config *ScalerConfig) (*metricsAPIScalerMetadata, error) {
meta := metricsAPIScalerMetadata{}
meta.scalerIndex = config.ScalerIndex
if val, ok := config.TriggerMetadata["targetValue"]; ok {
targetValue, err := strconv.Atoi(val)
if err != nil {
return nil, fmt.Errorf("targetValue parsing error %s", err.Error())
}
meta.targetValue = targetValue
} else {
return nil, fmt.Errorf("no targetValue given in metadata")
}
if val, ok := config.TriggerMetadata["url"]; ok {
meta.url = val
} else {
return nil, fmt.Errorf("no url given in metadata")
}
if val, ok := config.TriggerMetadata["valueLocation"]; ok {
meta.valueLocation = val
} else {
return nil, fmt.Errorf("no valueLocation given in metadata")
}
authMode, ok := config.TriggerMetadata["authMode"]
// no authMode specified
if !ok {
return &meta, nil
}
authType := authentication.Type(strings.TrimSpace(authMode))
switch authType {
case authentication.APIKeyAuthType:
if len(config.AuthParams["apiKey"]) == 0 {
return nil, errors.New("no apikey provided")
}
meta.apiKey = config.AuthParams["apiKey"]
// default behaviour is header. only change if query param requested
meta.method = "header"
meta.enableAPIKeyAuth = true
if config.TriggerMetadata["method"] == methodValueQuery {
meta.method = methodValueQuery
}
if len(config.TriggerMetadata["keyParamName"]) > 0 {
meta.keyParamName = config.TriggerMetadata["keyParamName"]
}
case authentication.BasicAuthType:
if len(config.AuthParams["username"]) == 0 {
return nil, errors.New("no username given")
}
meta.username = config.AuthParams["username"]
// password is optional. For convenience, many application implements basic auth with
// username as apikey and password as empty
meta.password = config.AuthParams["password"]
meta.enableBaseAuth = true
case authentication.TLSAuthType:
if len(config.AuthParams["ca"]) == 0 {
return nil, errors.New("no ca given")
}
if len(config.AuthParams["cert"]) == 0 {
return nil, errors.New("no cert given")
}
meta.cert = config.AuthParams["cert"]
if len(config.AuthParams["key"]) == 0 {
return nil, errors.New("no key given")
}
meta.key = config.AuthParams["key"]
meta.enableTLS = true
case authentication.BearerAuthType:
if len(config.AuthParams["token"]) == 0 {
return nil, errors.New("no token provided")
}
meta.bearerToken = config.AuthParams["token"]
meta.enableBearerAuth = true
default:
return nil, fmt.Errorf("err incorrect value for authMode is given: %s", authMode)
}
if len(config.AuthParams["ca"]) > 0 {
meta.ca = config.AuthParams["ca"]
}
return &meta, nil
}
// GetValueFromResponse uses provided valueLocation to access the numeric value in provided body
func GetValueFromResponse(body []byte, valueLocation string) (*resource.Quantity, error) {
r := gjson.GetBytes(body, valueLocation)
errorMsg := "valueLocation must point to value of type number or a string representing a Quantity got: '%s'"
if r.Type == gjson.String {
q, err := resource.ParseQuantity(r.String())
if err != nil {
return nil, fmt.Errorf(errorMsg, r.String())
}
return &q, nil
}
if r.Type != gjson.Number {
return nil, fmt.Errorf(errorMsg, r.Type.String())
}
return resource.NewQuantity(int64(r.Num), resource.DecimalSI), nil
}
func (s *metricsAPIScaler) getMetricValue(ctx context.Context) (*resource.Quantity, error) {
request, err := getMetricAPIServerRequest(ctx, s.metadata)
if err != nil {
return nil, err
}
r, err := s.client.Do(request)
if err != nil {
return nil, err
}
if r.StatusCode != http.StatusOK {
msg := fmt.Sprintf("api returned %d", r.StatusCode)
return nil, errors.New(msg)
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
v, err := GetValueFromResponse(b, s.metadata.valueLocation)
if err != nil {
return nil, err
}
return v, nil
}
// Close does nothing in case of metricsAPIScaler
func (s *metricsAPIScaler) Close(context.Context) error {
return nil
}
// IsActive returns true if there are pending messages to be processed
func (s *metricsAPIScaler) IsActive(ctx context.Context) (bool, error) {
v, err := s.getMetricValue(ctx)
if err != nil {
httpLog.Error(err, fmt.Sprintf("Error when checking metric value: %s", err))
return false, err
}
return v.AsApproximateFloat64() > 0.0, nil
}
// GetMetricSpecForScaling returns the MetricSpec for the Horizontal Pod Autoscaler
func (s *metricsAPIScaler) GetMetricSpecForScaling(context.Context) []v2beta2.MetricSpec {
targetValue := resource.NewQuantity(int64(s.metadata.targetValue), resource.DecimalSI)
externalMetric := &v2beta2.ExternalMetricSource{
Metric: v2beta2.MetricIdentifier{
Name: GenerateMetricNameWithIndex(s.metadata.scalerIndex, kedautil.NormalizeString(fmt.Sprintf("metric-api-%s", s.metadata.valueLocation))),
},
Target: v2beta2.MetricTarget{
Type: v2beta2.AverageValueMetricType,
AverageValue: targetValue,
},
}
metricSpec := v2beta2.MetricSpec{
External: externalMetric, Type: externalMetricType,
}
return []v2beta2.MetricSpec{metricSpec}
}
// GetMetrics returns value for a supported metric and an error if there is a problem getting the metric
func (s *metricsAPIScaler) GetMetrics(ctx context.Context, metricName string, metricSelector labels.Selector) ([]external_metrics.ExternalMetricValue, error) {
v, err := s.getMetricValue(ctx)
if err != nil {
return []external_metrics.ExternalMetricValue{}, fmt.Errorf("error requesting metrics endpoint: %s", err)
}
metric := external_metrics.ExternalMetricValue{
MetricName: metricName,
Value: *v,
Timestamp: metav1.Now(),
}
return append([]external_metrics.ExternalMetricValue{}, metric), nil
}
func getMetricAPIServerRequest(ctx context.Context, meta *metricsAPIScalerMetadata) (*http.Request, error) {
var req *http.Request
var err error
switch {
case meta.enableAPIKeyAuth:
if meta.method == methodValueQuery {
url, _ := neturl.Parse(meta.url)
queryString := url.Query()
if len(meta.keyParamName) == 0 {
queryString.Set("api_key", meta.apiKey)
} else {
queryString.Set(meta.keyParamName, meta.apiKey)
}
url.RawQuery = queryString.Encode()
req, err = http.NewRequestWithContext(ctx, "GET", url.String(), nil)
if err != nil {
return nil, err
}
} else {
// default behaviour is to use header method
req, err = http.NewRequestWithContext(ctx, "GET", meta.url, nil)
if err != nil {
return nil, err
}
if len(meta.keyParamName) == 0 {
req.Header.Add("X-API-KEY", meta.apiKey)
} else {
req.Header.Add(meta.keyParamName, meta.apiKey)
}
}
case meta.enableBaseAuth:
req, err = http.NewRequestWithContext(ctx, "GET", meta.url, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(meta.username, meta.password)
case meta.enableBearerAuth:
req, err = http.NewRequestWithContext(ctx, "GET", meta.url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", meta.bearerToken))
default:
req, err = http.NewRequestWithContext(ctx, "GET", meta.url, nil)
if err != nil {
return nil, err
}
}
return req, nil
}