Skip to content

Commit 7a9df22

Browse files
committed
feat(trait): service and container ports
Enable `container.ports` and `service.ports` to expose any generic port internally and externally (through Service), Close #6248
1 parent 31079ca commit 7a9df22

File tree

16 files changed

+812
-161
lines changed

16 files changed

+812
-161
lines changed

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

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6670,40 +6670,51 @@ string
66706670
66716671
The maximum amount of memory to be provided (default 512 Mi).
66726672
6673+
|`ports` +
6674+
[]string
6675+
|
6676+
6677+
6678+
List of container ports available in the container (syntax: <port-name>;<port-number>[;port-protocol]).
6679+
When omitted, `port-protocol` (admitted values `TCP`, `UDP` or `SCTP`) is `TCP`.
6680+
Don't use this for the primary http managed port (for which case you need to use `portName` and `port`).
6681+
Don't use in Knative based environments.
6682+
66736683
|`expose` +
66746684
bool
66756685
|
66766686
66776687
6678-
Can be used to enable/disable exposure via kubernetes Service.
6688+
Can be used to enable/disable http exposure via kubernetes Service.
66796689
66806690
|`port` +
66816691
int32
66826692
|
66836693
66846694
6685-
To configure a different port exposed by the container (default `8080`).
6695+
To configure a different http port exposed by the container (default `8080`).
66866696
66876697
|`portName` +
66886698
string
66896699
|
66906700
66916701
6692-
To configure a different port name for the port exposed by the container. It defaults to `http` only when the `expose` parameter is true.
6702+
To configure a different http port name for the port exposed by the container.
6703+
It defaults to `http` only when the `expose` parameter is true.
66936704
66946705
|`servicePort` +
66956706
int32
66966707
|
66976708
66986709
6699-
To configure under which service port the container port is to be exposed (default `80`).
6710+
To configure under which service port the http container port is to be exposed (default `80`).
67006711
67016712
|`servicePortName` +
67026713
string
67036714
|
67046715
67056716
6706-
To configure under which service port name the container port is to be exposed (default `http`).
6717+
To configure under which service port name the http container port is to be exposed (default `http`).
67076718
67086719
|`name` +
67096720
string
@@ -8957,8 +8968,8 @@ List of Services in the form [[apigroup/]version:]kind:[namespace/]name
89578968
89588969
* <<#_camel_apache_org_v1_Traits, Traits>>
89598970
8960-
The Service trait exposes the integration with a Service resource so that it can be accessed by other applications
8961-
(or integrations) in the same namespace.
8971+
The Service trait exposes the Integration with a Service resource so that it can be accessed by other applications
8972+
(or Integrations) in the same namespace.
89628973
89638974
NOTE: this trait is automatically disabled if the Knative Service trait is enabled.
89648975
@@ -9013,6 +9024,17 @@ map[string]string
90139024
90149025
The labels added to the Service object.
90159026
9027+
|`ports` +
9028+
[]string
9029+
|
9030+
9031+
9032+
List of container ports available in the container to expose
9033+
(syntax: <port-name>;<port-number>;<container-port-number>[;<port-protocol]).
9034+
When omitted, `port-protocol` (admitted values `TCP`, `UDP` or `SCTP`) is `TCP`.
9035+
Don't use this for the primary http managed port (which is managed by container trait).
9036+
Don't use in Knative based environments.
9037+
90169038
90179039
|===
90189040

docs/modules/traits/pages/container.adoc

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,33 @@ The following configuration options are available:
5151
| string
5252
| The maximum amount of memory to be provided (default 512 Mi).
5353

54+
| container.ports
55+
| []string
56+
| List of container ports available in the container (syntax: <port-name>;<port-number>[;port-protocol]).
57+
When omitted, `port-protocol` (admitted values `TCP`, `UDP` or `SCTP`) is `TCP`.
58+
Don't use this for the primary http managed port (for which case you need to use `portName` and `port`).
59+
Don't use in Knative based environments.
60+
5461
| container.expose
5562
| bool
56-
| Can be used to enable/disable exposure via kubernetes Service.
63+
| Can be used to enable/disable http exposure via kubernetes Service.
5764

5865
| container.port
5966
| int32
60-
| To configure a different port exposed by the container (default `8080`).
67+
| To configure a different http port exposed by the container (default `8080`).
6168

6269
| container.port-name
6370
| string
64-
| To configure a different port name for the port exposed by the container. It defaults to `http` only when the `expose` parameter is true.
71+
| To configure a different http port name for the port exposed by the container.
72+
It defaults to `http` only when the `expose` parameter is true.
6573

6674
| container.service-port
6775
| int32
68-
| To configure under which service port the container port is to be exposed (default `80`).
76+
| To configure under which service port the http container port is to be exposed (default `80`).
6977

7078
| container.service-port-name
7179
| string
72-
| To configure under which service port name the container port is to be exposed (default `http`).
80+
| To configure under which service port name the http container port is to be exposed (default `http`).
7381

7482
| container.name
7583
| string

docs/modules/traits/pages/service.adoc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// Start of autogenerated code - DO NOT EDIT! (badges)
44
// End of autogenerated code - DO NOT EDIT! (badges)
55
// Start of autogenerated code - DO NOT EDIT! (description)
6-
The Service trait exposes the integration with a Service resource so that it can be accessed by other applications
7-
(or integrations) in the same namespace.
6+
The Service trait exposes the Integration with a Service resource so that it can be accessed by other applications
7+
(or Integrations) in the same namespace.
88

99
NOTE: this trait is automatically disabled if the Knative Service trait is enabled.
1010

@@ -53,6 +53,26 @@ Deprecated: Use service type instead.
5353
| map[string]string
5454
| The labels added to the Service object.
5555

56+
| service.ports
57+
| []string
58+
| List of container ports available in the container to expose
59+
(syntax: <port-name>;<port-number>;<container-port-number>[;<port-protocol]).
60+
When omitted, `port-protocol` (admitted values `TCP`, `UDP` or `SCTP`) is `TCP`.
61+
Don't use this for the primary http managed port (which is managed by container trait).
62+
Don't use in Knative based environments.
63+
5664
|===
5765

5866
// End of autogenerated code - DO NOT EDIT! (configuration)
67+
68+
== Examples
69+
70+
* Expose a Service (we call it `hello`) on a port (`85``) which is used internally by your application (`8085`):
71+
+
72+
[source,console]
73+
$ kamel run PlatformHttpServer.java -p quarkus.http.port=8085 -t container.ports=hello;8085 -t service.ports=hello;85;8085
74+
75+
* Expose an UDP Service (we call it `udp`) on a port (`95`) which is used internally by your application (`9095`):
76+
+
77+
[source,console]
78+
$ kamel run MyUDPServer.java -t container.ports=udp;95;UDP -t service.ports=udp;95;9095;UDP

e2e/common/traits/service_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ package common
2424

2525
import (
2626
"context"
27+
"fmt"
2728
"testing"
29+
"time"
2830

2931
. "github.com/onsi/gomega"
3032

3133
corev1 "k8s.io/api/core/v1"
3234

3335
. "github.com/apache/camel-k/v2/e2e/support"
36+
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
3437
)
3538

3639
func TestServiceTrait(t *testing.T) {
@@ -97,3 +100,33 @@ func TestServiceTrait(t *testing.T) {
97100
})
98101
})
99102
}
103+
104+
func TestPortsServiceTrait(t *testing.T) {
105+
t.Parallel()
106+
WithNewTestNamespace(t, func(ctx context.Context, g *WithT, ns string) {
107+
t.Run("Service on port 8085", func(t *testing.T) {
108+
name := RandomizedSuffixName("svc")
109+
g.Expect(KamelRun(t, ctx, ns, "files/PlatformHttpServer.java",
110+
"-p", "quarkus.http.port=8085",
111+
"-t", "container.ports=hello;8085",
112+
"-t", "service.ports=hello;85;8085",
113+
"--name", name,
114+
).Execute()).To(Succeed())
115+
g.Eventually(IntegrationConditionStatus(t, ctx, ns, name, v1.IntegrationConditionReady)).
116+
Should(Equal(corev1.ConditionTrue))
117+
// We cannot use the health trait to make sure the application is ready to
118+
// get requests as we're sharing the service port.
119+
g.Eventually(IntegrationLogs(t, ctx, ns, name), TestTimeoutMedium).Should(ContainSubstring("Listening on: http://0.0.0.0:8085"))
120+
121+
response, err := TestClient(t).CoreV1().RESTClient().Get().
122+
AbsPath(fmt.Sprintf("/api/v1/namespaces/%s/services/%s:%d/proxy/hello/", ns, name, 85)).
123+
SetHeader("name", "service-test").
124+
Timeout(30 * time.Second).
125+
DoRaw(ctx)
126+
127+
g.Expect(err).ToNot(HaveOccurred())
128+
g.Expect(string(response)).To(Equal("Hello service-test"))
129+
})
130+
131+
})
132+
}

0 commit comments

Comments
 (0)