From 198d13c3451e28dd24a57bf7f8a4117c101d2c57 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Tue, 30 May 2023 00:28:06 +0200 Subject: [PATCH 1/7] update current tests Signed-off-by: Jorge Turrado --- .../min_replica_sj/min_replica_sj_test.go | 63 +++++++++++++------ .../external_scaler_sj_test.go | 49 ++++++++++----- .../external_scaler_so_test.go | 55 +++++++++++----- 3 files changed, 117 insertions(+), 50 deletions(-) diff --git a/tests/internals/min_replica_sj/min_replica_sj_test.go b/tests/internals/min_replica_sj/min_replica_sj_test.go index 4e2bf94680f..1016efedbc3 100644 --- a/tests/internals/min_replica_sj/min_replica_sj_test.go +++ b/tests/internals/min_replica_sj/min_replica_sj_test.go @@ -22,11 +22,12 @@ const ( ) var ( - testNamespace = fmt.Sprintf("%s-ns", testName) - serviceName = fmt.Sprintf("%s-service", testName) - scalerName = fmt.Sprintf("%s-scaler", testName) - scaledJobName = fmt.Sprintf("%s-sj", testName) - iterationCount = 15 + testNamespace = fmt.Sprintf("%s-ns", testName) + serviceName = fmt.Sprintf("%s-service", testName) + scalerName = fmt.Sprintf("%s-scaler", testName) + scaledJobName = fmt.Sprintf("%s-sj", testName) + metricsServerEndpoint = fmt.Sprintf("http://%s.%s.svc.cluster.local:8080/api/value", serviceName, testNamespace) + iterationCount = 60 ) type templateData struct { @@ -34,6 +35,7 @@ type templateData struct { ServiceName string ScalerName string ScaledJobName string + MetricsServerEndpoint string MetricThreshold, MetricValue int MinReplicaCount, MaxReplicaCount int } @@ -48,7 +50,11 @@ metadata: spec: ports: - port: 6000 + name: grpc targetPort: 6000 + - port: 8080 + name: http + targetPort: 8080 selector: app: {{.ScalerName}} ` @@ -73,10 +79,11 @@ spec: spec: containers: - name: scaler - image: ghcr.io/kedacore/tests-external-scaler-e2e:latest + image: ghcr.io/kedacore/tests-external-scaler:latest imagePullPolicy: Always ports: - containerPort: 6000 + - containerPort: 8080 ` scaledJobTemplate = ` @@ -108,8 +115,23 @@ spec: metadata: scalerAddress: {{.ServiceName}}.{{.TestNamespace}}:6000 metricThreshold: "{{.MetricThreshold}}" - metricValue: "{{.MetricValue}}" ` + + updateMetricTemplate = `apiVersion: batch/v1 +kind: Job +metadata: + name: update-metric-value + namespace: {{.TestNamespace}} +spec: + ttlSecondsAfterFinished: 0 + template: + spec: + containers: + - name: curl-client + image: curlimages/curl + imagePullPolicy: Always + command: ["curl", "-X", "POST", "{{.MetricsServerEndpoint}}/{{.MetricValue}}"] + restartPolicy: Never` ) func TestMinReplicaCount(t *testing.T) { @@ -122,7 +144,7 @@ func TestMinReplicaCount(t *testing.T) { CreateKubernetesResources(t, kc, testNamespace, data, templates) - assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, minReplicaCount, iterationCount, 1), + assert.True(t, WaitForJobCount(t, kc, testNamespace, minReplicaCount, iterationCount, 1), "job count should be %d after %d iterations", minReplicaCount, iterationCount) testMinReplicaCountWithMetricValue(t, kc, data) @@ -140,9 +162,10 @@ func testMinReplicaCountWithMetricValue(t *testing.T, kc *kubernetes.Clientset, data.MetricValue = 1 KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) expectedTarget := data.MinReplicaCount + data.MetricValue - assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, expectedTarget, 15, 1), + assert.True(t, WaitForJobCount(t, kc, testNamespace, expectedTarget, iterationCount, 1), "job count should be %d after %d iterations", expectedTarget, iterationCount) } @@ -154,8 +177,9 @@ func testMinReplicaCountGreaterMaxReplicaCountScalesOnlyToMaxReplicaCount(t *tes data.MetricValue = 0 KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) - assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, data.MaxReplicaCount, 15, 1), + assert.True(t, WaitForJobCount(t, kc, testNamespace, data.MaxReplicaCount, iterationCount, 1), "job count should be %d after %d iterations", data.MaxReplicaCount, iterationCount) } @@ -167,21 +191,22 @@ func testMinReplicaCountWithMetricValueGreaterMaxReplicaCountScalesOnlyToMaxRepl data.MetricValue = 3 KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) - assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, data.MaxReplicaCount, 15, 1), + assert.True(t, WaitForJobCount(t, kc, testNamespace, data.MaxReplicaCount, iterationCount, 1), "job count should be %d after %d iterations", data.MaxReplicaCount, iterationCount) } func getTemplateData(minReplicaCount int, maxReplicaCount int, metricValue int) (templateData, []Template) { return templateData{ - TestNamespace: testNamespace, - ServiceName: serviceName, - ScalerName: scalerName, - ScaledJobName: scaledJobName, - MetricThreshold: 1, - MetricValue: metricValue, - MinReplicaCount: minReplicaCount, - MaxReplicaCount: maxReplicaCount, + TestNamespace: testNamespace, + ServiceName: serviceName, + ScalerName: scalerName, + ScaledJobName: scaledJobName, + MetricThreshold: 1, + MetricsServerEndpoint: metricsServerEndpoint, + MinReplicaCount: minReplicaCount, + MaxReplicaCount: maxReplicaCount, }, []Template{ {Name: "scalerTemplate", Config: scalerTemplate}, {Name: "serviceTemplate", Config: serviceTemplate}, diff --git a/tests/scalers/external_scaler_sj/external_scaler_sj_test.go b/tests/scalers/external_scaler_sj/external_scaler_sj_test.go index 595eb88a837..ccf846e579a 100644 --- a/tests/scalers/external_scaler_sj/external_scaler_sj_test.go +++ b/tests/scalers/external_scaler_sj/external_scaler_sj_test.go @@ -22,10 +22,11 @@ const ( ) var ( - testNamespace = fmt.Sprintf("%s-ns", testName) - serviceName = fmt.Sprintf("%s-service", testName) - scalerName = fmt.Sprintf("%s-scaler", testName) - scaledJobName = fmt.Sprintf("%s-sj", testName) + testNamespace = fmt.Sprintf("%s-ns", testName) + serviceName = fmt.Sprintf("%s-service", testName) + scalerName = fmt.Sprintf("%s-scaler", testName) + scaledJobName = fmt.Sprintf("%s-sj", testName) + metricsServerEndpoint = fmt.Sprintf("http://%s.%s.svc.cluster.local:8080/api/value", serviceName, testNamespace) ) type templateData struct { @@ -33,6 +34,7 @@ type templateData struct { ServiceName string ScalerName string ScaledJobName string + MetricsServerEndpoint string MetricThreshold, MetricValue int } @@ -46,7 +48,11 @@ metadata: spec: ports: - port: 6000 + name: grpc targetPort: 6000 + - port: 8080 + name: http + targetPort: 8080 selector: app: {{.ScalerName}} ` @@ -71,10 +77,11 @@ spec: spec: containers: - name: scaler - image: ghcr.io/kedacore/tests-external-scaler-e2e:latest + image: ghcr.io/kedacore/tests-external-scaler:latest imagePullPolicy: Always ports: - containerPort: 6000 + - containerPort: 8080 ` scaledJobTemplate = ` @@ -105,8 +112,22 @@ spec: metadata: scalerAddress: {{.ServiceName}}.{{.TestNamespace}}:6000 metricThreshold: "{{.MetricThreshold}}" - metricValue: "{{.MetricValue}}" ` + updateMetricTemplate = `apiVersion: batch/v1 +kind: Job +metadata: + name: update-metric-value + namespace: {{.TestNamespace}} +spec: + ttlSecondsAfterFinished: 0 + template: + spec: + containers: + - name: curl-client + image: curlimages/curl + imagePullPolicy: Always + command: ["curl", "-X", "POST", "{{.MetricsServerEndpoint}}/{{.MetricValue}}"] + restartPolicy: Never` ) func TestScaler(t *testing.T) { @@ -132,12 +153,12 @@ func TestScaler(t *testing.T) { func getTemplateData() (templateData, []Template) { return templateData{ - TestNamespace: testNamespace, - ServiceName: serviceName, - ScalerName: scalerName, - ScaledJobName: scaledJobName, - MetricThreshold: 10, - MetricValue: 0, + TestNamespace: testNamespace, + ServiceName: serviceName, + ScalerName: scalerName, + ScaledJobName: scaledJobName, + MetricThreshold: 10, + MetricsServerEndpoint: metricsServerEndpoint, }, []Template{ {Name: "scalerTemplate", Config: scalerTemplate}, {Name: "serviceTemplate", Config: serviceTemplate}, @@ -150,7 +171,7 @@ func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { t.Log("scaling to max replicas") data.MetricValue = data.MetricThreshold * 3 - KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) assert.True(t, WaitForJobCount(t, kc, testNamespace, 3, 60, 1), "job count should be 3 after 1 minute") @@ -161,7 +182,7 @@ func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { t.Log("scaling to idle replicas") data.MetricValue = 0 - KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) assert.True(t, WaitForJobCount(t, kc, testNamespace, 0, 60, 1), "job count should be 0 after 1 minute") diff --git a/tests/scalers/external_scaler_so/external_scaler_so_test.go b/tests/scalers/external_scaler_so/external_scaler_so_test.go index aec3c73b76f..c98fdf6343e 100644 --- a/tests/scalers/external_scaler_so/external_scaler_so_test.go +++ b/tests/scalers/external_scaler_so/external_scaler_so_test.go @@ -22,11 +22,12 @@ const ( ) var ( - testNamespace = fmt.Sprintf("%s-ns", testName) - serviceName = fmt.Sprintf("%s-service", testName) - deploymentName = fmt.Sprintf("%s-deployment", testName) - scalerName = fmt.Sprintf("%s-scaler", testName) - scaledObjectName = fmt.Sprintf("%s-so", testName) + testNamespace = fmt.Sprintf("%s-ns", testName) + serviceName = fmt.Sprintf("%s-service", testName) + deploymentName = fmt.Sprintf("%s-deployment", testName) + scalerName = fmt.Sprintf("%s-scaler", testName) + scaledObjectName = fmt.Sprintf("%s-so", testName) + metricsServerEndpoint = fmt.Sprintf("http://%s.%s.svc.cluster.local:8080/api/value", serviceName, testNamespace) ) type templateData struct { @@ -35,6 +36,7 @@ type templateData struct { DeploymentName string ScalerName string ScaledObjectName string + MetricsServerEndpoint string MetricThreshold, MetricValue int } @@ -48,7 +50,11 @@ metadata: spec: ports: - port: 6000 + name: grpc targetPort: 6000 + - port: 8080 + name: http + targetPort: 8080 selector: app: {{.ScalerName}} ` @@ -73,10 +79,11 @@ spec: spec: containers: - name: scaler - image: ghcr.io/kedacore/tests-external-scaler-e2e:latest + image: ghcr.io/kedacore/tests-external-scaler:latest imagePullPolicy: Always ports: - containerPort: 6000 + - containerPort: 8080 ` deploymentTemplate = ` @@ -121,8 +128,22 @@ spec: metadata: scalerAddress: {{.ServiceName}}.{{.TestNamespace}}:6000 metricThreshold: "{{.MetricThreshold}}" - metricValue: "{{.MetricValue}}" ` + updateMetricTemplate = `apiVersion: batch/v1 +kind: Job +metadata: + name: update-metric-value + namespace: {{.TestNamespace}} +spec: + ttlSecondsAfterFinished: 0 + template: + spec: + containers: + - name: curl-client + image: curlimages/curl + imagePullPolicy: Always + command: ["curl", "-X", "POST", "{{.MetricsServerEndpoint}}/{{.MetricValue}}"] + restartPolicy: Never` ) func TestScaler(t *testing.T) { @@ -151,13 +172,13 @@ func TestScaler(t *testing.T) { func getTemplateData() (templateData, []Template) { return templateData{ - TestNamespace: testNamespace, - ServiceName: serviceName, - DeploymentName: deploymentName, - ScalerName: scalerName, - ScaledObjectName: scaledObjectName, - MetricThreshold: 10, - MetricValue: 0, + TestNamespace: testNamespace, + ServiceName: serviceName, + DeploymentName: deploymentName, + ScalerName: scalerName, + ScaledObjectName: scaledObjectName, + MetricThreshold: 10, + MetricsServerEndpoint: metricsServerEndpoint, }, []Template{ {Name: "scalerTemplate", Config: scalerTemplate}, {Name: "serviceTemplate", Config: serviceTemplate}, @@ -171,14 +192,14 @@ func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { t.Log("scaling to min replicas") data.MetricValue = data.MetricThreshold - KubectlApplyWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate) + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 1, 60, 1), "replica count should be 1 after 1 minute") t.Log("scaling to max replicas") data.MetricValue = data.MetricThreshold * 2 - KubectlApplyWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate) + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 2, 60, 2), "replica count should be 2 after 2 minutes") @@ -189,7 +210,7 @@ func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { t.Log("scaling to idle replicas") data.MetricValue = 0 - KubectlApplyWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate) + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 0, 60, 2), "replica count should be 0 after 2 minutes") From 4130a58074e7bdb5d2ab5167ac83428f9c55dbff Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Tue, 30 May 2023 00:29:09 +0200 Subject: [PATCH 2/7] update changelog Signed-off-by: Jorge Turrado --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 127fc1c5eb0..c0ebac6a006 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,7 @@ New deprecation(s): ### Other +- **General**: Add e2e test for external push scaler ([#2698](https://github.com/kedacore/keda/pull/2698)) - **General**: Bump Golang to 1.20 ([#4517](https://github.com/kedacore/keda/issues/4517)) - **General**: Drop a transitive dependency on bou.ke/monkey ([#4364](https://github.com/kedacore/keda/issues/4364)) - **General**: Fix odd number of arguments passed as key-value pairs for logging ([#4368](https://github.com/kedacore/keda/issues/4368)) @@ -93,6 +94,7 @@ New deprecation(s): - **General**: Use default metrics provider from sigs.k8s.io/custom-metrics-apiserver ([#4473](https://github.com/kedacore/keda/pull/4473)) - **General**: Refactor several functions for Status & Conditions handling into pkg util functions ([#2906](https://github.com/kedacore/keda/pull/2906)) + ## v2.10.1 ### Fixes From c9533efc57ab6601fd6faa217552240305617dd0 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Tue, 30 May 2023 00:32:05 +0200 Subject: [PATCH 3/7] add external-push scaler Signed-off-by: Jorge Turrado --- .../external_push_scaler_test.go | 217 ++++++++++++++++++ 1 file changed, 217 insertions(+) create mode 100644 tests/scalers/external_push_scaler/external_push_scaler_test.go diff --git a/tests/scalers/external_push_scaler/external_push_scaler_test.go b/tests/scalers/external_push_scaler/external_push_scaler_test.go new file mode 100644 index 00000000000..7ed1a071801 --- /dev/null +++ b/tests/scalers/external_push_scaler/external_push_scaler_test.go @@ -0,0 +1,217 @@ +//go:build e2e +// +build e2e + +package external_push_scaler_test + +import ( + "fmt" + "testing" + + "github.com/joho/godotenv" + "github.com/stretchr/testify/assert" + "k8s.io/client-go/kubernetes" + + . "github.com/kedacore/keda/v2/tests/helper" +) + +// Load environment variables from .env file +var _ = godotenv.Load("../../.env") + +const ( + testName = "external-push-scaler-test" +) + +var ( + testNamespace = fmt.Sprintf("%s-ns", testName) + serviceName = fmt.Sprintf("%s-service", testName) + deploymentName = fmt.Sprintf("%s-deployment", testName) + scalerName = fmt.Sprintf("%s-scaler", testName) + scaledObjectName = fmt.Sprintf("%s-so", testName) + metricsServerEndpoint = fmt.Sprintf("http://%s.%s.svc.cluster.local:8080/api/value", serviceName, testNamespace) +) + +type templateData struct { + TestNamespace string + ServiceName string + DeploymentName string + ScalerName string + ScaledObjectName string + MetricsServerEndpoint string + MetricThreshold, MetricValue int +} + +const ( + serviceTemplate = ` +apiVersion: v1 +kind: Service +metadata: + name: {{.ServiceName}} + namespace: {{.TestNamespace}} +spec: + ports: + - port: 6000 + name: grpc + targetPort: 6000 + - port: 8080 + name: http + targetPort: 8080 + selector: + app: {{.ScalerName}} +` + + scalerTemplate = ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{.ScalerName}} + namespace: {{.TestNamespace}} + labels: + app: {{.ScalerName}} +spec: + replicas: 1 + selector: + matchLabels: + app: {{.ScalerName}} + template: + metadata: + labels: + app: {{.ScalerName}} + spec: + containers: + - name: scaler + image: ghcr.io/kedacore/tests-external-scaler:latest + imagePullPolicy: Always + ports: + - containerPort: 6000 + - containerPort: 8080 +` + + deploymentTemplate = ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{.DeploymentName}} + namespace: {{.TestNamespace}} + labels: + app: {{.DeploymentName}} +spec: + replicas: 0 + selector: + matchLabels: + app: {{.DeploymentName}} + template: + metadata: + labels: + app: {{.DeploymentName}} + spec: + containers: + - name: nginx + image: nginxinc/nginx-unprivileged +` + + scaledObjectTemplate = ` +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: {{.ScaledObjectName}} + namespace: {{.TestNamespace}} +spec: + scaleTargetRef: + name: {{.DeploymentName}} + pollingInterval: 5 + cooldownPeriod: 10 + idleReplicaCount: 0 + minReplicaCount: 1 + maxReplicaCount: 2 + triggers: + - type: external-push + metadata: + scalerAddress: {{.ServiceName}}.{{.TestNamespace}}:6000 + metricThreshold: "{{.MetricThreshold}}" +` + updateMetricTemplate = `apiVersion: batch/v1 +kind: Job +metadata: + name: update-metric-value + namespace: {{.TestNamespace}} +spec: + ttlSecondsAfterFinished: 0 + template: + spec: + containers: + - name: curl-client + image: curlimages/curl + imagePullPolicy: Always + command: ["curl", "-X", "POST", "{{.MetricsServerEndpoint}}/{{.MetricValue}}"] + restartPolicy: Never` +) + +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) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 0, 60, 1), + "replica count should be 0 after 1 minute") + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, scalerName, testNamespace, 1, 60, 1), + "replica count should be 1 after 1 minute") + + // test scaling + testScaleOut(t, kc, data) + testScaleIn(t, kc, data) + + // cleanup + DeleteKubernetesResources(t, testNamespace, data, templates) +} + +func getTemplateData() (templateData, []Template) { + return templateData{ + TestNamespace: testNamespace, + ServiceName: serviceName, + DeploymentName: deploymentName, + ScalerName: scalerName, + ScaledObjectName: scaledObjectName, + MetricThreshold: 10, + MetricsServerEndpoint: metricsServerEndpoint, + }, []Template{ + {Name: "scalerTemplate", Config: scalerTemplate}, + {Name: "serviceTemplate", Config: serviceTemplate}, + {Name: "deploymentTemplate", Config: deploymentTemplate}, + {Name: "scaledObjectTemplate", Config: scaledObjectTemplate}, + } +} + +func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { + t.Log("--- testing scale out ---") + + t.Log("scaling to min replicas") + data.MetricValue = data.MetricThreshold + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 1, 60, 1), + "replica count should be 1 after 1 minute") + + t.Log("scaling to max replicas") + data.MetricValue = data.MetricThreshold * 2 + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 2, 60, 2), + "replica count should be 2 after 2 minutes") +} + +func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { + t.Log("--- testing scale in ---") + + t.Log("scaling to idle replicas") + data.MetricValue = 0 + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 0, 60, 2), + "replica count should be 0 after 2 minutes") +} From 0e6b1de2d6b8e647a6bad25013d5b7ff969fae2f Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Tue, 30 May 2023 00:33:15 +0200 Subject: [PATCH 4/7] remove whiteline Signed-off-by: Jorge Turrado --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0ebac6a006..feea1d9686b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,7 +94,6 @@ New deprecation(s): - **General**: Use default metrics provider from sigs.k8s.io/custom-metrics-apiserver ([#4473](https://github.com/kedacore/keda/pull/4473)) - **General**: Refactor several functions for Status & Conditions handling into pkg util functions ([#2906](https://github.com/kedacore/keda/pull/2906)) - ## v2.10.1 ### Fixes From dde3895e4fec80cf652835c82aeb89f130bc3a81 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Tue, 30 May 2023 01:07:52 +0200 Subject: [PATCH 5/7] fix style Signed-off-by: Jorge Turrado --- tests/internals/min_replica_sj/min_replica_sj_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/internals/min_replica_sj/min_replica_sj_test.go b/tests/internals/min_replica_sj/min_replica_sj_test.go index 1016efedbc3..882694cd5a1 100644 --- a/tests/internals/min_replica_sj/min_replica_sj_test.go +++ b/tests/internals/min_replica_sj/min_replica_sj_test.go @@ -138,9 +138,8 @@ func TestMinReplicaCount(t *testing.T) { kc := GetKubernetesClient(t) minReplicaCount := 2 maxReplicaCount := 10 - metricValue := 0 - data, templates := getTemplateData(minReplicaCount, maxReplicaCount, metricValue) + data, templates := getTemplateData(minReplicaCount, maxReplicaCount) CreateKubernetesResources(t, kc, testNamespace, data, templates) @@ -197,7 +196,7 @@ func testMinReplicaCountWithMetricValueGreaterMaxReplicaCountScalesOnlyToMaxRepl "job count should be %d after %d iterations", data.MaxReplicaCount, iterationCount) } -func getTemplateData(minReplicaCount int, maxReplicaCount int, metricValue int) (templateData, []Template) { +func getTemplateData(minReplicaCount int, maxReplicaCount int) (templateData, []Template) { return templateData{ TestNamespace: testNamespace, ServiceName: serviceName, From 0dd6b916f5de1e95a25c8f756f528642b3326ffc Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Wed, 31 May 2023 13:21:24 +0200 Subject: [PATCH 6/7] added a helper function to monitor only scaledjobs Signed-off-by: Jorge Turrado --- tests/helper/helper.go | 11 +++++++++++ .../internals/min_replica_sj/min_replica_sj_test.go | 12 +++++++----- .../external_push_scaler_test.go | 4 +++- .../external_scaler_sj/external_scaler_sj_test.go | 7 ++++--- .../external_scaler_so/external_scaler_so_test.go | 4 +++- tests/scalers/mongodb/mongodb_test.go | 6 +++--- 6 files changed, 31 insertions(+), 13 deletions(-) diff --git a/tests/helper/helper.go b/tests/helper/helper.go index 6c685f933e9..67279b76178 100644 --- a/tests/helper/helper.go +++ b/tests/helper/helper.go @@ -299,7 +299,18 @@ func WaitForNamespaceDeletion(t *testing.T, nsName string) bool { return false } +func WaitForScaledJobCount(t *testing.T, kc *kubernetes.Clientset, scaledJobName, namespace string, + target, iterations, intervalSeconds int) bool { + + return waitForJobCount(t, kc, fmt.Sprintf("scaledjob.keda.sh/name=%s", scaledJobName), namespace, target, iterations, intervalSeconds) +} + func WaitForJobCount(t *testing.T, kc *kubernetes.Clientset, namespace string, + target, iterations, intervalSeconds int) bool { + return waitForJobCount(t, kc, "", namespace, target, iterations, intervalSeconds) +} + +func waitForJobCount(t *testing.T, kc *kubernetes.Clientset, selector, namespace string, target, iterations, intervalSeconds int) bool { for i := 0; i < iterations; i++ { jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), metav1.ListOptions{}) diff --git a/tests/internals/min_replica_sj/min_replica_sj_test.go b/tests/internals/min_replica_sj/min_replica_sj_test.go index 882694cd5a1..facb17260cd 100644 --- a/tests/internals/min_replica_sj/min_replica_sj_test.go +++ b/tests/internals/min_replica_sj/min_replica_sj_test.go @@ -123,7 +123,6 @@ metadata: name: update-metric-value namespace: {{.TestNamespace}} spec: - ttlSecondsAfterFinished: 0 template: spec: containers: @@ -143,7 +142,7 @@ func TestMinReplicaCount(t *testing.T) { CreateKubernetesResources(t, kc, testNamespace, data, templates) - assert.True(t, WaitForJobCount(t, kc, testNamespace, minReplicaCount, iterationCount, 1), + assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, minReplicaCount, iterationCount, 1), "job count should be %d after %d iterations", minReplicaCount, iterationCount) testMinReplicaCountWithMetricValue(t, kc, data) @@ -164,8 +163,9 @@ func testMinReplicaCountWithMetricValue(t *testing.T, kc *kubernetes.Clientset, KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) expectedTarget := data.MinReplicaCount + data.MetricValue - assert.True(t, WaitForJobCount(t, kc, testNamespace, expectedTarget, iterationCount, 1), + assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, expectedTarget, iterationCount, 1), "job count should be %d after %d iterations", expectedTarget, iterationCount) + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) } func testMinReplicaCountGreaterMaxReplicaCountScalesOnlyToMaxReplicaCount(t *testing.T, kc *kubernetes.Clientset, data templateData) { @@ -178,8 +178,9 @@ func testMinReplicaCountGreaterMaxReplicaCountScalesOnlyToMaxReplicaCount(t *tes KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) - assert.True(t, WaitForJobCount(t, kc, testNamespace, data.MaxReplicaCount, iterationCount, 1), + assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, data.MaxReplicaCount, iterationCount, 1), "job count should be %d after %d iterations", data.MaxReplicaCount, iterationCount) + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) } func testMinReplicaCountWithMetricValueGreaterMaxReplicaCountScalesOnlyToMaxReplicaCount(t *testing.T, kc *kubernetes.Clientset, data templateData) { @@ -192,8 +193,9 @@ func testMinReplicaCountWithMetricValueGreaterMaxReplicaCountScalesOnlyToMaxRepl KubectlApplyWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) - assert.True(t, WaitForJobCount(t, kc, testNamespace, data.MaxReplicaCount, iterationCount, 1), + assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, data.MaxReplicaCount, iterationCount, 1), "job count should be %d after %d iterations", data.MaxReplicaCount, iterationCount) + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) } func getTemplateData(minReplicaCount int, maxReplicaCount int) (templateData, []Template) { diff --git a/tests/scalers/external_push_scaler/external_push_scaler_test.go b/tests/scalers/external_push_scaler/external_push_scaler_test.go index 7ed1a071801..4d312e62992 100644 --- a/tests/scalers/external_push_scaler/external_push_scaler_test.go +++ b/tests/scalers/external_push_scaler/external_push_scaler_test.go @@ -135,7 +135,6 @@ metadata: name: update-metric-value namespace: {{.TestNamespace}} spec: - ttlSecondsAfterFinished: 0 template: spec: containers: @@ -196,6 +195,7 @@ func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 1, 60, 1), "replica count should be 1 after 1 minute") + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) t.Log("scaling to max replicas") data.MetricValue = data.MetricThreshold * 2 @@ -203,6 +203,7 @@ func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 2, 60, 2), "replica count should be 2 after 2 minutes") + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) } func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { @@ -214,4 +215,5 @@ func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 0, 60, 2), "replica count should be 0 after 2 minutes") + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) } diff --git a/tests/scalers/external_scaler_sj/external_scaler_sj_test.go b/tests/scalers/external_scaler_sj/external_scaler_sj_test.go index ccf846e579a..acf0c30483e 100644 --- a/tests/scalers/external_scaler_sj/external_scaler_sj_test.go +++ b/tests/scalers/external_scaler_sj/external_scaler_sj_test.go @@ -119,7 +119,6 @@ metadata: name: update-metric-value namespace: {{.TestNamespace}} spec: - ttlSecondsAfterFinished: 0 template: spec: containers: @@ -173,8 +172,9 @@ func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { data.MetricValue = data.MetricThreshold * 3 KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) - assert.True(t, WaitForJobCount(t, kc, testNamespace, 3, 60, 1), + assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, 3, 60, 1), "job count should be 3 after 1 minute") + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) } func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { @@ -184,6 +184,7 @@ func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { data.MetricValue = 0 KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) - assert.True(t, WaitForJobCount(t, kc, testNamespace, 0, 60, 1), + assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, 0, 60, 1), "job count should be 0 after 1 minute") + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) } diff --git a/tests/scalers/external_scaler_so/external_scaler_so_test.go b/tests/scalers/external_scaler_so/external_scaler_so_test.go index c98fdf6343e..6557ce5f59b 100644 --- a/tests/scalers/external_scaler_so/external_scaler_so_test.go +++ b/tests/scalers/external_scaler_so/external_scaler_so_test.go @@ -135,7 +135,6 @@ metadata: name: update-metric-value namespace: {{.TestNamespace}} spec: - ttlSecondsAfterFinished: 0 template: spec: containers: @@ -196,6 +195,7 @@ func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 1, 60, 1), "replica count should be 1 after 1 minute") + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) t.Log("scaling to max replicas") data.MetricValue = data.MetricThreshold * 2 @@ -203,6 +203,7 @@ func testScaleOut(t *testing.T, kc *kubernetes.Clientset, data templateData) { assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 2, 60, 2), "replica count should be 2 after 2 minutes") + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) } func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { @@ -214,4 +215,5 @@ func testScaleIn(t *testing.T, kc *kubernetes.Clientset, data templateData) { assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, 0, 60, 2), "replica count should be 0 after 2 minutes") + KubectlDeleteWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) } diff --git a/tests/scalers/mongodb/mongodb_test.go b/tests/scalers/mongodb/mongodb_test.go index 58c4c11cc81..ebfe22925f2 100644 --- a/tests/scalers/mongodb/mongodb_test.go +++ b/tests/scalers/mongodb/mongodb_test.go @@ -118,7 +118,7 @@ func TestScaler(t *testing.T) { mongoPod := setupMongo(t, kc) CreateKubernetesResources(t, kc, testNamespace, data, templates) - assert.True(t, WaitForJobCount(t, kc, testNamespace, 0, 60, 1), + assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, 0, 60, 1), "job count should be 0 after 1 minute") // test scaling @@ -194,7 +194,7 @@ func testActivation(t *testing.T, kc *kubernetes.Clientset, mongoPod string) { _, err := ExecuteCommand(fmt.Sprintf("kubectl exec %s -n %s -- mongosh --eval '%s'", mongoPod, mongoNamespace, insertCmd)) assert.NoErrorf(t, err, "cannot insert mongo records - %s", err) time.Sleep(time.Second * 60) - assert.True(t, WaitForJobCount(t, kc, testNamespace, 0, 60, 1), + assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, 0, 60, 1), "job count should be 0 after 1 minute") } @@ -210,7 +210,7 @@ func testScaleOut(t *testing.T, kc *kubernetes.Clientset, mongoPod string) { _, err := ExecuteCommand(fmt.Sprintf("kubectl exec %s -n %s -- mongosh --eval '%s'", mongoPod, mongoNamespace, insertCmd)) assert.NoErrorf(t, err, "cannot insert mongo records - %s", err) - assert.True(t, WaitForJobCount(t, kc, testNamespace, 5, 60, 1), + assert.True(t, WaitForScaledJobCount(t, kc, scaledJobName, testNamespace, 5, 60, 1), "job count should be 5 after 1 minute") } From c10f41893ba9cce3aafaac92350f6581035d2386 Mon Sep 17 00:00:00 2001 From: Jorge Turrado Date: Wed, 31 May 2023 13:34:59 +0200 Subject: [PATCH 7/7] commit missing changes Signed-off-by: Jorge Turrado --- tests/helper/helper.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/helper/helper.go b/tests/helper/helper.go index 67279b76178..12bc9e1cf30 100644 --- a/tests/helper/helper.go +++ b/tests/helper/helper.go @@ -301,7 +301,6 @@ func WaitForNamespaceDeletion(t *testing.T, nsName string) bool { func WaitForScaledJobCount(t *testing.T, kc *kubernetes.Clientset, scaledJobName, namespace string, target, iterations, intervalSeconds int) bool { - return waitForJobCount(t, kc, fmt.Sprintf("scaledjob.keda.sh/name=%s", scaledJobName), namespace, target, iterations, intervalSeconds) } @@ -313,7 +312,9 @@ func WaitForJobCount(t *testing.T, kc *kubernetes.Clientset, namespace string, func waitForJobCount(t *testing.T, kc *kubernetes.Clientset, selector, namespace string, target, iterations, intervalSeconds int) bool { for i := 0; i < iterations; i++ { - jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), metav1.ListOptions{}) + jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), metav1.ListOptions{ + LabelSelector: selector, + }) count := len(jobList.Items) t.Logf("Waiting for job count to hit target. Namespace - %s, Current - %d, Target - %d",