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

Fix metric name passed to external scaler in GetMetrics call. #2890

Merged
merged 7 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion pkg/scalers/external_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,11 @@ func (s *externalScaler) GetMetrics(ctx context.Context, metricName string, metr
defer done()

// Remove the sX- prefix as the external scaler shouldn't have to know about it
metricName = strings.SplitN(metricName, "-", 2)[1]
metricName, err = GenerateMetricNameWithoutIndex(s.metadata.scalerIndex, metricName)
if err != nil {
return metrics, err
}

request := &pb.GetMetricsRequest{
MetricName: metricName,
ScaledObjectRef: &s.scaledObjectRef,
Expand Down
16 changes: 16 additions & 0 deletions pkg/scalers/scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package scalers
import (
"context"
"fmt"
"strings"
"time"

"k8s.io/api/autoscaling/v2beta2"
Expand Down Expand Up @@ -110,6 +111,21 @@ func GenerateMetricNameWithIndex(scalerIndex int, metricName string) string {
return fmt.Sprintf("s%d-%s", scalerIndex, metricName)
}

// GenerateMetricNameWithoutIndex removes the index prefix from the metric name
func GenerateMetricNameWithoutIndex(scalerIndex int, metricName string) (string, error) {
v-shenoy marked this conversation as resolved.
Show resolved Hide resolved
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", scalerIndex) {
return "", fmt.Errorf("metric name contains incorrect index prefix")
}

return metricNameWithoutIndex, nil
}

// GetMetricTargetType helps getting the metric target type of the scaler
func GetMetricTargetType(config *ScalerConfig) (v2beta2.MetricTargetType, error) {
switch config.MetricType {
Expand Down
35 changes: 35 additions & 0 deletions pkg/scalers/scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,38 @@ func TestGetMetricTarget(t *testing.T) {
})
}
}

func TestGenerateMetricNameWithoutIndex(t *testing.T) {
cases := []struct {
scalerIndex int
metricName string
expectedMetricNameWithoutIndexPrefix string
isError bool
}{
// Proper input
{scalerIndex: 0, metricName: "s0-metricName", expectedMetricNameWithoutIndexPrefix: "metricName", isError: false},
// Incorrect index prefix
{scalerIndex: 1, metricName: "s0-metricName", expectedMetricNameWithoutIndexPrefix: "", isError: true},
// Incorrect index prefix
{scalerIndex: 0, metricName: "0-metricName", expectedMetricNameWithoutIndexPrefix: "", isError: true},
// No index prefix
{scalerIndex: 0, metricName: "metricName", expectedMetricNameWithoutIndexPrefix: "", isError: true},
v-shenoy marked this conversation as resolved.
Show resolved Hide resolved
}

for _, testCase := range cases {
metricName, err := GenerateMetricNameWithoutIndex(testCase.scalerIndex, testCase.metricName)
if err != nil && !testCase.isError {
t.Error("Expected success but got error", err)
}

if testCase.isError && err == nil {
t.Error("Expected error but got success")
}

if err == nil {
if metricName != testCase.expectedMetricNameWithoutIndexPrefix {
t.Errorf("Expected - %s, Got - %s", testCase.expectedMetricNameWithoutIndexPrefix, metricName)
}
}
}
}