Skip to content

Commit 33ea2d9

Browse files
committed
feat(trait): Add KEDA auto-discovery for Camel component URIs
1 parent a10f41c commit 33ea2d9

17 files changed

+669
-68
lines changed

docs/modules/ROOT/partials/apis/camel-k-crds.adoc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7932,14 +7932,22 @@ int32
79327932
79337933
Maximum number of replicas.
79347934
7935-
|`triggers` +
7936-
*xref:#_camel_apache_org_v1_trait_KedaTrigger[[\]KedaTrigger]*
7935+
|`auto` +
7936+
bool
79377937
|
79387938
79397939
79407940
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
79417941
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
79427942
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
7943+
Automatically discover KEDA triggers from Camel component URIs.
7944+
7945+
|`triggers` +
7946+
*xref:#_camel_apache_org_v1_trait_KedaTrigger[[\]KedaTrigger]*
7947+
|
7948+
7949+
7950+
79437951
79447952
79457953
|===

docs/modules/traits/pages/keda.adoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,16 @@ The following configuration options are available:
4747
| int32
4848
| Maximum number of replicas.
4949

50-
| keda.triggers
51-
| []github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait.KedaTrigger
50+
| keda.auto
51+
| bool
5252
| Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
5353
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
5454
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
55+
Automatically discover KEDA triggers from Camel component URIs.
56+
57+
| keda.triggers
58+
| []github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait.KedaTrigger
59+
|
5560

5661
|===
5762

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: camel.apache.org/v1
2+
kind: Integration
3+
metadata:
4+
name: keda-kafka-auto-discovery
5+
namespace: kafka
6+
spec:
7+
sources:
8+
- content: |
9+
from("kafka:my-topic?brokers=my-cluster-kafka-bootstrap.kafka.svc:9092&groupId=auto-group")
10+
.log("${body}");
11+
name: routes.java
12+
traits:
13+
keda:
14+
enabled: true
15+
minReplicaCount: 0
16+
maxReplicaCount: 1
17+
# No triggers - auto-discovery should create kafka scaler
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# ---------------------------------------------------------------------------
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
# ---------------------------------------------------------------------------
17+
# This integration tests KEDA auto-discovery - no manual triggers are specified,
18+
# the trait should automatically discover the kafka component and create a trigger.
19+
apiVersion: camel.apache.org/v1
20+
kind: Integration
21+
metadata:
22+
name: keda-kafka-auto-discovery
23+
namespace: kafka
24+
spec:
25+
sources:
26+
- content: |
27+
from("kafka:my-topic?brokers=my-cluster-kafka-bootstrap.kafka.svc:9092&groupId=auto-group")
28+
.log("${body}");
29+
name: routes.java
30+
traits:
31+
keda:
32+
enabled: true
33+
minReplicaCount: 0
34+
maxReplicaCount: 1
35+
cooldownPeriod: 20
36+
pollingInterval: 10
37+
# No triggers specified - auto-discovery should create kafka scaler

e2e/kafka/kafka_autoscale_keda_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,28 @@ func TestKafkaKedaAutoscale(t *testing.T) {
7373
})
7474
})
7575
}
76+
77+
func TestKafkaKedaAutoDiscovery(t *testing.T) {
78+
WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) {
79+
t.Run("Auto-discovery Kafka", func(t *testing.T) {
80+
ExpectExecSucceed(t, g, Kubectl("apply", "-f", "files/keda-kafka-auto-discovery.yaml"))
81+
ns := "kafka"
82+
integrationName := "keda-kafka-auto-discovery"
83+
84+
// Wait for ScaledObject
85+
g.Eventually(ScaledObject(t, ctx, ns, integrationName), TestTimeoutMedium).
86+
ShouldNot(BeNil())
87+
88+
// Verify the auto-discovered
89+
scaledObj := ScaledObject(t, ctx, ns, integrationName)()
90+
g.Expect(scaledObj).NotTo(BeNil())
91+
g.Expect(scaledObj.Spec.Triggers).To(HaveLen(1))
92+
g.Expect(scaledObj.Spec.Triggers[0].Type).To(Equal("kafka"))
93+
g.Expect(scaledObj.Spec.Triggers[0].Metadata["topic"]).To(Equal("my-topic"))
94+
g.Expect(scaledObj.Spec.Triggers[0].Metadata["bootstrapServers"]).To(Equal("my-cluster-kafka-bootstrap.kafka.svc:9092"))
95+
g.Expect(scaledObj.Spec.Triggers[0].Metadata["consumerGroup"]).To(Equal("auto-group"))
96+
97+
g.Expect(Kamel(t, ctx, "delete", integrationName, "-n", ns).Execute()).To(Succeed())
98+
})
99+
})
100+
}

e2e/support/test_support.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import (
7575
"github.com/apache/camel-k/v2/e2e/support/util"
7676
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
7777
traitv1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1/trait"
78+
kedav1alpha1 "github.com/apache/camel-k/v2/pkg/apis/duck/keda/v1alpha1"
7879
"github.com/apache/camel-k/v2/pkg/client"
7980
"github.com/apache/camel-k/v2/pkg/cmd"
8081
"github.com/apache/camel-k/v2/pkg/platform"
@@ -192,6 +193,7 @@ func init() {
192193
client.FastMapperAllowedAPIGroups["operators.coreos.com"] = true
193194
client.FastMapperAllowedAPIGroups["config.openshift.io"] = true
194195
client.FastMapperAllowedAPIGroups["policy"] = true
196+
client.FastMapperAllowedAPIGroups["keda.sh"] = true
195197

196198
var err error
197199

@@ -3110,3 +3112,19 @@ func DefaultOperatorSecurityContext() *corev1.SecurityContext {
31103112

31113113
return &sc
31123114
}
3115+
3116+
// ScaledObject retrieves a KEDA ScaledObject by name from a namespace.
3117+
func ScaledObject(t *testing.T, ctx context.Context, ns, name string) func() *kedav1alpha1.ScaledObject {
3118+
return func() *kedav1alpha1.ScaledObject {
3119+
scaledObject := kedav1alpha1.ScaledObject{}
3120+
key := ctrl.ObjectKey{
3121+
Namespace: ns,
3122+
Name: name,
3123+
}
3124+
if err := TestClient(t).Get(ctx, key, &scaledObject); err != nil {
3125+
log.Error(err, "Error while retrieving ScaledObject "+name)
3126+
return nil
3127+
}
3128+
return &scaledObject
3129+
}
3130+
}

helm/camel-k/crds/camel-k-crds.yaml

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4726,6 +4726,13 @@ spec:
47264726
keda:
47274727
description: The configuration of Keda trait
47284728
properties:
4729+
auto:
4730+
description: |-
4731+
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
4732+
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
4733+
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
4734+
Automatically discover KEDA triggers from Camel component URIs.
4735+
type: boolean
47294736
configuration:
47304737
description: |-
47314738
Legacy trait configuration parameters.
@@ -4759,10 +4766,6 @@ spec:
47594766
format: int32
47604767
type: integer
47614768
triggers:
4762-
description: |-
4763-
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
4764-
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
4765-
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
47664769
items:
47674770
properties:
47684771
metadata:
@@ -7054,6 +7057,13 @@ spec:
70547057
keda:
70557058
description: The configuration of Keda trait
70567059
properties:
7060+
auto:
7061+
description: |-
7062+
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
7063+
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
7064+
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
7065+
Automatically discover KEDA triggers from Camel component URIs.
7066+
type: boolean
70577067
configuration:
70587068
description: |-
70597069
Legacy trait configuration parameters.
@@ -7087,10 +7097,6 @@ spec:
70877097
format: int32
70887098
type: integer
70897099
triggers:
7090-
description: |-
7091-
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
7092-
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
7093-
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
70947100
items:
70957101
properties:
70967102
metadata:
@@ -9284,6 +9290,13 @@ spec:
92849290
keda:
92859291
description: The configuration of Keda trait
92869292
properties:
9293+
auto:
9294+
description: |-
9295+
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
9296+
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
9297+
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
9298+
Automatically discover KEDA triggers from Camel component URIs.
9299+
type: boolean
92879300
configuration:
92889301
description: |-
92899302
Legacy trait configuration parameters.
@@ -9317,10 +9330,6 @@ spec:
93179330
format: int32
93189331
type: integer
93199332
triggers:
9320-
description: |-
9321-
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
9322-
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
9323-
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
93249333
items:
93259334
properties:
93269335
metadata:
@@ -11491,6 +11500,13 @@ spec:
1149111500
keda:
1149211501
description: The configuration of Keda trait
1149311502
properties:
11503+
auto:
11504+
description: |-
11505+
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
11506+
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
11507+
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
11508+
Automatically discover KEDA triggers from Camel component URIs.
11509+
type: boolean
1149411510
configuration:
1149511511
description: |-
1149611512
Legacy trait configuration parameters.
@@ -11524,10 +11540,6 @@ spec:
1152411540
format: int32
1152511541
type: integer
1152611542
triggers:
11527-
description: |-
11528-
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
11529-
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
11530-
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
1153111543
items:
1153211544
properties:
1153311545
metadata:
@@ -20532,6 +20544,13 @@ spec:
2053220544
keda:
2053320545
description: The configuration of Keda trait
2053420546
properties:
20547+
auto:
20548+
description: |-
20549+
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
20550+
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
20551+
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
20552+
Automatically discover KEDA triggers from Camel component URIs.
20553+
type: boolean
2053520554
configuration:
2053620555
description: |-
2053720556
Legacy trait configuration parameters.
@@ -20565,10 +20584,6 @@ spec:
2056520584
format: int32
2056620585
type: integer
2056720586
triggers:
20568-
description: |-
20569-
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
20570-
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
20571-
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
2057220587
items:
2057320588
properties:
2057420589
metadata:
@@ -22688,6 +22703,13 @@ spec:
2268822703
keda:
2268922704
description: The configuration of Keda trait
2269022705
properties:
22706+
auto:
22707+
description: |-
22708+
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
22709+
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
22710+
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
22711+
Automatically discover KEDA triggers from Camel component URIs.
22712+
type: boolean
2269122713
configuration:
2269222714
description: |-
2269322715
Legacy trait configuration parameters.
@@ -22721,10 +22743,6 @@ spec:
2272122743
format: int32
2272222744
type: integer
2272322745
triggers:
22724-
description: |-
22725-
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
22726-
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
22727-
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
2272822746
items:
2272922747
properties:
2273022748
metadata:
@@ -33091,6 +33109,13 @@ spec:
3309133109
keda:
3309233110
description: The configuration of Keda trait
3309333111
properties:
33112+
auto:
33113+
description: |-
33114+
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
33115+
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
33116+
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
33117+
Automatically discover KEDA triggers from Camel component URIs.
33118+
type: boolean
3309433119
configuration:
3309533120
description: |-
3309633121
Legacy trait configuration parameters.
@@ -33125,10 +33150,6 @@ spec:
3312533150
format: int32
3312633151
type: integer
3312733152
triggers:
33128-
description: |-
33129-
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
33130-
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
33131-
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
3313233153
items:
3313333154
properties:
3313433155
metadata:
@@ -35184,6 +35205,13 @@ spec:
3518435205
keda:
3518535206
description: The configuration of Keda trait
3518635207
properties:
35208+
auto:
35209+
description: |-
35210+
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
35211+
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
35212+
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
35213+
Automatically discover KEDA triggers from Camel component URIs.
35214+
type: boolean
3518735215
configuration:
3518835216
description: |-
3518935217
Legacy trait configuration parameters.
@@ -35217,10 +35245,6 @@ spec:
3521735245
format: int32
3521835246
type: integer
3521935247
triggers:
35220-
description: |-
35221-
Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
35222-
to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
35223-
and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
3522435248
items:
3522535249
properties:
3522635250
metadata:

pkg/apis/camel/v1/trait/keda.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ type KedaTrait struct {
3636
// Definition of triggers according to the KEDA format. Each trigger must contain `type` field corresponding
3737
// to the name of a KEDA autoscaler and a key/value map named `metadata` containing specific trigger options
3838
// and optionally a mapping of secrets, used by Keda operator to poll resources according to the autoscaler type.
39+
// Automatically discover KEDA triggers from Camel component URIs.
40+
// +kubebuilder:validation:Optional
41+
Auto *bool `json:"auto,omitempty" property:"auto"`
3942
Triggers []KedaTrigger `json:"triggers,omitempty" property:"triggers"`
4043
}
4144

pkg/apis/camel/v1/trait/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)