This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new cc20044362f CAMEL-18171: camel-kubernetes - Add secret/configmap
property placeholder function.
cc20044362f is described below
commit cc20044362fe272d664bc60cd21920a860672f21
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Jun 28 14:44:53 2022 +0200
CAMEL-18171: camel-kubernetes - Add secret/configmap property placeholder
function.
---
.../properties/BasePropertiesFunction.java | 34 +++++++++----
.../ConfigMapPropertiesFunctionLocalModeTest.java | 55 ++++++++++++++++++++++
.../ROOT/pages/using-propertyplaceholder.adoc | 46 ++++++++++++++----
3 files changed, 118 insertions(+), 17 deletions(-)
diff --git
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BasePropertiesFunction.java
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BasePropertiesFunction.java
index 46ac5af6753..c084fc785d2 100644
---
a/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BasePropertiesFunction.java
+++
b/components/camel-kubernetes/src/main/java/org/apache/camel/component/kubernetes/properties/BasePropertiesFunction.java
@@ -50,9 +50,10 @@ import org.slf4j.LoggerFactory;
abstract class BasePropertiesFunction extends ServiceSupport implements
PropertiesFunction, CamelContextAware {
// keys in application.properties
- public static final String CLIENT_ENABLED =
"camel.kubernetes.client.enabled";
- public static final String MOUNT_PATH_CONFIGMAPS =
"camel.kubernetes.mount-path-configmaps";
- public static final String MOUNT_PATH_SECRETS =
"camel.kubernetes.mount-path-secrets";
+ public static final String CLIENT_ENABLED =
"camel.kubernetes-config.client-enabled";
+ public static final String LOCAL_MODE =
"camel.kubernetes-config.local-mode";
+ public static final String MOUNT_PATH_CONFIGMAPS =
"camel.kubernetes-config.mount-path-configmaps";
+ public static final String MOUNT_PATH_SECRETS =
"camel.kubernetes-config.mount-path-secrets";
// use camel-k ENV for mount paths
public static final String ENV_MOUNT_PATH_CONFIGMAPS =
"camel.k.mount-path.configmaps";
@@ -63,6 +64,7 @@ abstract class BasePropertiesFunction extends ServiceSupport
implements Properti
private CamelContext camelContext;
private KubernetesClient client;
+ private Boolean localMode;
private Boolean clientEnabled;
private String mountPathConfigMaps;
private String mountPathSecrets;
@@ -71,6 +73,16 @@ abstract class BasePropertiesFunction extends ServiceSupport
implements Properti
@SuppressWarnings("unchecked")
protected void doInit() throws Exception {
ObjectHelper.notNull(camelContext, "CamelContext");
+ if (localMode == null) {
+ localMode = "true"
+
.equalsIgnoreCase(camelContext.getPropertiesComponent().resolveProperty(LOCAL_MODE).orElse("false"));
+ }
+ if (!localMode) {
+ doInitKubernetesClient();
+ }
+ }
+
+ protected void doInitKubernetesClient() throws Exception {
if (clientEnabled == null) {
clientEnabled = "true"
.equalsIgnoreCase(camelContext.getPropertiesComponent().resolveProperty(CLIENT_ENABLED).orElse("true"));
@@ -90,11 +102,8 @@ abstract class BasePropertiesFunction extends
ServiceSupport implements Properti
// try to auto-configure via properties
PropertiesComponent pc = camelContext.getPropertiesComponent();
OrderedLocationProperties properties = (OrderedLocationProperties)
pc
- .loadProperties(k ->
k.startsWith("camel.kubernetes.client."),
- k -> k.replace("camel.kubernetes.client.", ""));
- // used for enabling this
- properties.remove("enabled");
-
+ .loadProperties(k ->
k.startsWith("camel.kubernetes-config.client."),
+ k -> k.replace("camel.kubernetes-config.client.",
""));
if (!properties.isEmpty()) {
ConfigBuilder config = new ConfigBuilder();
@@ -135,7 +144,8 @@ abstract class BasePropertiesFunction extends
ServiceSupport implements Properti
}
if (!copy.isEmpty()) {
for (var e : copy.entrySet()) {
- LOG.warn("Property not auto-configured:
camel.kubernetes.client.{}={}", e.getKey(), e.getValue());
+ LOG.warn("Property not auto-configured:
camel.kubernetes-config.client.{}={}", e.getKey(),
+ e.getValue());
}
}
} else {
@@ -222,6 +232,12 @@ abstract class BasePropertiesFunction extends
ServiceSupport implements Properti
return defaultValue;
}
+ // local-mode will not lookup in kubernetes but as local properties
+ if (localMode) {
+ String localKey = name + "/" + key;
+ return
getCamelContext().getPropertiesComponent().resolveProperty(localKey).orElse(defaultValue);
+ }
+
String answer = null;
Path root = getMountPath();
if (root != null) {
diff --git
a/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionLocalModeTest.java
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionLocalModeTest.java
new file mode 100644
index 00000000000..a82daa95e3e
--- /dev/null
+++
b/components/camel-kubernetes/src/test/java/org/apache/camel/component/kubernetes/properties/ConfigMapPropertiesFunctionLocalModeTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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 org.apache.camel.component.kubernetes.properties;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.kubernetes.KubernetesTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Order;
+import org.junit.jupiter.api.Test;
+
+public class ConfigMapPropertiesFunctionLocalModeTest extends
KubernetesTestSupport {
+
+ @Override
+ protected RoutesBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start")
+ .transform().simple("Hello ${body} we are at
{{configmap:myconfig/bar}}");
+ }
+ };
+ }
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+
context.getPropertiesComponent().addInitialProperty(ConfigMapPropertiesFunction.LOCAL_MODE,
"true");
+ context.getPropertiesComponent().addInitialProperty("myconfig/bar",
"The Local Bar");
+ return context;
+ }
+
+ @Test
+ @Order(1)
+ public void configMapLocalMode() throws Exception {
+ String out = template.requestBody("direct:start", "Jack",
String.class);
+ Assertions.assertEquals("Hello Jack we are at The Local Bar", out);
+ }
+
+}
diff --git a/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
b/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
index 6bb124812cc..d7ec74932a5 100644
--- a/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
+++ b/docs/user-manual/modules/ROOT/pages/using-propertyplaceholder.adoc
@@ -508,8 +508,8 @@ Camel will first use _mount paths_ (if configured) to
lookup, and then fallback
The configuration of the _month path_ are used by the given order:
-1. Reading configuration property with keys
`camel.kubernetes.mount-path-configmaps`
-and `camel.kubernetes.mount-path-secrets`.
+1. Reading configuration property with keys
`camel.kubernetes-config.mount-path-configmaps`
+and `camel.kubernetes-config.mount-path-secrets`.
2. Use JVM system property with key `camel.k.mount-path.configmaps` and
`camel.k.mount-path.secrets` (Camel K compatible).
3. Use OS ENV variable with key `camel.k.mount-path.configmaps` and
`camel.k.mount-path.secrets` (Camel K compatible).
@@ -517,8 +517,8 @@ For example to use `/etc/camel/resources/` as mount path,
you can configure this
[source,properties]
----
-camel.kubernetes.mount-path-configmaps = /etc/camel/myconfig/
-camel.kubernetes.mount-path-secrets = /etc/camel/mysecrets/
+camel.kubernetes-config.mount-path-configmaps = /etc/camel/myconfig/
+camel.kubernetes-config.mount-path-secrets = /etc/camel/mysecrets/
----
==== Configuring Kubernetes Client
@@ -526,7 +526,7 @@ camel.kubernetes.mount-path-secrets = /etc/camel/mysecrets/
Camel will autowire the `KubernetesClient` if a single instance of the client
exists in the running application (lookup via the xref:registry.adoc[Registry]).
Otherwise, a new `KubernetesClient` is created. The client can be configured
from either
-- Using `camel.kubernetes.client.` properties (see below for example)
+- Using `camel.kubernetes-config.client.` properties (see below for example)
- Attempt to auto-configure itself by a combination of OS Environment
variables, reading from `~./kube/config` configuration,
and service account token file. For more details see the
https://github.com/fabric8io/kubernetes-client documentation.
@@ -536,8 +536,8 @@ such as the masterUrl and oauthToken as shown:
[source,properties]
----
-camel.kubernetes.client.masterUrl = https://127.0.0.1:50179/
-camel.kubernetes-client.oauthToken = eyJhbGciOiJSUzI1NiIsImtpZCI...
+camel.kubernetes-config.client.masterUrl = https://127.0.0.1:50179/
+camel.kubernetes-config.client.oauthToken = eyJhbGciOiJSUzI1NiIsImtpZCI...
----
The `KubernetesClient` has many options, see the
https://github.com/fabric8io/kubernetes-client documentation.
@@ -546,7 +546,7 @@ If you only use _mount paths_ then its good practice to
disable `KubernetesClien
[source,properties]
----
-camel.kubernetes.client.enabled = false
+camel.kubernetes-config.client-enabled = false
----
When running your Camel applications inside an existing Kubernetes cluster,
then you often
@@ -647,6 +647,36 @@ Which reduces the route to:
</camelContext>
----
+==== Using configmap or secrets in local-mode
+
+During development you may want to run in _local mode_ where you do not need
acces to a Kubernetes cluster, to lookup the configmap.
+In the local mode, then Camel will lookup the configmap _keys_ from local
properties, eg:
+
+For example the example above with the postgresql kamelet, that was configured
using a secret:
+
+[source,properties]
+----
+camel.component.kamelet.postgresql-sink.databaseName={{secret:mydb/myhost}}
+camel.component.kamelet.postgresql-sink.serverPort={{secret:mydb/myport}}
+camel.component.kamelet.postgresql-sink.username={{secret:mydb/myuser}}
+camel.component.kamelet.postgresql-sink.password={{secret:mydb/mypass}}
+----
+
+Now suppose we have a local Postrgres database we want to use, then we can
turn on _local mode_
+and specify the credentials in the same properties file:
+
+[source,properties]
+----
+camel.kubernetes-config.local-mode = true
+mydb/myhost=localhost
+mydb/myport=1234
+mydb/myuser=scott
+mydb/mypass=tiger
+----
+
+NOTE: Notice how the key is prefixed with the name of the secret and a slash,
eg `name/key`. This makes it easy to copy/paste
+from the actual use of the configmap/secret and into the
`application.properties` file.
+
=== Using custom property placeholder functions
The xref:components::properties-component.adoc[Properties] component allow to
plugin 3rd party functions which can be used during parsing of the property
placeholders.