-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Metrics endpoint returns correct HPA value (#3584)
- Loading branch information
Jorge Turrado Ferrero
authored
Aug 25, 2022
1 parent
40da2e2
commit ef48177
Showing
6 changed files
with
193 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
tests/internals/prometheus_metrics/prometheus_metrics_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package prometheus_metrics_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/prometheus/common/expfmt" | ||
"github.com/stretchr/testify/assert" | ||
|
||
. "github.com/kedacore/keda/v2/tests/helper" | ||
) | ||
|
||
const ( | ||
testName = "prometheus-metrics-test" | ||
) | ||
|
||
var ( | ||
testNamespace = fmt.Sprintf("%s-ns", testName) | ||
deploymentName = fmt.Sprintf("%s-deployment", testName) | ||
monitoredDeploymentName = fmt.Sprintf("%s-monitored", testName) | ||
scaledObjectName = fmt.Sprintf("%s-so", testName) | ||
clientName = fmt.Sprintf("%s-client", testName) | ||
) | ||
|
||
type templateData struct { | ||
TestNamespace string | ||
DeploymentName string | ||
ScaledObjectName string | ||
MonitoredDeploymentName string | ||
ClientName string | ||
} | ||
|
||
const ( | ||
monitoredDeploymentTemplate = ` | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: {{.MonitoredDeploymentName}} | ||
namespace: {{.TestNamespace}} | ||
labels: | ||
app: {{.MonitoredDeploymentName}} | ||
spec: | ||
replicas: 4 | ||
selector: | ||
matchLabels: | ||
app: {{.MonitoredDeploymentName}} | ||
template: | ||
metadata: | ||
labels: | ||
app: {{.MonitoredDeploymentName}} | ||
spec: | ||
containers: | ||
- name: {{.MonitoredDeploymentName}} | ||
image: nginx | ||
` | ||
|
||
deploymentTemplate = ` | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: {{.DeploymentName}} | ||
namespace: {{.TestNamespace}} | ||
labels: | ||
app: {{.DeploymentName}} | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: {{.DeploymentName}} | ||
template: | ||
metadata: | ||
labels: | ||
app: {{.DeploymentName}} | ||
spec: | ||
containers: | ||
- name: {{.DeploymentName}} | ||
image: nginx | ||
` | ||
|
||
scaledObjectTemplate = ` | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledObject | ||
metadata: | ||
name: {{.ScaledObjectName}} | ||
namespace: {{.TestNamespace}} | ||
spec: | ||
scaleTargetRef: | ||
name: {{.DeploymentName}} | ||
pollingInterval: 5 | ||
idleReplicaCount: 0 | ||
minReplicaCount: 1 | ||
maxReplicaCount: 2 | ||
cooldownPeriod: 10 | ||
triggers: | ||
- type: kubernetes-workload | ||
metadata: | ||
podSelector: 'app={{.MonitoredDeploymentName}}' | ||
value: '1' | ||
` | ||
|
||
clientTemplate = ` | ||
apiVersion: v1 | ||
kind: Pod | ||
metadata: | ||
name: {{.ClientName}} | ||
namespace: {{.TestNamespace}} | ||
spec: | ||
containers: | ||
- name: {{.ClientName}} | ||
image: curlimages/curl | ||
command: | ||
- sh | ||
- -c | ||
- "exec tail -f /dev/null"` | ||
) | ||
|
||
func TestScaler(t *testing.T) { | ||
// setup | ||
t.Log("--- setting up ---") | ||
|
||
// Create kubernetes resources | ||
kc := GetKubernetesClient(t) | ||
data, templates := getTemplateData() | ||
|
||
CreateKubernetesResources(t, kc, testNamespace, data, templates) | ||
|
||
// scaling to max replica count to ensure the counter is registered before we test it | ||
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 2, 60, 2), | ||
"replica count should be 2 after 2 minute") | ||
|
||
testHPAScalerMetricValue(t) | ||
|
||
// cleanup | ||
DeleteKubernetesResources(t, kc, testNamespace, data, templates) | ||
} | ||
|
||
func getTemplateData() (templateData, []Template) { | ||
return templateData{ | ||
TestNamespace: testNamespace, | ||
DeploymentName: deploymentName, | ||
ScaledObjectName: scaledObjectName, | ||
MonitoredDeploymentName: monitoredDeploymentName, | ||
ClientName: clientName, | ||
}, []Template{ | ||
{Name: "deploymentTemplate", Config: deploymentTemplate}, | ||
{Name: "monitoredDeploymentTemplate", Config: monitoredDeploymentTemplate}, | ||
{Name: "scaledObjectTemplate", Config: scaledObjectTemplate}, | ||
{Name: "clientTemplate", Config: clientTemplate}, | ||
} | ||
} | ||
|
||
func testHPAScalerMetricValue(t *testing.T) { | ||
t.Log("--- testing hpa scaler metric value ---") | ||
|
||
out, _, err := ExecCommandOnSpecificPod(t, clientName, testNamespace, "curl --insecure http://keda-metrics-apiserver.keda:9022/metrics") | ||
assert.NoErrorf(t, err, "cannot execute command - %s", err) | ||
|
||
parser := expfmt.TextParser{} | ||
// Ensure EOL | ||
reader := strings.NewReader(strings.ReplaceAll(out, "\r\n", "\n")) | ||
family, err := parser.TextToMetricFamilies(reader) | ||
assert.NoErrorf(t, err, "cannot parse metrics - %s", err) | ||
|
||
if val, ok := family["keda_metrics_adapter_scaler_metrics_value"]; ok { | ||
var found bool | ||
metrics := val.GetMetric() | ||
for _, metric := range metrics { | ||
labels := metric.GetLabel() | ||
for _, label := range labels { | ||
if *label.Name == "scaledObject" && *label.Value == scaledObjectName { | ||
assert.Equal(t, float64(4), *metric.Gauge.Value) | ||
found = true | ||
} | ||
} | ||
} | ||
assert.Equal(t, true, found) | ||
} else { | ||
t.Errorf("metric not available") | ||
} | ||
} |