This is an automated email from the ASF dual-hosted git repository.
squakez pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git
The following commit(s) were added to refs/heads/main by this push:
new d80486774 feat(trait/keda): add NATS JetStream scaler mapping (#6974)
d80486774 is described below
commit d804867748ce62c7f8136cf15433237eb00df8eb
Author: Harshana Lakshara Fernando <[email protected]>
AuthorDate: Tue Nov 17 16:58:02 2026 +0530
feat(trait/keda): add NATS JetStream scaler mapping (#6974)
* feat(trait/keda): add NATS JetStream scaler mapping
Signed-off-by: UchihaIthachi <[email protected]>
* test(e2e): let the NATS consumer create its stream before scaling to zero
KEDA scaled the consumer to 0 as soon as it was created, so the stream and
the durable consumer were never created and the producer got 503 No Responders.
Start the consumer with minReplicaCount 1, wait for the stream, then lower it
to 0. Raise the KEDA e2e timeout to 30m, since the suite on main was already
close to 20m.
Signed-off-by: UchihaIthachi <[email protected]>
---------
Signed-off-by: UchihaIthachi <[email protected]>
---
.../ROOT/pages/contributing/keda-scaler.adoc | 2 +-
docs/modules/traits/pages/keda.adoc | 1 +
e2e/keda/files/keda-it-nats-to-log-auto.yaml | 39 ++++++
e2e/keda/files/nats-ephemeral.yaml | 68 ++++++++++
e2e/keda/files/timer-to-nats.yaml | 34 +++++
e2e/keda/keda_nats_test.go | 88 +++++++++++++
pkg/trait/keda/mapper.go | 5 +
pkg/trait/keda/scalers/nats.go | 95 ++++++++++++++
pkg/trait/keda/scalers/nats_test.go | 139 +++++++++++++++++++++
script/Makefile | 2 +-
10 files changed, 471 insertions(+), 2 deletions(-)
diff --git a/docs/modules/ROOT/pages/contributing/keda-scaler.adoc
b/docs/modules/ROOT/pages/contributing/keda-scaler.adoc
index 84ecfbf95..ea7e206f0 100644
--- a/docs/modules/ROOT/pages/contributing/keda-scaler.adoc
+++ b/docs/modules/ROOT/pages/contributing/keda-scaler.adoc
@@ -136,7 +136,7 @@ It should not add unrelated Camel endpoint parameters to
the KEDA metadata.
. Add `pkg/trait/keda/scalers/<component>.go`.
. Define a type that implements `ScaleMapper`.
. Return the Camel component name from `Component`.
-. In `Map`, translate only relevant Camel URI path and parameter values to the
metadata expected by the existing KEDA scaler, and return its scaler type.
+. In `Map`, translate only relevant Camel URI path and parameter values to the
metadata expected by the existing KEDA scaler, and return its scaler type.
Return an empty scaler type when the endpoint has nothing the scaler can
measure; no trigger is then created for it.
. Register the mapper with `keda.Register` in an `init` function.
. Add `pkg/trait/keda/scalers/<component>_test.go`.
diff --git a/docs/modules/traits/pages/keda.adoc
b/docs/modules/traits/pages/keda.adoc
index de5abacf5..6ac7f70a2 100644
--- a/docs/modules/traits/pages/keda.adoc
+++ b/docs/modules/traits/pages/keda.adoc
@@ -80,5 +80,6 @@ Automatic discovery currently supports the following Camel
components:
* `aws2-sqs`: creates an AWS SQS Queue trigger. `queueURL` comes from the
`queueUrl` endpoint parameter, or is built from a queue ARN, or from the queue
name with the `region` and `queueOwnerAWSAccountId` parameters. Otherwise the
plain queue name is used, which KEDA accepts when it is unambiguous.
`awsRegion` comes from `region` or the ARN, and `awsEndpoint` from
`uriEndpointOverride` when `overrideEndpoint=true`. Set other metadata, such as
`queueLength`, with `keda.autoMetadata`, and [...]
* `pulsar`: creates an Apache Pulsar trigger from the topic in the endpoint
URI and the `subscriptionName` option (Camel's default `subs` when it is not
set). When the service URL (`serviceUrl` option or
`camel.component.pulsar.service-url` property) uses the `pulsar://` scheme,
`adminURL` is set to `http://<host>:8080`; for TLS or a custom admin port, set
it with `keda.autoMetadata`. See the
https://keda.sh/docs/latest/scalers/pulsar/[KEDA Apache Pulsar scaler]
documentation for the ava [...]
* `spring-rabbitmq`: creates a RabbitMQ trigger whose `queueName` is the
`queues` value of the endpoint URI, when it names a single queue. The broker
address is not part of the URI: a `host` endpoint parameter, when present, is
used as the trigger `host` as is; otherwise set `host`, together with `mode`
and `value`, with `keda.autoMetadata`. See the
https://keda.sh/docs/latest/scalers/rabbitmq-queue/[KEDA RabbitMQ Queue scaler]
documentation for the available trigger metadata.
+* `nats`: creates a NATS JetStream trigger for a JetStream consumer with a
durable name (`jetstreamEnabled=true`, `jetstreamName` and `durableName`, as
endpoint options or `camel.component.nats.*` properties). The stream is
`jetstreamName`, the consumer is `<durableName>-durable` (the name Camel gives
the durable consumer) and the account is `$G`, the default account of a NATS
server without configured accounts. When `servers` is set,
`natsServerMonitoringEndpoint` is set to `<host>:8222 [...]
Unsupported Camel components do not produce an automatically discovered
trigger. You can still configure their KEDA triggers explicitly with
`keda.triggers`.
diff --git a/e2e/keda/files/keda-it-nats-to-log-auto.yaml
b/e2e/keda/files/keda-it-nats-to-log-auto.yaml
new file mode 100644
index 000000000..26d99bd7a
--- /dev/null
+++ b/e2e/keda/files/keda-it-nats-to-log-auto.yaml
@@ -0,0 +1,39 @@
+# ---------------------------------------------------------------------------
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ---------------------------------------------------------------------------
+
+apiVersion: camel.apache.org/v1
+kind: Integration
+metadata:
+ name: keda-it-nats-to-log-auto
+spec:
+ flows:
+ - route:
+ from:
+ uri:
nats:orders?jetstreamEnabled=true&jetstreamName=ORDERS&durableName=keda
+ steps:
+ - to: log:info
+ traits:
+ camel:
+ properties:
+ - camel.component.nats.servers=nats://nats.%%%.svc:4222
+ keda:
+ enabled: true
+ # Starts with one replica so the consumer creates the stream and the
durable consumer; the test then lowers it to 0.
+ minReplicaCount: 1
+ maxReplicaCount: 1
+ cooldownPeriod: 20
+ pollingInterval: 10
diff --git a/e2e/keda/files/nats-ephemeral.yaml
b/e2e/keda/files/nats-ephemeral.yaml
new file mode 100644
index 000000000..0789d1372
--- /dev/null
+++ b/e2e/keda/files/nats-ephemeral.yaml
@@ -0,0 +1,68 @@
+# ---------------------------------------------------------------------------
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ---------------------------------------------------------------------------
+
+apiVersion: apps/v1
+kind: Deployment
+metadata:
+ name: nats
+spec:
+ replicas: 1
+ selector:
+ matchLabels:
+ app: nats
+ template:
+ metadata:
+ labels:
+ app: nats
+ spec:
+ containers:
+ - name: nats
+ image: nats:2.11-alpine
+ # JetStream enabled, monitoring endpoint on 8222 for the KEDA scaler.
+ args: ["-js", "-m", "8222"]
+ ports:
+ - containerPort: 4222
+ name: client
+ - containerPort: 8222
+ name: monitoring
+ readinessProbe:
+ httpGet:
+ path: /healthz?js-enabled-only=true
+ port: 8222
+ initialDelaySeconds: 2
+ periodSeconds: 2
+ resources:
+ requests:
+ memory: "64Mi"
+ cpu: "50m"
+ limits:
+ memory: "256Mi"
+---
+apiVersion: v1
+kind: Service
+metadata:
+ name: nats
+spec:
+ selector:
+ app: nats
+ ports:
+ - name: client
+ port: 4222
+ targetPort: 4222
+ - name: monitoring
+ port: 8222
+ targetPort: 8222
diff --git a/e2e/keda/files/timer-to-nats.yaml
b/e2e/keda/files/timer-to-nats.yaml
new file mode 100644
index 000000000..5c6e177a3
--- /dev/null
+++ b/e2e/keda/files/timer-to-nats.yaml
@@ -0,0 +1,34 @@
+# ---------------------------------------------------------------------------
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+# ---------------------------------------------------------------------------
+
+apiVersion: camel.apache.org/v1
+kind: Integration
+metadata:
+ name: timer-to-nats
+spec:
+ flows:
+ - route:
+ from:
+ uri: timer:foo?period=1000
+ steps:
+ - setBody:
+ constant: "Hello NATS"
+ - to: nats:orders?jetstreamEnabled=true&jetstreamName=ORDERS
+ traits:
+ camel:
+ properties:
+ - camel.component.nats.servers=nats://nats.%%%.svc:4222
diff --git a/e2e/keda/keda_nats_test.go b/e2e/keda/keda_nats_test.go
new file mode 100644
index 000000000..04ee11d93
--- /dev/null
+++ b/e2e/keda/keda_nats_test.go
@@ -0,0 +1,88 @@
+//go:build integration
+// +build integration
+
+// To enable compilation of this file in Goland, go to "Settings -> Go ->
Vendoring & Build Tags -> Custom Tags" and add "integration"
+
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package keda
+
+import (
+ "context"
+ "testing"
+
+ . "github.com/onsi/gomega"
+ . "github.com/onsi/gomega/gstruct"
+ corev1 "k8s.io/api/core/v1"
+
+ . "github.com/apache/camel-k/v2/e2e/support"
+ v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
+)
+
+func TestNatsKedaAutoscale(t *testing.T) {
+ WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) {
+ ExpectExecSucceed(t, g, Kubectl("apply", "-f",
"files/nats-ephemeral.yaml", "-n", ns))
+ ExpectExecSucceed(t, g, Kubectl("rollout", "status",
"deployment", "nats", "-n", ns, "--timeout=300s"))
+
+ t.Run("NATS JetStream auto-discovered trigger", func(t
*testing.T) {
+ consumerName := "keda-it-nats-to-log-auto"
+ producerName := "timer-to-nats"
+
+ ExpectExecSucceed(t, g, Kubectl("apply", "-f",
cloneAndReplaceNamespace(t, "files/keda-it-nats-to-log-auto.yaml", ns), "-n",
ns))
+
+ // The ScaledObject uses the trigger discovered from
the NATS endpoint URI
+ g.Eventually(ScaledObject(t, ctx, ns, consumerName),
TestTimeoutMedium).ShouldNot(BeNil())
+ scaledObj := ScaledObject(t, ctx, ns, consumerName)()
+ g.Expect(scaledObj.Spec.Triggers).To(HaveLen(1))
+
g.Expect(scaledObj.Spec.Triggers[0].Type).To(Equal("nats-jetstream"))
+
g.Expect(scaledObj.Spec.Triggers[0].Metadata["account"]).To(Equal("$G"))
+
g.Expect(scaledObj.Spec.Triggers[0].Metadata["stream"]).To(Equal("ORDERS"))
+
g.Expect(scaledObj.Spec.Triggers[0].Metadata["consumer"]).To(Equal("keda-durable"))
+
g.Expect(scaledObj.Spec.Triggers[0].Metadata["natsServerMonitoringEndpoint"]).To(Equal("nats."
+ ns + ".svc:8222"))
+
+ // Start consumer; it creates the stream and the
durable consumer. Until both exist the scaler has nothing to
+ // read, and a producer publishing to JetStream gets no
responder, so the consumer starts with minReplicaCount 1.
+ g.Eventually(IntegrationConditionStatus(t, ctx, ns,
consumerName, v1.IntegrationConditionReady), TestTimeoutLong).
+ Should(Equal(corev1.ConditionTrue))
+ g.Eventually(IntegrationLogs(t, ctx, ns, consumerName),
TestTimeoutMedium).
+ Should(ContainSubstring("JetStream stream
'ORDERS' created"))
+ ExpectExecSucceed(t, g, Kubectl("patch", "it",
consumerName, "-n", ns, "--type=merge",
+ "-p",
`{"spec":{"traits":{"keda":{"minReplicaCount":0}}}}`))
+
+ // Scale down to 0 when idle
+ g.Eventually(IntegrationStatusReplicas(t, ctx, ns,
consumerName), TestTimeoutMedium).
+ Should(PointTo(BeNumerically("==", 0)))
+
+ // Start producer to build a backlog on the durable
consumer
+ ExpectExecSucceed(t, g, Kubectl("apply", "-f",
cloneAndReplaceNamespace(t, "files/timer-to-nats.yaml", ns), "-n", ns))
+ g.Eventually(IntegrationConditionStatus(t, ctx, ns,
producerName, v1.IntegrationConditionReady), TestTimeoutLong).
+ Should(Equal(corev1.ConditionTrue))
+
+ // Consumer scales up to 1 and processes the messages
+ g.Eventually(IntegrationStatusReplicas(t, ctx, ns,
consumerName), TestTimeoutMedium).
+ Should(PointTo(BeNumerically("==", 1)))
+ g.Eventually(IntegrationLogs(t, ctx, ns, consumerName),
TestTimeoutMedium).
+ Should(ContainSubstring("Hello NATS"))
+
+ // Stop producer and scale back to 0
+ ExpectExecSucceed(t, g, Kubectl("delete", "it",
producerName, "-n", ns))
+ g.Eventually(IntegrationStatusReplicas(t, ctx, ns,
consumerName), TestTimeoutMedium).
+ Should(PointTo(BeNumerically("==", 0)))
+ })
+ })
+}
diff --git a/pkg/trait/keda/mapper.go b/pkg/trait/keda/mapper.go
index 50037170d..ceeca765b 100644
--- a/pkg/trait/keda/mapper.go
+++ b/pkg/trait/keda/mapper.go
@@ -22,6 +22,7 @@ import (
)
// ScaleMapper defines the interface for mapping Camel component URIs to KEDA
triggers.
+// Map returns an empty scaler type when the endpoint has nothing a KEDA
scaler can measure.
type ScaleMapper interface {
Component() string
Map(pathValue string, params map[string]string, props
map[string]string) (kedaType string, metadata map[string]string)
@@ -52,6 +53,10 @@ func MapToKedaTrigger(rawURI string, props
map[string]string) (*traitv1.KedaTrig
return nil, nil
}
kedaType, metadata := mapper.Map(pathValue, params, props)
+ if kedaType == "" {
+ // The mapper found nothing to scale on for this endpoint.
+ return nil, nil
+ }
return &traitv1.KedaTrigger{
Type: kedaType,
diff --git a/pkg/trait/keda/scalers/nats.go b/pkg/trait/keda/scalers/nats.go
new file mode 100644
index 000000000..24ed7d736
--- /dev/null
+++ b/pkg/trait/keda/scalers/nats.go
@@ -0,0 +1,95 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package scalers
+
+import (
+ "net"
+ "net/url"
+ "strings"
+
+ keda "github.com/apache/camel-k/v2/pkg/trait/keda"
+)
+
+const (
+ // natsDefaultAccount is the account that holds the streams of a NATS
server without configured accounts.
+ natsDefaultAccount = "$G"
+ // natsMonitoringPort is the default port of the NATS server monitoring
endpoint.
+ natsMonitoringPort = "8222"
+ // natsDurableSuffix is appended by Camel to the durableName option to
name the JetStream consumer.
+ natsDurableSuffix = "-durable"
+)
+
+// NatsScaler maps Camel NATS JetStream consumer endpoints to the KEDA
nats-jetstream scaler.
+type NatsScaler struct{}
+
+func init() {
+ keda.Register(&NatsScaler{})
+}
+
+func (n *NatsScaler) Component() string {
+ return "nats"
+}
+
+func (n *NatsScaler) Map(pathValue string, params map[string]string, props
map[string]string) (string, map[string]string) {
+ option := func(name, property string) string {
+ if v := params[name]; v != "" {
+ return v
+ }
+
+ return props["camel.component.nats."+property]
+ }
+
+ // KEDA scales on the pending messages of a durable JetStream consumer.
A core NATS subject keeps no backlog, and an
+ // ephemeral consumer is removed while the Integration is scaled to
zero, so neither produces a trigger.
+ stream := option("jetstreamName", "jetstream-name")
+ durable := option("durableName", "durable-name")
+ if !strings.EqualFold(option("jetstreamEnabled", "jetstream-enabled"),
"true") || stream == "" || durable == "" {
+ return "", nil
+ }
+
+ metadata := map[string]string{
+ "account": natsDefaultAccount,
+ "stream": stream,
+ "consumer": durable + natsDurableSuffix,
+ }
+ // The monitoring endpoint is not part of the Camel configuration.
Derive it from the first server on the default
+ // monitoring port; for other setups it can be set with
keda.autoMetadata.
+ if host := natsServerHost(option("servers", "servers")); host != "" {
+ metadata["natsServerMonitoringEndpoint"] =
net.JoinHostPort(host, natsMonitoringPort)
+ }
+
+ return "nats-jetstream", metadata
+}
+
+// natsServerHost returns the host of the first server of a Camel servers
option, for example
+// "nats://nats.ns.svc:4222" or "nats.ns.svc:4222,other.ns.svc:4222".
+func natsServerHost(servers string) string {
+ first := strings.TrimSpace(strings.Split(servers, ",")[0])
+ if first == "" {
+ return ""
+ }
+ if !strings.Contains(first, "://") {
+ first = "nats://" + first
+ }
+ u, err := url.Parse(first)
+ if err != nil {
+ return ""
+ }
+
+ return u.Hostname()
+}
diff --git a/pkg/trait/keda/scalers/nats_test.go
b/pkg/trait/keda/scalers/nats_test.go
new file mode 100644
index 000000000..66815bfa1
--- /dev/null
+++ b/pkg/trait/keda/scalers/nats_test.go
@@ -0,0 +1,139 @@
+/*
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/
+
+package scalers
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ keda "github.com/apache/camel-k/v2/pkg/trait/keda"
+)
+
+func TestNatsScalerRegistered(t *testing.T) {
+ mapper, found := keda.GetMapper("nats")
+ require.True(t, found, "nats scaler should be registered")
+ assert.Equal(t, "nats", mapper.Component())
+}
+
+func TestNatsScalerMap(t *testing.T) {
+ scaler := &NatsScaler{}
+
+ tests := []struct {
+ name string
+ params map[string]string
+ props map[string]string
+ expectedType string
+ expectedMeta map[string]string
+ }{
+ {
+ name: "durable JetStream consumer from the endpoint",
+ params: map[string]string{
+ "jetstreamEnabled": "true", "jetstreamName":
"ORDERS", "durableName": "keda",
+ "servers": "nats://nats.ns.svc:4222",
+ },
+ expectedType: "nats-jetstream",
+ expectedMeta: map[string]string{
+ "account": "$G",
+ "stream": "ORDERS",
+ "consumer": "keda-durable",
+ "natsServerMonitoringEndpoint":
"nats.ns.svc:8222",
+ },
+ },
+ {
+ name: "options from component properties",
+ params: map[string]string{},
+ props: map[string]string{
+ "camel.component.nats.jetstream-enabled":
"true",
+ "camel.component.nats.jetstream-name":
"ORDERS",
+ "camel.component.nats.durable-name":
"keda",
+ "camel.component.nats.servers":
"broker:4222,other:4222",
+ },
+ expectedType: "nats-jetstream",
+ expectedMeta: map[string]string{
+ "account": "$G",
+ "stream": "ORDERS",
+ "consumer": "keda-durable",
+ "natsServerMonitoringEndpoint": "broker:8222",
+ },
+ },
+ {
+ name: "endpoint options take precedence over
properties",
+ params: map[string]string{"jetstreamEnabled": "true",
"jetstreamName": "URI", "durableName": "uri", "servers": "uri-host:4222"},
+ props: map[string]string{
+ "camel.component.nats.jetstream-name": "PROPS",
+ "camel.component.nats.durable-name": "props",
+ "camel.component.nats.servers":
"props-host:4222",
+ },
+ expectedType: "nats-jetstream",
+ expectedMeta: map[string]string{
+ "account": "$G",
+ "stream": "URI",
+ "consumer": "uri-durable",
+ "natsServerMonitoringEndpoint": "uri-host:8222",
+ },
+ },
+ {
+ name: "no servers leaves the monitoring
endpoint to autoMetadata",
+ params: map[string]string{"jetstreamEnabled":
"true", "jetstreamName": "ORDERS", "durableName": "keda"},
+ expectedType: "nats-jetstream",
+ expectedMeta: map[string]string{"account": "$G",
"stream": "ORDERS", "consumer": "keda-durable"},
+ },
+ {
+ name: "core NATS subject",
+ params: map[string]string{"servers":
"nats://broker:4222"},
+ },
+ {
+ name: "ephemeral JetStream consumer",
+ params: map[string]string{"jetstreamEnabled": "true",
"jetstreamName": "ORDERS"},
+ },
+ {
+ name: "JetStream without a stream name",
+ params: map[string]string{"jetstreamEnabled": "true",
"durableName": "keda"},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ kedaType, metadata := scaler.Map("orders", tt.params,
tt.props)
+ assert.Equal(t, tt.expectedType, kedaType)
+ assert.Equal(t, tt.expectedMeta, metadata)
+ })
+ }
+}
+
+func TestMapToKedaTriggerNats(t *testing.T) {
+ trigger, err := keda.MapToKedaTrigger(
+
"nats:orders?jetstreamEnabled=true&jetstreamName=ORDERS&durableName=keda&servers=nats://broker:4222",
nil)
+ require.NoError(t, err)
+ require.NotNil(t, trigger)
+ assert.Equal(t, "nats-jetstream", trigger.Type)
+ assert.Equal(t, map[string]string{
+ "account": "$G",
+ "stream": "ORDERS",
+ "consumer": "keda-durable",
+ "natsServerMonitoringEndpoint": "broker:8222",
+ }, trigger.Metadata)
+}
+
+func TestMapToKedaTriggerNatsWithoutDurableConsumer(t *testing.T) {
+ trigger, err :=
keda.MapToKedaTrigger("nats:orders?servers=nats://broker:4222", nil)
+ require.NoError(t, err)
+ assert.Nil(t, trigger)
+}
diff --git a/script/Makefile b/script/Makefile
index 00ff792c7..0dd57747c 100644
--- a/script/Makefile
+++ b/script/Makefile
@@ -360,7 +360,7 @@ test-kafka: gotestfmt-install
#
test-keda: gotestfmt-install
FAILED=0; \
- go test -timeout 20m -v ./e2e/keda -tags=integration $(GOTESTFMT) ||
((FAILED++)); \
+ go test -timeout 30m -v ./e2e/keda -tags=integration $(GOTESTFMT) ||
((FAILED++)); \
exit $${FAILED}
#