Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling host value of "interceptor" in GetMetricSpec #273

Merged
merged 5 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scaler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type config struct {
// UpdateRoutingTableDur is the duration between manual
// updates to the routing table.
UpdateRoutingTableDur time.Duration `envconfig:"KEDA_HTTP_SCALER_ROUTING_TABLE_UPDATE_DUR" default:"100ms"`
// This will be the 'Target Pending Requests' for the interceptor
TargetPendingRequestsInterceptor int `envconfig:"KEDA_HTTP_SCALER_TARGET_PENDING_REQUESTS_INTERCEPTOR" default:"100"`
}

func mustParseConfig() *config {
Expand Down
45 changes: 27 additions & 18 deletions scaler/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ func init() {
}

type impl struct {
lggr logr.Logger
pinger *queuePinger
routingTable routing.TableReader
targetMetric int64
lggr logr.Logger
pinger *queuePinger
routingTable routing.TableReader
targetMetric int64
targetMetricInterceptor int64
externalscaler.UnimplementedExternalScalerServer
}

Expand All @@ -33,12 +34,14 @@ func newImpl(
pinger *queuePinger,
routingTable routing.TableReader,
defaultTargetMetric int64,
defaultTargetMetricInterceptor int64,
) *impl {
return &impl{
lggr: lggr,
pinger: pinger,
routingTable: routingTable,
targetMetric: defaultTargetMetric,
lggr: lggr,
pinger: pinger,
routingTable: routingTable,
targetMetric: defaultTargetMetric,
targetMetricInterceptor: defaultTargetMetricInterceptor,
}
}

Expand Down Expand Up @@ -115,20 +118,26 @@ func (e *impl) GetMetricSpec(
lggr.Error(err, "no 'host' found in ScaledObject metadata")
return nil, err
}
target, err := e.routingTable.Lookup(host)
if err != nil {
lggr.Error(
err,
"error getting target for host",
"host",
host,
)
return nil, err
var targetPendingRequests int64
if host == "interceptor" {
targetPendingRequests = e.targetMetricInterceptor
} else {
target, err := e.routingTable.Lookup(host)
if err != nil {
lggr.Error(
err,
"error getting target for host",
"host",
host,
)
return nil, err
}
targetPendingRequests = int64(target.TargetPendingRequests)
}
metricSpecs := []*externalscaler.MetricSpec{
{
MetricName: host,
TargetSize: int64(target.TargetPendingRequests),
TargetSize: targetPendingRequests,
},
}

Expand Down
11 changes: 6 additions & 5 deletions scaler/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestIsActive(t *testing.T) {
pinger,
table,
123,
200,
)
res, err := hdl.IsActive(
ctx,
Expand Down Expand Up @@ -81,7 +82,7 @@ func TestGetMetricSpec(t *testing.T) {
))
ticker, pinger := newFakeQueuePinger(ctx, lggr)
defer ticker.Stop()
hdl := newImpl(lggr, pinger, table, 123)
hdl := newImpl(lggr, pinger, table, 123, 200)
meta := map[string]string{
"host": host,
"targetPendingRequests": strconv.Itoa(int(target)),
Expand Down Expand Up @@ -110,7 +111,7 @@ func TestGetMetricsMissingHostInMetadata(t *testing.T) {
table := routing.NewTable()
ticker, pinger := newFakeQueuePinger(ctx, lggr)
defer ticker.Stop()
hdl := newImpl(lggr, pinger, table, 123)
hdl := newImpl(lggr, pinger, table, 123, 200)

// no 'host' in the ScalerObjectRef's metadata field
res, err := hdl.GetMetrics(ctx, req)
Expand All @@ -137,7 +138,7 @@ func TestGetMetricsMissingHostInQueue(t *testing.T) {
table := routing.NewTable()
ticker, pinger := newFakeQueuePinger(ctx, lggr)
defer ticker.Stop()
hdl := newImpl(lggr, pinger, table, 123)
hdl := newImpl(lggr, pinger, table, 123, 200)

req := &externalscaler.GetMetricsRequest{
ScaledObjectRef: &externalscaler.ScaledObjectRef{},
Expand Down Expand Up @@ -212,7 +213,7 @@ func TestGetMetricsHostFoundInQueueCounts(t *testing.T) {
// first tick
time.Sleep(50 * time.Millisecond)

hdl := newImpl(lggr, pinger, table, 123)
hdl := newImpl(lggr, pinger, table, 123, 200)
res, err := hdl.GetMetrics(ctx, req)
r.NoError(err)
r.NotNil(res)
Expand Down Expand Up @@ -284,7 +285,7 @@ func TestGetMetricsInterceptorReturnsAggregate(t *testing.T) {
// first tick
time.Sleep(tickDur * 5)

hdl := newImpl(lggr, pinger, table, 123)
hdl := newImpl(lggr, pinger, table, 123, 200)
res, err := hdl.GetMetrics(ctx, req)
r.NoError(err)
r.NotNil(res)
Expand Down
4 changes: 4 additions & 0 deletions scaler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func main() {
svcName := cfg.TargetService
targetPortStr := fmt.Sprintf("%d", cfg.TargetPort)
targetPendingRequests := cfg.TargetPendingRequests
targetPendingRequestsInterceptor := cfg.TargetPendingRequestsInterceptor

k8sCl, _, err := k8s.NewClientset()
if err != nil {
Expand Down Expand Up @@ -69,6 +70,7 @@ func main() {
pinger,
table,
int64(targetPendingRequests),
int64(targetPendingRequestsInterceptor),
)
})

Expand Down Expand Up @@ -106,6 +108,7 @@ func startGrpcServer(
pinger *queuePinger,
routingTable *routing.Table,
targetPendingRequests int64,
targetPendingRequestsInterceptor int64,
) error {

addr := fmt.Sprintf("0.0.0.0:%d", port)
Expand All @@ -123,6 +126,7 @@ func startGrpcServer(
pinger,
routingTable,
targetPendingRequests,
targetPendingRequestsInterceptor,
),
)
reflection.Register(grpcServer)
Expand Down