This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel-k.git
commit 0b132c937a9d59e5cf4ab9cef7bcd50668d51fde Author: Pasquale Congiusti <[email protected]> AuthorDate: Tue Jun 15 10:13:27 2021 +0200 doc(examples): databases --- .../pages/configuration/runtime-resources.adoc | 12 +++--- examples/README.md | 2 + examples/databases/PostgresDB.java | 46 ++++++++++++++++++++++ examples/databases/PostgresDBAutoDatasource.java | 45 +++++++++++++++++++++ examples/databases/README.md | 3 ++ examples/databases/datasource.properties | 3 ++ examples/databases/postgres-deploy/README.md | 30 ++++++++++++++ .../postgres-deploy/postgres-configmap.yaml | 10 +++++ .../postgres-deploy/postgres-deployment.yaml | 30 ++++++++++++++ .../postgres-deploy/postgres-service.yaml | 12 ++++++ .../postgres-deploy/postgres-storage.yaml | 29 ++++++++++++++ 11 files changed, 216 insertions(+), 6 deletions(-) diff --git a/docs/modules/ROOT/pages/configuration/runtime-resources.adoc b/docs/modules/ROOT/pages/configuration/runtime-resources.adoc index c4d81d5..aab28b3 100644 --- a/docs/modules/ROOT/pages/configuration/runtime-resources.adoc +++ b/docs/modules/ROOT/pages/configuration/runtime-resources.adoc @@ -10,14 +10,14 @@ NOTE: you'll find `--resource` is very similar to the `--config` run flag. The m [[runtime-resource-file]] == Runtime file resource -Most of the time you will deal with the need to provide your `Integration` with resource files you have stored in your local machine. In this case you can use the `--resource file:/path/to/file` flag that will copy that file under the _/etc/camel/data/resources/_ directory. You can look at the _resource destination path_ section at the bottom of this page to specify the destination file location. +Most of the time you will deal with the need to provide your `Integration` with resource files you have stored in your local machine. In this case you can use the `--resource file:/path/to/file` flag that will copy that file under the _/etc/camel/resources/_ directory. You can look at the _resource destination path_ section at the bottom of this page to specify the destination file location. Let's see an example. We want to create an `Integration` unzipping and reading the content of a file we'll provide (ie, _resources-data.zip_): [source,groovy] .resource-file-binary-route.groovy ---- -from('file:/etc/camel/data/resources/?fileName=resources-data.zip&noop=true&idempotent=false') +from('file:/etc/camel/resources/?fileName=resources-data.zip&noop=true&idempotent=false') .unmarshal().zipFile() .log('resource file unzipped content is: ${body}') ---- @@ -48,11 +48,11 @@ We want to use the materialized file in an integration: [source,groovy] .resource-configmap-route.groovy ---- -from('file:/etc/camel/data/configmaps/my-cm/?fileName=my-configmap-key&noop=true&idempotent=false') +from('file:/etc/camel/resources/my-cm/?fileName=my-configmap-key&noop=true&idempotent=false') .log('resource file content is: ${body}') ---- -You can see that we're expecting to use a _my-configmap-key_ file stored in the default resource location (_/etc/camel/data/configmaps/_). In order to materialize the `Configmap` will be as easy as running the `--resource` _configmap_ syntax: +You can see that we're expecting to use a _my-configmap-key_ file stored in the default resource location (_/etc/camel/resources/_). In order to materialize the `Configmap` will be as easy as running the `--resource` _configmap_ syntax: ---- kamel run --resource configmap:my-cm resource-configmap-route.groovy --dev @@ -78,11 +78,11 @@ We want to use the materialized secret file in an integration: [source,groovy] .resource-secret-route.groovy ---- -from('file:/etc/camel/data/secrets/my-sec/?fileName=my-secret-key&noop=true&idempotent=false') +from('file:/etc/camel/resources/my-sec/?fileName=my-secret-key&noop=true&idempotent=false') .log('resource file content is: ${body}') ---- -You can see that we're expecting to use a _my-secret-key_ file stored in the default resource location (_/etc/camel/data/secrets/_). In order to materialize the `Secret` will be as easy as running the `--resource` _secret_ syntax: +You can see that we're expecting to use a _my-secret-key_ file stored in the default resource location (_/etc/camel/resources/_). In order to materialize the `Secret` will be as easy as running the `--resource` _secret_ syntax: ---- kamel run --resource secret:my-sec resource-secret-route.groovy --dev diff --git a/examples/README.md b/examples/README.md index a7db709..c6fe58d 100644 --- a/examples/README.md +++ b/examples/README.md @@ -25,7 +25,9 @@ In this section you can find a few examples of certain [`Camel` components](http |---|---|---| | AMQP | Component usage | [see examples](./amqp/)| | Caffeine | Component usage | [see examples](./caffeine/)| +| Databases | Component usage | [see examples](./databases/)| | DNS | Component usage | [see examples](./dns/)| +| HTTP/HTTPS | Component usage | [see examples](./http/)| | Kafka | Component usage | [see examples](./kafka/)| | Knative | Component usage | [see examples](./knative/)| diff --git a/examples/databases/PostgresDB.java b/examples/databases/PostgresDB.java new file mode 100644 index 0000000..2c236a2 --- /dev/null +++ b/examples/databases/PostgresDB.java @@ -0,0 +1,46 @@ +/* + * 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. + */ + +// You can use the sample postgres database available at /postgres-deploy/README.md +// +// kamel run PostgresDB.java --dev -d mvn:org.postgresql:postgresql:42.2.21 -d mvn:org.apache.commons:commons-dbcp2:2.8.0 + +import org.apache.camel.builder.RouteBuilder; +import org.apache.commons.dbcp2.BasicDataSource; + +public class PostgresDB extends RouteBuilder { + @Override + public void configure() throws Exception { + registerDatasource(); + + from("timer://foo?period=10000") + .setBody(constant("select * from test")) + .to("jdbc:myPostgresDS") + .to("log:info"); + } + + private void registerDatasource() throws Exception { + BasicDataSource ds = new BasicDataSource(); + ds.setUsername("postgresadmin"); + ds.setDriverClassName("org.postgresql.Driver"); + ds.setPassword("admin123"); + ds.setUrl("jdbc:postgresql://postgres:5432/test"); + + this.getContext().getRegistry().bind("myPostgresDS", ds); + } + +} \ No newline at end of file diff --git a/examples/databases/PostgresDBAutoDatasource.java b/examples/databases/PostgresDBAutoDatasource.java new file mode 100644 index 0000000..6984e33 --- /dev/null +++ b/examples/databases/PostgresDBAutoDatasource.java @@ -0,0 +1,45 @@ +/* + * 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. + */ + +// kamel run PostgresDBAutoDatasource.java --dev +// --build-property quarkus.datasource.camel.db-kind=postgresql +// -p quarkus.datasource.camel.jdbc.url=jdbc:postgresql://postgres:5432/test +// -p quarkus.datasource.camel.username=postgresadmin +// -p quarkus.datasource.camel.password=admin123 +// -d mvn:io.quarkus:quarkus-jdbc-postgresql:1.13.7.Final +// +// Alternatively, you can bundle your credentials as a secret properties file: +// +// kubectl create secret generic my-datasource --from-file=datasource.properties +// +// kamel run PostgresDBAutoDatasource.java --dev +// --build-property quarkus.datasource.camel.db-kind=postgresql +// --config secret:my-datasource +// -d mvn:io.quarkus:quarkus-jdbc-postgresql:1.13.7.Final + +import org.apache.camel.builder.RouteBuilder; + +public class PostgresDBAutoDatasource extends RouteBuilder { + @Override + public void configure() throws Exception { + from("timer://foo?period=10000") + .setBody(constant("select * from test")) + .to("jdbc:camel") + .to("log:info"); + } + +} \ No newline at end of file diff --git a/examples/databases/README.md b/examples/databases/README.md new file mode 100644 index 0000000..301a077 --- /dev/null +++ b/examples/databases/README.md @@ -0,0 +1,3 @@ +# Examples showing how to connect Camel K with databases + +Find useful examples about how to develop a Camel K integration connecting to a database. \ No newline at end of file diff --git a/examples/databases/datasource.properties b/examples/databases/datasource.properties new file mode 100644 index 0000000..587ca7f --- /dev/null +++ b/examples/databases/datasource.properties @@ -0,0 +1,3 @@ +quarkus.datasource.camel.jdbc.url=jdbc:postgresql://postgres:5432/test +quarkus.datasource.camel.username=postgresadmin +quarkus.datasource.camel.password=admin123 \ No newline at end of file diff --git a/examples/databases/postgres-deploy/README.md b/examples/databases/postgres-deploy/README.md new file mode 100644 index 0000000..0b095b4 --- /dev/null +++ b/examples/databases/postgres-deploy/README.md @@ -0,0 +1,30 @@ +# How to deploy a simple Postgres DB to Kubernetes cluster + +This is a very simple example to show how to create a Postgres database. **Note**, this is not ready for any production purposes. + +## Create a Kubernetes Deployment +``` +kubectl create -f postgres-configmap.yaml +kubectl create -f postgres-storage.yaml +kubectl create -f postgres-deployment.yaml +kubectl create -f postgres-service.yaml +``` +## Thest the connection + +Connection credentials available in the _postgres-configmap.yaml_ descriptor. + +``` +kubectl get svc postgres +psql -h <IP> -U postgresadmin1 --password -p <PORT> postgresdb +``` +## Create a test database and table +``` +CREATE DATABASE test; +CREATE TABLE test (data TEXT PRIMARY KEY); +INSERT INTO test(data) VALUES ('hello'), ('world'); +``` +### Read the test database and table +``` +SELECT * FROM test; +``` + diff --git a/examples/databases/postgres-deploy/postgres-configmap.yaml b/examples/databases/postgres-deploy/postgres-configmap.yaml new file mode 100644 index 0000000..c94b6ad --- /dev/null +++ b/examples/databases/postgres-deploy/postgres-configmap.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: postgres-config + labels: + app: postgres +data: + POSTGRES_DB: postgresdb + POSTGRES_USER: postgresadmin + POSTGRES_PASSWORD: admin123 \ No newline at end of file diff --git a/examples/databases/postgres-deploy/postgres-deployment.yaml b/examples/databases/postgres-deploy/postgres-deployment.yaml new file mode 100644 index 0000000..001d2ec --- /dev/null +++ b/examples/databases/postgres-deploy/postgres-deployment.yaml @@ -0,0 +1,30 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgres +spec: + replicas: 1 + selector: + matchLabels: + app: postgres + template: + metadata: + labels: + app: postgres + spec: + containers: + - name: postgres + image: postgres:10.4 + imagePullPolicy: "IfNotPresent" + ports: + - containerPort: 5432 + envFrom: + - configMapRef: + name: postgres-config + volumeMounts: + - mountPath: /var/lib/postgresql/data + name: postgredb + volumes: + - name: postgredb + persistentVolumeClaim: + claimName: postgres-pv-claim \ No newline at end of file diff --git a/examples/databases/postgres-deploy/postgres-service.yaml b/examples/databases/postgres-deploy/postgres-service.yaml new file mode 100644 index 0000000..f3ed357 --- /dev/null +++ b/examples/databases/postgres-deploy/postgres-service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: postgres + labels: + app: postgres +spec: + type: NodePort + ports: + - port: 5432 + selector: + app: postgres \ No newline at end of file diff --git a/examples/databases/postgres-deploy/postgres-storage.yaml b/examples/databases/postgres-deploy/postgres-storage.yaml new file mode 100644 index 0000000..36a8db2 --- /dev/null +++ b/examples/databases/postgres-deploy/postgres-storage.yaml @@ -0,0 +1,29 @@ +kind: PersistentVolume +apiVersion: v1 +metadata: + name: postgres-pv-volume + labels: + type: local + app: postgres +spec: + storageClassName: manual + capacity: + storage: 5Gi + accessModes: + - ReadWriteMany + hostPath: + path: "/mnt/data" +--- +kind: PersistentVolumeClaim +apiVersion: v1 +metadata: + name: postgres-pv-claim + labels: + app: postgres +spec: + storageClassName: manual + accessModes: + - ReadWriteMany + resources: + requests: + storage: 5Gi \ No newline at end of file
