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

commit 4bf48eaf2a017f213820a4d33663c6c7346fd739
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Jun 24 16:43:58 2022 +0200

    CAMEL-18171: camel-kubernetes - Allow to configure k8s client from 
application.properties.
---
 .../properties/BasePropertiesFunction.java         | 45 ++++++++++++++++++++++
 1 file changed, 45 insertions(+)

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 b8ff0740bb5..c78fbcd9e37 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
@@ -21,21 +21,33 @@ import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Locale;
+import java.util.Map;
 
+import io.fabric8.kubernetes.client.ConfigBuilder;
+import io.fabric8.kubernetes.client.DefaultKubernetesClient;
 import io.fabric8.kubernetes.client.KubernetesClient;
 import org.apache.camel.CamelContext;
 import org.apache.camel.CamelContextAware;
+import org.apache.camel.spi.PropertiesComponent;
 import org.apache.camel.spi.PropertiesFunction;
 import org.apache.camel.support.CamelContextHelper;
+import org.apache.camel.support.PropertyBindingSupport;
 import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.LocationHelper;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.OrderedLocationProperties;
+import org.apache.camel.util.SensitiveUtils;
 import org.apache.camel.util.StringHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Base for kubernetes {@link PropertiesFunction}.
  */
 abstract class BasePropertiesFunction extends ServiceSupport implements 
PropertiesFunction, CamelContextAware {
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(BasePropertiesFunction.class);
+
     // keys in application.properties for mount paths
     public static final String MOUNT_PATH_CONFIGMAPS = 
"org.apache.camel.component.kubernetes.properties.mount-path-configmaps";
     public static final String MOUNT_PATH_SECRETS = 
"org.apache.camel.component.kubernetes.properties.mount-path-secrets";
@@ -50,6 +62,7 @@ abstract class BasePropertiesFunction extends ServiceSupport 
implements Properti
     private String mountPathSecrets;
 
     @Override
+    @SuppressWarnings("unchecked")
     protected void doInit() throws Exception {
         ObjectHelper.notNull(camelContext, "CamelContext");
         if (mountPathConfigMaps == null) {
@@ -63,6 +76,38 @@ abstract class BasePropertiesFunction extends ServiceSupport 
implements Properti
         if (client == null) {
             client = CamelContextHelper.findSingleByType(camelContext, 
KubernetesClient.class);
         }
+        if (client == null) {
+            // try to auto-configure via properties
+            PropertiesComponent pc = camelContext.getPropertiesComponent();
+            OrderedLocationProperties properties = (OrderedLocationProperties) 
pc
+                    .loadProperties(k -> 
k.startsWith("camel.kubernetes-client.") || 
k.startsWith("camel.kubernetesClient."));
+            if (!properties.isEmpty()) {
+                ConfigBuilder config = new ConfigBuilder();
+                PropertyBindingSupport.build()
+                        .withProperties((Map) properties)
+                        .withFluentBuilder(true)
+                        .withIgnoreCase(true)
+                        .withReflection(true)
+                        .withTarget(config)
+                        .withCamelContext(camelContext)
+                        .bind();
+                client = new DefaultKubernetesClient(config.build());
+                LOG.info("Auto-configuration 
io.fabric8.kubernetes.client.KubernetesClient summary");
+                for (var entry : properties.entrySet()) {
+                    String k = entry.getKey().toString();
+                    Object v = entry.getValue();
+                    String loc = LocationHelper.locationSummary(properties, k);
+                    if (SensitiveUtils.containsSensitive(k)) {
+                        LOG.info("    {} {}=xxxxxx", loc, k);
+                    } else {
+                        LOG.info("    {} {}={}", loc, k, v);
+                    }
+                }
+                // add to registry so the client can be reused
+                camelContext.getRegistry().bind("camelKubernetesClient", 
client);
+            }
+        }
+
         if (client == null && getMountPath() == null) {
             throw new IllegalArgumentException("Either a mount path or the 
Kubernetes Client must be configured");
         }

Reply via email to