forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
266 lines (233 loc) · 7.44 KB
/
handlers.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
// Handlers contains the gRPC implementation for an external scaler as defined
// by the KEDA documentation at https://keda.sh/docs/2.0/concepts/external-scalers/#built-in-scalers-interface
// This is the interface KEDA will poll in order to get the request queue size
// and scale user apps properly
package main
import (
"context"
"errors"
"fmt"
"math"
"strconv"
"time"
"github.com/go-logr/logr"
"github.com/kedacore/keda/v2/pkg/scalers/externalscaler"
"google.golang.org/protobuf/types/known/emptypb"
"k8s.io/utils/ptr"
informershttpv1alpha1 "github.com/kedacore/http-add-on/operator/generated/informers/externalversions/http/v1alpha1"
"github.com/kedacore/http-add-on/pkg/k8s"
"github.com/kedacore/http-add-on/pkg/util"
)
const (
keyInterceptorTargetPendingRequests = "interceptorTargetPendingRequests"
)
var streamInterval time.Duration
func init() {
defaultMS := 200
timeoutMS, err := util.ResolveOsEnvInt("KEDA_HTTP_SCALER_STREAM_INTERVAL_MS", defaultMS)
if err != nil {
timeoutMS = defaultMS
}
streamInterval = time.Duration(timeoutMS) * time.Millisecond
}
type impl struct {
lggr logr.Logger
pinger *queuePinger
httpsoInformer informershttpv1alpha1.HTTPScaledObjectInformer
targetMetric int64
externalscaler.UnimplementedExternalScalerServer
}
func newImpl(
lggr logr.Logger,
pinger *queuePinger,
httpsoInformer informershttpv1alpha1.HTTPScaledObjectInformer,
defaultTargetMetric int64,
) *impl {
return &impl{
lggr: lggr,
pinger: pinger,
httpsoInformer: httpsoInformer,
targetMetric: defaultTargetMetric,
}
}
func (e *impl) Ping(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
return &emptypb.Empty{}, nil
}
func (e *impl) IsActive(
ctx context.Context,
sor *externalscaler.ScaledObjectRef,
) (*externalscaler.IsActiveResponse, error) {
lggr := e.lggr.WithName("IsActive")
gmr, err := e.GetMetrics(ctx, &externalscaler.GetMetricsRequest{
ScaledObjectRef: sor,
})
if err != nil {
lggr.Error(err, "GetMetrics failed", "scaledObjectRef", sor.String())
return nil, err
}
metricValues := gmr.GetMetricValues()
if err := errors.New("len(metricValues) != 1"); len(metricValues) != 1 {
lggr.Error(err, "invalid GetMetricsResponse", "scaledObjectRef", sor.String(), "getMetricsResponse", gmr.String())
return nil, err
}
metricValue := metricValues[0].GetMetricValue()
active := metricValue > 0
res := &externalscaler.IsActiveResponse{
Result: active,
}
return res, nil
}
func (e *impl) StreamIsActive(
scaledObject *externalscaler.ScaledObjectRef,
server externalscaler.ExternalScaler_StreamIsActiveServer,
) error {
// this function communicates with KEDA via the 'server' parameter.
// we call server.Send (below) every streamInterval, which tells it to immediately
// ping our IsActive RPC
ticker := time.NewTicker(streamInterval)
defer ticker.Stop()
for {
select {
case <-server.Context().Done():
return nil
case <-ticker.C:
active, err := e.IsActive(server.Context(), scaledObject)
if err != nil {
e.lggr.Error(
err,
"error getting active status in stream",
)
return err
}
err = server.Send(&externalscaler.IsActiveResponse{
Result: active.Result,
})
if err != nil {
e.lggr.Error(
err,
"error sending the active result in stream",
)
return err
}
}
}
}
func (e *impl) GetMetricSpec(
_ context.Context,
sor *externalscaler.ScaledObjectRef,
) (*externalscaler.GetMetricSpecResponse, error) {
lggr := e.lggr.WithName("GetMetricSpec")
namespacedName := k8s.NamespacedNameFromScaledObjectRef(sor)
metricName := MetricName(namespacedName)
httpso, err := e.httpsoInformer.Lister().HTTPScaledObjects(sor.Namespace).Get(sor.Name)
if err != nil {
if scalerMetadata := sor.GetScalerMetadata(); scalerMetadata != nil {
if interceptorTargetPendingRequests, ok := scalerMetadata[keyInterceptorTargetPendingRequests]; ok {
return e.interceptorMetricSpec(metricName, interceptorTargetPendingRequests)
}
}
lggr.Error(err, "unable to get HTTPScaledObject", "name", sor.Name, "namespace", sor.Namespace)
return nil, err
}
targetValue := int64(ptr.Deref(httpso.Spec.TargetPendingRequests, 100))
if httpso.Spec.ScalingMetric != nil {
if httpso.Spec.ScalingMetric.Concurrency != nil {
targetValue = int64(httpso.Spec.ScalingMetric.Concurrency.TargetValue)
}
if httpso.Spec.ScalingMetric.Rate != nil {
targetValue = int64(httpso.Spec.ScalingMetric.Rate.TargetValue)
}
}
res := &externalscaler.GetMetricSpecResponse{
MetricSpecs: []*externalscaler.MetricSpec{
{
MetricName: metricName,
TargetSize: targetValue,
},
},
}
return res, nil
}
func (e *impl) interceptorMetricSpec(metricName string, interceptorTargetPendingRequests string) (*externalscaler.GetMetricSpecResponse, error) {
lggr := e.lggr.WithName("interceptorMetricSpec")
targetPendingRequests, err := strconv.ParseInt(interceptorTargetPendingRequests, 10, 64)
if err != nil {
lggr.Error(err, "unable to parse interceptorTargetPendingRequests", "value", interceptorTargetPendingRequests)
return nil, err
}
res := &externalscaler.GetMetricSpecResponse{
MetricSpecs: []*externalscaler.MetricSpec{
{
MetricName: metricName,
TargetSize: targetPendingRequests,
},
},
}
return res, nil
}
func (e *impl) GetMetrics(
_ context.Context,
metricRequest *externalscaler.GetMetricsRequest,
) (*externalscaler.GetMetricsResponse, error) {
lggr := e.lggr.WithName("GetMetrics")
sor := metricRequest.ScaledObjectRef
namespacedName := k8s.NamespacedNameFromScaledObjectRef(sor)
metricName := MetricName(namespacedName)
scalerMetadata := sor.GetScalerMetadata()
httpScaledObjectName, ok := scalerMetadata[k8s.HTTPScaledObjectKey]
if !ok {
if scalerMetadata := sor.GetScalerMetadata(); scalerMetadata != nil {
if _, ok := scalerMetadata[keyInterceptorTargetPendingRequests]; ok {
return e.interceptorMetrics(metricName)
}
}
err := fmt.Errorf("unable to get HTTPScaledObject reference")
lggr.Error(err, "unable to get the linked HTTPScaledObject for ScaledObject", "name", sor.Name, "namespace", sor.Namespace)
return nil, err
}
httpso, err := e.httpsoInformer.Lister().HTTPScaledObjects(sor.Namespace).Get(httpScaledObjectName)
if err != nil {
lggr.Error(err, "unable to get HTTPScaledObject", "name", httpScaledObjectName, "namespace", sor.Namespace)
return nil, err
}
key := namespacedName.String()
count := e.pinger.counts()[key]
var metricValue int
if httpso.Spec.ScalingMetric != nil &&
httpso.Spec.ScalingMetric.Rate != nil {
metricValue = int(math.Ceil(count.RPS))
lggr.V(1).Info(fmt.Sprintf("%d rps for %s", metricValue, httpso.GetName()))
} else {
metricValue = count.Concurrency
lggr.V(1).Info(fmt.Sprintf("%d concurrent requests for %s", metricValue, httpso.GetName()))
}
res := &externalscaler.GetMetricsResponse{
MetricValues: []*externalscaler.MetricValue{
{
MetricName: metricName,
MetricValue: int64(metricValue),
},
},
}
return res, nil
}
func (e *impl) interceptorMetrics(metricName string) (*externalscaler.GetMetricsResponse, error) {
lggr := e.lggr.WithName("interceptorMetrics")
var count int64
for _, v := range e.pinger.counts() {
count += int64(v.Concurrency)
}
if err := strconv.ErrRange; count < 0 {
lggr.Error(err, "count overflowed", "value", count)
return nil, err
}
res := &externalscaler.GetMetricsResponse{
MetricValues: []*externalscaler.MetricValue{
{
MetricName: metricName,
MetricValue: count,
},
},
}
return res, nil
}