Kubernetes ConfigMap
Since Camel 2.17
Both producer and consumer are supported
The Kubernetes ConfigMap component is one of Kubernetes Components which provides a producer to execute Kubernetes ConfigMap operations and a consumer to consume events related to ConfigMap objects.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties
,*.yaml
files, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Kubernetes ConfigMap component supports 4 options, which are listed below.
Name | Description | Default | Type |
---|---|---|---|
Autowired To use an existing kubernetes client. |
KubernetesClient |
||
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
|
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. |
true |
boolean |
Endpoint Options
The Kubernetes ConfigMap endpoint is configured using URI syntax:
kubernetes-config-maps:masterUrl
With the following path and query parameters:
Path Parameters (1 parameters)
Name | Description | Default | Type |
---|---|---|---|
Required URL to a remote Kubernetes API server. This should only be used when your Camel application is connecting from outside Kubernetes. If you run your Camel application inside Kubernetes, then you can use local or client as the URL to tell Camel to run in local mode. If you connect remotely to Kubernetes, then you may also need some of the many other configuration options for secured connection with certificates, etc. |
String |
Query Parameters (33 parameters)
Name | Description | Default | Type |
---|---|---|---|
The Kubernetes API Version to use. |
String |
||
The dns domain, used for ServiceCall EIP. |
String |
||
Default KubernetesClient to use if provided. |
KubernetesClient |
||
The namespace. |
String |
||
The port name, used for ServiceCall EIP. |
String |
||
The port protocol, used for ServiceCall EIP. |
tcp |
String |
|
The Consumer CRD Resource Group we would like to watch. |
String |
||
The Consumer CRD Resource name we would like to watch. |
String |
||
The Consumer CRD Resource Plural we would like to watch. |
String |
||
The Consumer CRD Resource Scope we would like to watch. |
String |
||
The Consumer CRD Resource Version we would like to watch. |
String |
||
The Consumer Label key when watching at some resources. |
String |
||
The Consumer Label value when watching at some resources. |
String |
||
The Consumer pool size. |
1 |
int |
|
The Consumer Resource Name we would like to watch. |
String |
||
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
false |
boolean |
|
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. |
ExceptionHandler |
||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
|
ExchangePattern |
||
Producer operation to do on Kubernetes. |
String |
||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. |
false |
boolean |
|
Connection timeout in milliseconds to use when making requests to the Kubernetes API server. |
Integer |
||
The CA Cert Data. |
String |
||
The CA Cert File. |
String |
||
The Client Cert Data. |
String |
||
The Client Cert File. |
String |
||
The Key Algorithm used by the client. |
String |
||
The Client Key data. |
String |
||
The Client Key file. |
String |
||
The Client Key Passphrase. |
String |
||
The Auth Token. |
String |
||
Password to connect to Kubernetes. |
String |
||
Define if the certs we used are trusted anyway or not. |
Boolean |
||
Username to connect to Kubernetes. |
String |
Message Headers
The Kubernetes ConfigMap component supports 8 message header(s), which is/are listed below:
Name | Description | Default | Type |
---|---|---|---|
CamelKubernetesOperation (producer) Constant: |
The Producer operation. |
String |
|
CamelKubernetesNamespaceName (producer) Constant: |
The namespace name. |
String |
|
CamelKubernetesConfigMapsLabels (producer) Constant: |
The ConfigMap labels. |
Map |
|
CamelKubernetesConfigMapName (producer) Constant: |
The ConfigMap name. |
String |
|
CamelKubernetesConfigData (producer) Constant: |
The ConfigMap Data. |
Map |
|
CamelKubernetesConfigMapsAnnotations (producer) Constant: |
The ConfigMap annotations. |
Map |
|
CamelKubernetesEventAction (consumer) Constant: |
Action watched by the consumer. Enum values:
|
Action |
|
CamelKubernetesEventTimestamp (consumer) Constant: |
Timestamp of the action watched by the consumer. |
long |
Examples
Kubernetes ConfigMaps Producer Examples
-
listConfigMaps
: this operation lists the configmaps
from("direct:list").
to("kubernetes-config-maps:///?kubernetesClient=#kubernetesClient&operation=listConfigMaps").
to("mock:result");
This operation returns a List of ConfigMaps from your cluster
-
listConfigMapsByLabels
: this operation lists the configmaps selected by label
from("direct:listByLabels").process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
Map<String, String> labels = new HashMap<>();
labels.put("key1", "value1");
labels.put("key2", "value2");
exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_CONFIGMAPS_LABELS, labels);
}
});
to("kubernetes-config-maps:///?kubernetesClient=#kubernetesClient&operation=listConfigMapsByLabels").
to("mock:result");
This operation returns a List of ConfigMaps from your cluster, using a label selector (with key1 and key2, with value value1 and value2)
Kubernetes ConfigMaps Consumer Example
fromF("kubernetes-config-maps://%s?oauthToken=%s", host, authToken)
.process(new KubernetesProcessor()).to("mock:result");
public class KubernetesProcessor implements Processor {
@Override
public void process(Exchange exchange) throws Exception {
Message in = exchange.getIn();
ConfigMap cm = exchange.getIn().getBody(ConfigMap.class);
log.info("Got event with configmap name: " + cm.getMetadata().getName() + " and action " + in.getHeader(KubernetesConstants.KUBERNETES_EVENT_ACTION));
}
}
This consumer returns a message per event received for all ConfigMaps from all namespaces in the cluster.
You can narrow the scope of the consumer using the following query parameter combinations:
-
labelKey
+labelValue
- Watch ConfigMaps with the specified label in any namespace. -
namespace
- Watch all ConfigMaps in the specified namespace. -
namespace
+resourceName
- Watch the ConfigMap with the specified name in the given namespace. -
namespace
+labelKey
+labelValue
- Watch ConfigMaps with the specified label in the given namespace.
Using configmap properties function with Kubernetes
The camel-kubernetes
component include the following configmap related functions:
-
configmap
- A function to lookup the string property from Kubernetes ConfigMaps. -
configmap-binary
- A function to lookup the binary property from Kubernetes ConfigMaps.
Camel reads Configmaps from the Kubernetes API Server. And when RBAC is enabled on the cluster, the ServiceAccount that is used to run the application needs to have the proper permissions for such access.
Before the Kubernetes property placeholder functions can be used they need to be configured with either (or both)
-
path - A mount path that must be mounted to the running pod, to load the configmaps or secrets from local disk.
-
kubernetes client - Autowired An
io.fabric8.kubernetes.client.KubernetesClient
instance to use for connecting to the Kubernetes API server.
Camel will first use mount paths (if configured) to lookup, and then fallback to use the KubernetesClient
.
Using configmap with Kubernetes
Given a configmap named myconfig
in Kubernetes that has two entries:
drink = beer
first = Carlsberg
Then these values can be used in your Camel routes such as:
<camelContext>
<route>
<from uri="direct:start"/>
<log message="What {{configmap:myconfig/drink}} do you want?"/>
<log message="I want {{configmap:myconfig/first}}"/>
</route>
</camelContext>
You can also provide a default value in case a key does not exist:
<log message="I want {{configmap:myconfig/second:Heineken}}"/>
Automatic Camel context reloading on Configmaps Refresh
Being able to reload Camel context on a Configmap Refresh could be done by specifying the following properties:
camel.vault.kubernetescm.refreshEnabled=true
camel.vault.kubernetescm.configmaps=Configmap
camel.main.context-reload-enabled = true
where camel.vault.kubernetescm.refreshEnabled
will enable the automatic context reload and camel.vault.kubernetescm.configmaps
is a regex representing or a comma separated lists of the configmaps we want to track for updates.
Whenever a configmap listed in the property, will be updated in the same namespace of the running application, the Camel context will be reloaded, refreshing the configmap value.