-
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.
Apache Pulsar: Add support for partitioned topics
Signed-off-by: Michael Marshall <[email protected]>
- Loading branch information
1 parent
710acf6
commit 3364691
Showing
7 changed files
with
333 additions
and
271 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,247 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package helper | ||
|
||
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 ( | ||
apachePulsarVersion = "2.10.2" | ||
messageCount = 3 | ||
minReplicaCount = 0 | ||
maxReplicaCount = 5 | ||
msgBacklog = 10 | ||
) | ||
|
||
type templateData struct { | ||
ApachePulsarVersion string | ||
TestName string // Used for most resource names | ||
NumPartitions int // Use 0 to create a non-partitioned topic | ||
MessageCount int | ||
MinReplicaCount int | ||
MaxReplicaCount int | ||
MsgBacklog int | ||
} | ||
|
||
const pulsarStatefulsetTemplate = ` | ||
apiVersion: apps/v1 | ||
kind: StatefulSet | ||
metadata: | ||
name: {{.TestName}} | ||
namespace: {{.TestName}} | ||
labels: | ||
app: pulsar | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: pulsar | ||
replicas: 1 | ||
serviceName: {{.TestName}} | ||
template: | ||
metadata: | ||
labels: | ||
app: pulsar | ||
spec: | ||
containers: | ||
- name: pulsar | ||
image: apachepulsar/pulsar:{{.ApachePulsarVersion}} | ||
imagePullPolicy: IfNotPresent | ||
readinessProbe: | ||
tcpSocket: | ||
port: 8080 | ||
ports: | ||
- name: pulsar | ||
containerPort: 6650 | ||
protocol: TCP | ||
- name: admin | ||
containerPort: 8080 | ||
protocol: TCP | ||
env: | ||
- name: PULSAR_PREFIX_tlsRequireTrustedClientCertOnConnect | ||
value: "true" | ||
command: | ||
- sh | ||
- -c | ||
args: ["bin/apply-config-from-env.py conf/client.conf && bin/apply-config-from-env.py conf/standalone.conf && bin/pulsar standalone -nfw -nss"] | ||
` | ||
|
||
const pulsarServiceTemplate = ` | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: {{.TestName}} | ||
namespace: {{.TestName}} | ||
spec: | ||
type: ClusterIP | ||
ports: | ||
- name: http | ||
port: 8080 | ||
targetPort: 8080 | ||
protocol: TCP | ||
- name: pulsar | ||
port: 6650 | ||
targetPort: 6650 | ||
protocol: TCP | ||
selector: | ||
app: pulsar | ||
` | ||
|
||
const consumerTemplate = ` | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: {{.TestName}}-consumer | ||
namespace: {{.TestName}} | ||
labels: | ||
app: pulsar-consumer | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: pulsar-consumer | ||
template: | ||
metadata: | ||
labels: | ||
app: pulsar-consumer | ||
spec: | ||
containers: | ||
- name: pulsar-consumer | ||
image: ghcr.io/pulsar-sigs/pulsar-client:v0.3.1 | ||
imagePullPolicy: IfNotPresent | ||
readinessProbe: | ||
tcpSocket: | ||
port: 9494 | ||
args: ["consumer","--broker","pulsar://{{.TestName}}.{{.TestName}}:6650","--topic","persistent://public/default/keda","--subscription-name","keda","--consume-time","200"] | ||
` | ||
|
||
const scaledObjectTemplate = ` | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledObject | ||
metadata: | ||
name: {{.TestName}} | ||
namespace: {{.TestName}} | ||
spec: | ||
scaleTargetRef: | ||
name: {{.TestName}}-consumer | ||
pollingInterval: 5 # Optional. Default: 30 seconds | ||
cooldownPeriod: 30 # Optional. Default: 300 seconds | ||
maxReplicaCount: {{.MaxReplicaCount}} | ||
minReplicaCount: {{.MinReplicaCount}} | ||
isPartitionedTopic: {{ if .NumPartitions }} "true" {{else}} "false" {{end}} | ||
triggers: | ||
- type: pulsar | ||
metadata: | ||
msgBacklog: "{{.MsgBacklog}}" | ||
activationMsgBacklogThreshold: "5" | ||
adminURL: http://{{.TestName}}.{{.TestName}}:8080 | ||
topic: persistent://public/default/keda | ||
subscription: keda | ||
` | ||
|
||
const topicPublishJobTemplate = ` | ||
apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: {{.TestName}}-producer | ||
namespace: {{.TestName}} | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- name: pulsar-producer | ||
image: apachepulsar/pulsar:{{.ApachePulsarVersion}} | ||
imagePullPolicy: IfNotPresent | ||
args: ["bin/pulsar-perf produce --service-url pulsar://{{.TestName}}.{{.TestName}}:6650 --num-messages {{.MessageCount}} {{ if .NumPartitions }} "--partitions {{.NumPartitions}}" {{ end }} persistent://public/default/keda"] | ||
restartPolicy: Never | ||
backoffLimit: 4 | ||
` | ||
|
||
func TestScalerWithConfig(t *testing.T, testName string, numPartitions int) { | ||
// setup | ||
t.Log("--- setting up ---") | ||
|
||
// Create kubernetes resources | ||
kc := helper.GetKubernetesClient(t) | ||
data, templates := getTemplateData(testName, numPartitions) | ||
|
||
helper.CreateKubernetesResources(t, kc, testName, data, templates) | ||
|
||
assert.True(t, helper.WaitForStatefulsetReplicaReadyCount(t, kc, testName, testName, 1, 300, 1), | ||
"replica count should be 1 after 5 minute") | ||
|
||
helper.KubectlApplyWithTemplate(t, data, "consumerTemplate", consumerTemplate) | ||
|
||
// run consumer for create subscription | ||
assert.True(t, helper.WaitForDeploymentReplicaReadyCount(t, kc, getConsumerDeploymentName(testName), testName, 1, 300, 1), | ||
"replica count should be 1 after 5 minute") | ||
|
||
helper.KubectlApplyWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate) | ||
|
||
assert.True(t, helper.WaitForDeploymentReplicaReadyCount(t, kc, getConsumerDeploymentName(testName), testName, 0, 60, 1), | ||
"replica count should be 0 after a minute") | ||
|
||
testActivation(t, kc, data) | ||
// scale up | ||
testScaleUp(t, kc, data) | ||
// scale down | ||
testScaleDown(t, kc, testName) | ||
|
||
// cleanup | ||
helper.KubectlDeleteWithTemplate(t, data, "scaledObjectTemplate", scaledObjectTemplate) | ||
helper.KubectlDeleteWithTemplate(t, data, "publishJobTemplate", topicPublishJobTemplate) | ||
|
||
helper.DeleteKubernetesResources(t, kc, testName, data, templates) | ||
} | ||
|
||
func getTemplateData(testName string, numPartitions int) (templateData, []helper.Template) { | ||
return templateData{ | ||
ApachePulsarVersion: apachePulsarVersion, | ||
TestName: testName, | ||
NumPartitions: numPartitions, | ||
MessageCount: messageCount, | ||
MinReplicaCount: minReplicaCount, | ||
MaxReplicaCount: maxReplicaCount, | ||
MsgBacklog: msgBacklog, | ||
}, []helper.Template{ | ||
{Name: "statefulsetTemplate", Config: pulsarStatefulsetTemplate}, | ||
{Name: "serviceTemplate", Config: pulsarServiceTemplate}, | ||
} | ||
} | ||
|
||
func testActivation(t *testing.T, kc *kubernetes.Clientset, data templateData) { | ||
t.Log("--- testing activation ---") | ||
// publish message and less than MsgBacklog | ||
helper.KubectlApplyWithTemplate(t, data, "publishJobTemplate", topicPublishJobTemplate) | ||
helper.AssertReplicaCountNotChangeDuringTimePeriod(t, kc, getConsumerDeploymentName(data.TestName), data.TestName, data.MinReplicaCount, 60) | ||
helper.KubectlDeleteWithTemplate(t, data, "publishJobTemplate", topicPublishJobTemplate) | ||
} | ||
|
||
func testScaleUp(t *testing.T, kc *kubernetes.Clientset, data templateData) { | ||
data.MessageCount = 100 | ||
helper.KubectlApplyWithTemplate(t, data, "publishJobTemplate", topicPublishJobTemplate) | ||
assert.True(t, helper.WaitForDeploymentReplicaReadyCount(t, kc, getConsumerDeploymentName(data.TestName), data.TestName, 5, 300, 1), | ||
"replica count should be 5 after 5 minute") | ||
} | ||
|
||
func testScaleDown(t *testing.T, kc *kubernetes.Clientset, testName string) { | ||
t.Log("--- testing scale down ---") | ||
// Check if deployment scale down to 0 after 5 minutes | ||
assert.True(t, helper.WaitForDeploymentReplicaReadyCount(t, kc, getConsumerDeploymentName(testName), testName, 0, 300, 1), | ||
"Replica count should be 0 after 5 minutes") | ||
} | ||
|
||
func getConsumerDeploymentName(testName string) string { | ||
return fmt.Sprintf("%s-consumer", testName) | ||
} |
19 changes: 19 additions & 0 deletions
19
tests/scalers/pulsar/pulsar_non_partitioned_topic/pulsar_non_partitioned_topic_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,19 @@ | ||
//go:build e2e | ||
// +build e2e | ||
|
||
package pulsar_non_partitioned_topic_test | ||
|
||
import ( | ||
"testing" | ||
|
||
pulsar "github.com/kedacore/keda/v2/tests/scalers/pulsar/helper" | ||
) | ||
|
||
const ( | ||
testName = "pulsar-non-partitioned-topic-test" | ||
numPartitions = 0 | ||
) | ||
|
||
func TestScaler(t *testing.T) { | ||
pulsar.TestScalerWithConfig(t, testName, numPartitions) | ||
} |
Oops, something went wrong.