forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Provide support for explicitly pausing autoscaling of ScaledJobs
--- Accreditation --- Original PR: kedacore#3828 Borrows from the work originally implemented by: https://github.com/keegancwinchester Note: I would have loved to have pulled the commit from the original branch, but I could not be able to. Documentation MR by original implementor: kedacore/keda-docs#932 --- Fixes --- Fixes # kedacore#3656 --- PR Notes --- Introduce annotation to pause ScaledJobs. Signed-off-by: flux-benj <benjamin.jessop@fluxfederation.com>
- Loading branch information
Showing
9 changed files
with
289 additions
and
3 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
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
216 changes: 216 additions & 0 deletions
216
tests/internals/pause_scaledjob/pause_scaledjob_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,216 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package pause_scaledjob_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
. "github.com/kedacore/keda/v2/tests/helper" | ||
) | ||
|
||
// Load environment variables from .env file | ||
|
||
const ( | ||
testName = "pause-scaledjob-test" | ||
) | ||
|
||
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) | ||
minReplicaCount = 0 | ||
maxReplicaCount = 10 | ||
iterationCountInitial = 15 | ||
iterationCountLatter = 30 | ||
) | ||
|
||
type templateData struct { | ||
TestNamespace string | ||
ServiceName string | ||
ScalerName string | ||
ScaledJobName string | ||
MinReplicaCount, MaxReplicaCount int | ||
MetricThreshold, MetricValue int | ||
} | ||
|
||
const ( | ||
serviceTemplate = ` | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: {{.ServiceName}} | ||
namespace: {{.TestNamespace}} | ||
spec: | ||
ports: | ||
- port: 6000 | ||
targetPort: 6000 | ||
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-e2e:latest | ||
imagePullPolicy: Always | ||
ports: | ||
- containerPort: 6000 | ||
` | ||
|
||
scaledJobTemplate = ` | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledJob | ||
metadata: | ||
name: {{.ScaledJobName}} | ||
namespace: {{.TestNamespace}} | ||
spec: | ||
pollingInterval: 5 | ||
maxReplicaCount: {{.MaxReplicaCount}} | ||
minReplicaCount: {{.MinReplicaCount}} | ||
successfulJobsHistoryLimit: 0 | ||
failedJobsHistoryLimit: 0 | ||
jobTargetRef: | ||
template: | ||
spec: | ||
containers: | ||
- name: external-executor | ||
image: busybox | ||
command: | ||
- sleep | ||
- "15" | ||
imagePullPolicy: IfNotPresent | ||
restartPolicy: Never | ||
backoffLimit: 1 | ||
triggers: | ||
- type: external | ||
metadata: | ||
scalerAddress: {{.ServiceName}}.{{.TestNamespace}}:6000 | ||
metricThreshold: "{{.MetricThreshold}}" | ||
metricValue: "{{.MetricValue}}" | ||
` | ||
|
||
scaledJobAnnotatedTemplate = ` | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledJob | ||
metadata: | ||
name: {{.ScaledJobName}} | ||
namespace: {{.TestNamespace}} | ||
annotations: | ||
autoscaling.keda.sh/paused: "true" | ||
spec: | ||
pollingInterval: 5 | ||
maxReplicaCount: {{.MaxReplicaCount}} | ||
minReplicaCount: {{.MinReplicaCount}} | ||
successfulJobsHistoryLimit: 0 | ||
failedJobsHistoryLimit: 0 | ||
jobTargetRef: | ||
template: | ||
spec: | ||
containers: | ||
- name: external-executor | ||
image: busybox | ||
command: | ||
- sleep | ||
- "15" | ||
imagePullPolicy: IfNotPresent | ||
restartPolicy: Never | ||
backoffLimit: 1 | ||
triggers: | ||
- type: external | ||
metadata: | ||
scalerAddress: {{.ServiceName}}.{{.TestNamespace}}:6000 | ||
metricThreshold: "{{.MetricThreshold}}" | ||
metricValue: "{{.MetricValue}}" | ||
` | ||
) | ||
|
||
func TestScaler(t *testing.T) { | ||
// setup | ||
t.Log("--- setting up ---") | ||
|
||
// Create kubernetes resources | ||
kc := GetKubernetesClient(t) | ||
metricValue := 1 | ||
|
||
data, templates := getTemplateData(metricValue) | ||
|
||
CreateKubernetesResources(t, kc, testNamespace, data, templates) | ||
|
||
assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, data.MetricThreshold, iterationCountInitial, 1), | ||
"job count should be %d after %d iterations", data.MetricThreshold, iterationCountInitial) | ||
|
||
// remove the non-annotated job which scales fine | ||
KubectlDeleteWithTemplate(t, data, "scaledJobTemplate", scaledJobTemplate) | ||
|
||
// test scaling | ||
testIsPaused(t, kc, data) | ||
testUnpause(t, kc) | ||
|
||
// cleanup | ||
DeleteKubernetesResources(t, testNamespace, data, templates) | ||
} | ||
|
||
func getTemplateData(metricValue int) (templateData, []Template) { | ||
return templateData{ | ||
TestNamespace: testNamespace, | ||
ScaledJobName: scaledJobName, | ||
ScalerName: scalerName, | ||
ServiceName: serviceName, | ||
MinReplicaCount: minReplicaCount, | ||
MaxReplicaCount: maxReplicaCount, | ||
MetricThreshold: 1, | ||
MetricValue: metricValue, | ||
}, []Template{ | ||
{Name: "scalerTemplate", Config: scalerTemplate}, | ||
{Name: "serviceTemplate", Config: serviceTemplate}, | ||
{Name: "scaledJobTemplate", Config: scaledJobTemplate}, | ||
} | ||
} | ||
|
||
func testIsPaused(t *testing.T, kc *kubernetes.Clientset, data templateData) { | ||
t.Log("--- testing Paused annotation ---") | ||
|
||
data.MetricValue = maxReplicaCount | ||
|
||
KubectlApplyWithTemplate(t, data, "scaledJobAnnotatedTemplate", scaledJobAnnotatedTemplate) | ||
|
||
t.Log("job count does not change as job is paused") | ||
|
||
expectedTarget := 0 | ||
assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1), | ||
"job count should be %d after %d iterations", expectedTarget, iterationCountLatter) | ||
} | ||
|
||
func testUnpause(t *testing.T, kc *kubernetes.Clientset) { | ||
t.Log("--- testing removing Paused annotation ---") | ||
|
||
_, err := ExecuteCommand(fmt.Sprintf("kubectl annotate scaledjob %s autoscaling.keda.sh/paused-", scaledJobName)) | ||
assert.NoErrorf(t, err, "cannot execute command - %s", err) | ||
|
||
expectedTarget := maxReplicaCount | ||
assert.True(t, WaitForJobCountUntilIteration(t, kc, testNamespace, expectedTarget, iterationCountLatter, 1), | ||
"job count should be %d after %d iterations", expectedTarget, iterationCountLatter) | ||
} |
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