diff --git a/config/crd/bases/keda.sh_clustertriggerauthentications.yaml b/config/crd/bases/keda.sh_clustertriggerauthentications.yaml index b19b0a93fd5..7048ec3f85c 100644 --- a/config/crd/bases/keda.sh_clustertriggerauthentications.yaml +++ b/config/crd/bases/keda.sh_clustertriggerauthentications.yaml @@ -1,10 +1,9 @@ - --- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.6.1 + controller-gen.kubebuilder.io/version: v0.9.0 creationTimestamp: null name: clustertriggerauthentications.keda.sh spec: @@ -218,9 +217,3 @@ spec: served: true storage: true subresources: {} -status: - acceptedNames: - kind: "" - plural: "" - conditions: [] - storedVersions: [] diff --git a/config/crd/bases/keda.sh_scaledjobs.yaml b/config/crd/bases/keda.sh_scaledjobs.yaml index d450a103847..2d9ef35f09c 100644 --- a/config/crd/bases/keda.sh_scaledjobs.yaml +++ b/config/crd/bases/keda.sh_scaledjobs.yaml @@ -1,10 +1,9 @@ - --- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.6.1 + controller-gen.kubebuilder.io/version: v0.9.0 creationTimestamp: null name: scaledjobs.keda.sh spec: @@ -5860,8 +5859,8 @@ spec: constraint. - DoNotSchedule (default) tells the scheduler not to schedule it. - ScheduleAnyway tells the scheduler to schedule the pod in any - location, but giving higher precedence to topologies - that would help reduce the skew. A constraint + location, but giving higher precedence to topologies + that would help reduce the skew. A constraint is considered "Unsatisfiable" for an incoming pod if and only if every possible node assignment for that pod would violate "MaxSkew" on some topology. @@ -6329,12 +6328,12 @@ spec: deleted when the pod is removed. \n Use this if: a) the volume is only needed while the pod runs, b) features of normal volumes like restoring from - snapshot or capacity tracking are needed, c) - the storage driver is specified through a storage + snapshot or capacity tracking are needed, c) the + storage driver is specified through a storage class, and d) the storage driver supports dynamic - volume provisioning through a PersistentVolumeClaim - (see EphemeralVolumeSource for more information - on the connection between this volume type and + volume provisioning through a PersistentVolumeClaim + (see EphemeralVolumeSource for more information + on the connection between this volume type and PersistentVolumeClaim). \n Use PersistentVolumeClaim or one of the vendor-specific APIs for volumes that persist for longer than the lifecycle of @@ -6449,13 +6448,13 @@ spec: differences between DataSource and DataSourceRef: * While DataSource only allows two specific types of - objects, DataSourceRef allows any + objects, DataSourceRef allows any non-core object, as well as PersistentVolumeClaim objects. * While DataSource ignores disallowed values (dropping them), - DataSourceRef preserves all values, + DataSourceRef preserves all values, and generates an error if a disallowed - value is specified. (Alpha) Using + value is specified. (Alpha) Using this field requires the AnyVolumeDataSource feature gate to be enabled.' properties: @@ -7701,9 +7700,3 @@ spec: storage: true subresources: status: {} -status: - acceptedNames: - kind: "" - plural: "" - conditions: [] - storedVersions: [] diff --git a/config/crd/bases/keda.sh_scaledobjects.yaml b/config/crd/bases/keda.sh_scaledobjects.yaml index e2e4130c2d7..02675086c70 100644 --- a/config/crd/bases/keda.sh_scaledobjects.yaml +++ b/config/crd/bases/keda.sh_scaledobjects.yaml @@ -1,8 +1,9 @@ +--- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.6.1 + controller-gen.kubebuilder.io/version: v0.9.0 creationTimestamp: null name: scaledobjects.keda.sh spec: @@ -138,10 +139,9 @@ spec: type: object scaleUp: description: 'scaleUp is scaling policy for scaling Up. - If not set, the default value is the higher of: * - increase no more than 4 pods per 60 seconds * double - the number of pods per 60 seconds No stabilization is - used.' + If not set, the default value is the higher of: * increase + no more than 4 pods per 60 seconds * double the number + of pods per 60 seconds No stabilization is used.' properties: policies: description: policies is a list of potential scaling @@ -370,9 +370,3 @@ spec: storage: true subresources: status: {} -status: - acceptedNames: - kind: "" - plural: "" - conditions: [] - storedVersions: [] diff --git a/config/crd/bases/keda.sh_triggerauthentications.yaml b/config/crd/bases/keda.sh_triggerauthentications.yaml index 310f8e04447..1430a6138ec 100644 --- a/config/crd/bases/keda.sh_triggerauthentications.yaml +++ b/config/crd/bases/keda.sh_triggerauthentications.yaml @@ -1,10 +1,9 @@ - --- apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata: annotations: - controller-gen.kubebuilder.io/version: v0.6.1 + controller-gen.kubebuilder.io/version: v0.9.0 creationTimestamp: null name: triggerauthentications.keda.sh spec: @@ -217,9 +216,3 @@ spec: served: true storage: true subresources: {} -status: - acceptedNames: - kind: "" - plural: "" - conditions: [] - storedVersions: [] diff --git a/config/rbac/role.yaml b/config/rbac/role.yaml index cc67337efde..1846281cdbf 100644 --- a/config/rbac/role.yaml +++ b/config/rbac/role.yaml @@ -1,4 +1,3 @@ - --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole diff --git a/pkg/scalers/metrics_api_scaler.go b/pkg/scalers/metrics_api_scaler.go index 0f378361d8d..bca088ea605 100644 --- a/pkg/scalers/metrics_api_scaler.go +++ b/pkg/scalers/metrics_api_scaler.go @@ -28,9 +28,10 @@ type metricsAPIScaler struct { } type metricsAPIScalerMetadata struct { - targetValue float64 - url string - valueLocation string + targetValue float64 + activationTargetValue float64 + url string + valueLocation string // apiKeyAuth enableAPIKeyAuth bool @@ -108,6 +109,15 @@ func parseMetricsAPIMetadata(config *ScalerConfig) (*metricsAPIScalerMetadata, e return nil, fmt.Errorf("no targetValue given in metadata") } + meta.activationTargetValue = 0 + if val, ok := config.TriggerMetadata["activationTargetValue"]; ok { + activationTargetValue, err := strconv.ParseFloat(val, 64) + if err != nil { + return nil, fmt.Errorf("targetValue parsing error %s", err.Error()) + } + meta.activationTargetValue = activationTargetValue + } + if val, ok := config.TriggerMetadata["url"]; ok { meta.url = val } else { @@ -246,7 +256,7 @@ func (s *metricsAPIScaler) IsActive(ctx context.Context) (bool, error) { return false, err } - return v > 0.0, nil + return v > s.metadata.activationTargetValue, nil } // GetMetricSpecForScaling returns the MetricSpec for the Horizontal Pod Autoscaler diff --git a/pkg/scalers/metrics_api_scaler_test.go b/pkg/scalers/metrics_api_scaler_test.go index 5ad553a506f..755550d29cd 100644 --- a/pkg/scalers/metrics_api_scaler_test.go +++ b/pkg/scalers/metrics_api_scaler_test.go @@ -24,6 +24,8 @@ var testMetricsAPIMetadata = []metricsAPIMetadataTestData{ {metadata: map[string]string{"url": "http://dummy:1230/api/v1/", "valueLocation": "metric.test", "targetValue": "42"}, raisesError: false}, // Target not an int {metadata: map[string]string{"url": "http://dummy:1230/api/v1/", "valueLocation": "metric", "targetValue": "aa"}, raisesError: true}, + // Activation target not an int + {metadata: map[string]string{"url": "http://dummy:1230/api/v1/", "valueLocation": "metric", "targetValue": "1", "activationTargetValue": "aa"}, raisesError: true}, // Missing metric name {metadata: map[string]string{"url": "http://dummy:1230/api/v1/", "targetValue": "aa"}, raisesError: true}, // Missing url diff --git a/tests/scalers/metrics-api-trigger-auth.test.ts b/tests/scalers/metrics-api-trigger-auth.test.ts deleted file mode 100644 index 395e11047fb..00000000000 --- a/tests/scalers/metrics-api-trigger-auth.test.ts +++ /dev/null @@ -1,205 +0,0 @@ -import * as sh from 'shelljs' -import test from 'ava' -import { createNamespace, waitForDeploymentReplicaCount } from './helpers' -import * as tmp from 'tmp' -import * as fs from 'fs' - -const testNamespace = 'metrics-api-trigger-test' -const maxReplicaCount = 2 -const endpoint = `http://metrics-entrypoint.${testNamespace}.svc.cluster.local:8080/api` - -test.before((t) => { - sh.config.silent = true - createDeployment(t) -}) - -test.serial('Deployment should have 0 replicas on start', async (t) => { - t.true( - await waitForDeploymentReplicaCount(0, 'test-deployment', testNamespace, 10, 5000), - 'Replica count should start out as 0' - ) -}) - -test.serial( - `Deployment should scale to ${maxReplicaCount} when target value > 5 then back to 0`, - async (t) => { - updateMetricValue(10) - t.true( - await waitForDeploymentReplicaCount( - maxReplicaCount, - 'test-deployment', - testNamespace, - 20, - 5000 - ), - `Replica count should be ${maxReplicaCount}` - ) - - updateMetricValue(0) - - t.true( - await waitForDeploymentReplicaCount(0, 'test-deployment', testNamespace, 32, 10000), - 'Replica count should be 0 after 4 minutes' - ) - } -) - -test.after.always.cb('clean up metrics-api deployment', (t) => { - const resources = [ - 'scaledobject.keda.sh/test-scaledobject', - 'triggerauthentication.keda.sh/keda-metric-api-creds', - ] - - for (const resource of resources) { - sh.exec(`kubectl delete ${resource} --namespace ${testNamespace}`) - } - sh.exec(`kubectl delete namespace ${testNamespace}`) - t.end() -}) - -function createDeployment(t) { - const tmpFile = tmp.fileSync() - fs.writeFileSync( - tmpFile.name, - deployYaml - .replace('{{METRIC_SERVER_ENDPOINT}}', endpoint + '/basic/value') - .replace('{{MAX_REPLICA_COUNT}}', maxReplicaCount.toString()) - ) - - createNamespace(testNamespace) - sh.exec(`kubectl apply -f ${tmpFile.name} --namespace ${testNamespace}`) -} - -function updateMetricValue(value: number) { - sh.exec(`kubectl delete jobs/update-metric-value --namespace ${testNamespace}`) - - const tmpFile = tmp.fileSync() - fs.writeFileSync( - tmpFile.name, - updateMetricValueYaml - .replace('{{METRIC_SERVER_ENDPOINT}}', endpoint) - .replace('{{VALUE}}', value.toString()) - ) - sh.exec(`kubectl apply -f ${tmpFile.name} --namespace ${testNamespace}`) -} - -const updateMetricValueYaml = `apiVersion: batch/v1 -kind: Job -metadata: - name: update-metric-value -spec: - template: - spec: - containers: - - name: curl-client - image: curlimages/curl - imagePullPolicy: Always - command: ["curl", "-X", "POST", "{{METRIC_SERVER_ENDPOINT}}/value/{{VALUE}}"] - restartPolicy: Never` - -const deployYaml = `apiVersion: v1 -kind: Secret -metadata: - name: metrics-secrets -stringData: - username: "user" - password: "SECRET" ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: test-deployment -spec: - selector: - matchLabels: - app: testapp - replicas: 0 - template: - metadata: - labels: - app: testapp - spec: - containers: - - name: nginx - image: nginx:1.14.2 - ports: - - containerPort: 80 ---- -apiVersion: keda.sh/v1alpha1 -kind: TriggerAuthentication -metadata: - name: keda-metric-api-creds -spec: - secretTargetRef: - - parameter: username - name: metrics-secrets - key: username - - parameter: password - name: metrics-secrets - key: password ---- -apiVersion: keda.sh/v1alpha1 -kind: ScaledObject -metadata: - name: metric-api-scaledobject - labels: - deploymentName: http-scaled -spec: - maxReplicaCount: {{MAX_REPLICA_COUNT}} - scaleTargetRef: - name: test-deployment - triggers: - - type: metrics-api - metadata: - targetValue: "5" - url: "{{METRIC_SERVER_ENDPOINT}}" - valueLocation: 'value' - authMode: "basic" - method: "query" - authenticationRef: - name: keda-metric-api-creds ---- -apiVersion: v1 -kind: Service -metadata: - name: metrics-entrypoint -spec: - selector: - app: metrics - ports: - - port: 8080 - targetPort: 8080 ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: metrics-deployment - labels: - app: metrics -spec: - replicas: 1 - selector: - matchLabels: - app: metrics - template: - metadata: - labels: - app: metrics - spec: - containers: - - name: metrics - image: ghcr.io/kedacore/tests-metrics-api:latest - ports: - - containerPort: 8080 - imagePullPolicy: Always - env: - - name: AUTH_USERNAME - valueFrom: - secretKeyRef: - name: metrics-secrets - key: username - - name: AUTH_PASSWORD - valueFrom: - secretKeyRef: - name: metrics-secrets - key: password` diff --git a/tests/scalers/metrics-api.test.ts b/tests/scalers/metrics-api.test.ts deleted file mode 100644 index d366787f62d..00000000000 --- a/tests/scalers/metrics-api.test.ts +++ /dev/null @@ -1,186 +0,0 @@ -import * as sh from 'shelljs' -import test from 'ava' -import { createNamespace, waitForDeploymentReplicaCount } from './helpers' -import * as tmp from 'tmp' -import * as fs from 'fs' - -const testNamespace = 'metrics-api-test' -const maxReplicaCount = 2 -const endpoint = `http://metrics-entrypoint.${testNamespace}.svc.cluster.local:8080/api/value` - -test.before((t) => { - sh.config.silent = true - createDeployment(t) -}) - -test.serial('Deployment should have 0 replicas on start', async (t) => { - t.true( - await waitForDeploymentReplicaCount(0, 'test-deployment', testNamespace, 10, 5000), - 'Replica count should start out as 0' - ) -}) - -test.serial( - `Deployment should scale to ${maxReplicaCount} when target value > 5 then back to 0`, - async (t) => { - updateMetricValue(10) - t.true( - await waitForDeploymentReplicaCount( - maxReplicaCount, - 'test-deployment', - testNamespace, - 10, - 5000 - ), - `Replica count should be ${maxReplicaCount}` - ) - - updateMetricValue(0) - t.true( - await waitForDeploymentReplicaCount(0, 'test-deployment', testNamespace, 32, 10000), - 'Replica count should be 0 after 4 minutes' - ) - } -) - -test.after.always.cb('clean up metrics-api deployment', (t) => { - const resources = ['scaledobject.keda.sh/test-scaledobject'] - - for (const resource of resources) { - sh.exec(`kubectl delete ${resource} --namespace ${testNamespace}`) - } - sh.exec(`kubectl delete namespace ${testNamespace}`) - t.end() -}) - -function createDeployment(t) { - const tmpFile = tmp.fileSync() - fs.writeFileSync( - tmpFile.name, - deployYaml - .replace('{{METRIC_SERVER_ENDPOINT}}', endpoint) - .replace('{{MAX_REPLICA_COUNT}}', maxReplicaCount.toString()) - ) - - createNamespace(testNamespace) - sh.exec(`kubectl apply -f ${tmpFile.name} --namespace ${testNamespace}`) -} - -function updateMetricValue(value: number) { - sh.exec(`kubectl delete jobs/update-metric-value --namespace ${testNamespace}`) - - const tmpFile = tmp.fileSync() - fs.writeFileSync( - tmpFile.name, - updateMetricValueYaml - .replace('{{METRIC_SERVER_ENDPOINT}}', endpoint) - .replace('{{VALUE}}', value.toString()) - ) - - sh.exec(`kubectl apply -f ${tmpFile.name} --namespace ${testNamespace}`) -} - -const updateMetricValueYaml = `apiVersion: batch/v1 -kind: Job -metadata: - name: update-metric-value -spec: - template: - spec: - containers: - - name: curl-client - image: curlimages/curl - imagePullPolicy: Always - command: ["curl", "-X", "POST", "{{METRIC_SERVER_ENDPOINT}}/{{VALUE}}"] - restartPolicy: Never` - -const deployYaml = `apiVersion: apps/v1 -kind: Deployment -metadata: - name: metrics-deployment - labels: - app: metrics -spec: - replicas: 1 - selector: - matchLabels: - app: metrics - template: - metadata: - labels: - app: metrics - spec: - containers: - - name: metrics - image: ghcr.io/kedacore/tests-metrics-api:latest - ports: - - containerPort: 8080 - envFrom: - - configMapRef: - name: metrics-config - - secretRef: - name: metrics-secrets - imagePullPolicy: Always ---- -apiVersion: v1 -kind: ConfigMap -metadata: - name: metrics-config -data: - AUTH_USERNAME: "user" ---- -apiVersion: v1 -kind: Secret -metadata: - name: metrics-secrets -stringData: - AUTH_PASSWORD: "SECRET" - AUTH_TOKEN: "qwerty1234567890" ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: test-deployment -spec: - selector: - matchLabels: - app: testapp - replicas: 0 - template: - metadata: - labels: - app: testapp - spec: - containers: - - name: nginx - image: nginx:1.14.2 - ports: - - containerPort: 80 ---- -apiVersion: keda.sh/v1alpha1 -kind: ScaledObject -metadata: - name: metric-api-scaledobject - labels: - deploymentName: http-scaled -spec: - maxReplicaCount: {{MAX_REPLICA_COUNT}} - scaleTargetRef: - name: test-deployment - triggers: - - type: metrics-api - metadata: - targetValue: "5" - url: "{{METRIC_SERVER_ENDPOINT}}" - valueLocation: 'value' ---- -apiVersion: v1 -kind: Service -metadata: - name: metrics-entrypoint -spec: - selector: - app: metrics - ports: - - port: 8080 - targetPort: 8080` diff --git a/tests/scalers_go/datadog/datadog_test.go b/tests/scalers_go/datadog/datadog_test.go index 27e936c463f..5d01db3779f 100644 --- a/tests/scalers_go/datadog/datadog_test.go +++ b/tests/scalers_go/datadog/datadog_test.go @@ -207,7 +207,6 @@ spec: maxReplicaCount: {{.MaxReplicaCount}} pollingInterval: 1 cooldownPeriod: 1 - triggers: advanced: horizontalPodAutoscalerConfig: behavior: diff --git a/tests/scalers_go/metrics_api/metrics_api_test.go b/tests/scalers_go/metrics_api/metrics_api_test.go new file mode 100644 index 00000000000..2e8fce18d44 --- /dev/null +++ b/tests/scalers_go/metrics_api/metrics_api_test.go @@ -0,0 +1,257 @@ +//go:build e2e +// +build e2e + +package metrics_api_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 = "metrics-api-test" +) + +var ( + testNamespace = fmt.Sprintf("%s-ns", testName) + deploymentName = fmt.Sprintf("%s-deployment", testName) + metricsServerDeploymentName = fmt.Sprintf("%s-metrics-server", testName) + servciceName = fmt.Sprintf("%s-service", testName) + triggerAuthName = fmt.Sprintf("%s-ta", testName) + scaledObjectName = fmt.Sprintf("%s-so", testName) + secretName = fmt.Sprintf("%s-secret", testName) + metricsServerEndpoint = fmt.Sprintf("http://%s.%s.svc.cluster.local:8080/api/value", servciceName, testNamespace) + minReplicaCount = 0 + maxReplicaCount = 2 +) + +type templateData struct { + TestNamespace string + DeploymentName string + MetricsServerDeploymentName string + MetricsServerEndpoint string + ServciceName string + ScaledObjectName string + TriggerAuthName string + SecretName string + MetricValue int + MinReplicaCount string + MaxReplicaCount string +} + +type templateValues map[string]string + +const ( + secretTemplate = `apiVersion: v1 +kind: Secret +metadata: + name: {{.SecretName}} + namespace: {{.TestNamespace}} +data: + AUTH_PASSWORD: U0VDUkVUCg== + AUTH_USERNAME: VVNFUgo= +` + + triggerAuthenticationTemplate = `apiVersion: keda.sh/v1alpha1 +kind: TriggerAuthentication +metadata: + name: {{.TriggerAuthName}} + namespace: {{.TestNamespace}} +spec: + secretTargetRef: + - parameter: username + name: {{.SecretName}} + key: AUTH_USERNAME + - parameter: password + name: {{.SecretName}} + key: AUTH_PASSWORD +` + + metricsServerdeploymentTemplate = ` +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{.MetricsServerDeploymentName}} + namespace: {{.TestNamespace}} + labels: + app: {{.MetricsServerDeploymentName}} +spec: + replicas: 1 + selector: + matchLabels: + app: {{.MetricsServerDeploymentName}} + template: + metadata: + labels: + app: {{.MetricsServerDeploymentName}} + spec: + containers: + - name: metrics + image: ghcr.io/kedacore/tests-metrics-api + ports: + - containerPort: 8080 + envFrom: + - secretRef: + name: {{.SecretName}} + imagePullPolicy: Always +` + + serviceTemplate = ` +apiVersion: v1 +kind: Service +metadata: + name: {{.ServciceName}} + namespace: {{.TestNamespace}} +spec: + selector: + app: {{.MetricsServerDeploymentName}} + ports: + - port: 8080 + targetPort: 8080 +` + + deploymentTemplate = ` +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: {{.DeploymentName}} + name: {{.DeploymentName}} + namespace: {{.TestNamespace}} +spec: + selector: + matchLabels: + app: {{.DeploymentName}} + replicas: 0 + template: + metadata: + labels: + app: {{.DeploymentName}} + spec: + containers: + - name: nginx + image: nginx:1.14.2 + ports: + - containerPort: 80 +` + + scaledObjectTemplate = ` +apiVersion: keda.sh/v1alpha1 +kind: ScaledObject +metadata: + name: {{.ScaledObjectName}} + namespace: {{.TestNamespace}} + labels: + app: {{.DeploymentName}} +spec: + scaleTargetRef: + name: {{.DeploymentName}} + minReplicaCount: {{.MinReplicaCount}} + maxReplicaCount: {{.MaxReplicaCount}} + cooldownPeriod: 1 + triggers: + - type: metrics-api + metadata: + targetValue: "5" + activationTargetValue: "20" + url: "{{.MetricsServerEndpoint}}" + valueLocation: 'value' + authMode: "basic" + method: "query" + authenticationRef: + name: {{.TriggerAuthName}} +` + 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, minReplicaCount, 180, 3), + "replica count should be %d after 3 minutes", minReplicaCount) + + // test scaling + testActivation(t, kc, data) + testScaleUp(t, kc, data) + testScaleDown(t, kc, data) + + // cleanup + DeleteKubernetesResources(t, kc, testNamespace, data, templates) +} + +func testActivation(t *testing.T, kc *kubernetes.Clientset, data templateData) { + t.Log("--- testing activation ---") + data.MetricValue = 10 + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) + + AssertReplicaCountNotChangeDuringTimePeriod(t, kc, deploymentName, testNamespace, minReplicaCount, 60) +} + +func testScaleUp(t *testing.T, kc *kubernetes.Clientset, data templateData) { + t.Log("--- testing scale up ---") + data.MetricValue = 50 + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, maxReplicaCount, 60, 3), + "replica count should be %d after 3 minutes", maxReplicaCount) +} + +func testScaleDown(t *testing.T, kc *kubernetes.Clientset, data templateData) { + t.Log("--- testing scale down ---") + data.MetricValue = 0 + KubectlApplyWithTemplate(t, data, "updateMetricTemplate", updateMetricTemplate) + + assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, minReplicaCount, 60, 3), + "replica count should be %d after 3 minutes", minReplicaCount) +} + +func getTemplateData() (templateData, map[string]string) { + return templateData{ + TestNamespace: testNamespace, + DeploymentName: deploymentName, + MetricsServerDeploymentName: metricsServerDeploymentName, + ServciceName: servciceName, + TriggerAuthName: triggerAuthName, + ScaledObjectName: scaledObjectName, + SecretName: secretName, + MetricsServerEndpoint: metricsServerEndpoint, + MinReplicaCount: fmt.Sprintf("%v", minReplicaCount), + MaxReplicaCount: fmt.Sprintf("%v", maxReplicaCount), + MetricValue: 0, + }, templateValues{ + "secretTemplate": secretTemplate, + "metricsServerdeploymentTemplate": metricsServerdeploymentTemplate, + "serviceTemplate": serviceTemplate, + "triggerAuthenticationTemplate": triggerAuthenticationTemplate, + "deploymentTemplate": deploymentTemplate, + "scaledObjectTemplate": scaledObjectTemplate, + } +}