Skip to main content

3. Externalize the Configuration

So far everything is written in the route. A real integration takes its values from outside, so that the same route runs in development and in production with different settings.

An integration is configured through settings that you add to it one by one. This step adds four.

You want to give the integrationSettingStored in
Values, such as a timeout or an addressConfigurationA Properties configuration
Sensitive values, such as a passwordConfigurationA Secure Properties configuration
One or two values that differ by environmentEnvironmentThe setting itself
A fileResourcesA Resources File configuration
More CPU, memory or instancesComputeThe setting itself

Why externalize​

A route that contains the address of the partner, the batch size and the password works in one place only. Externalized, the same route (the same release) runs in development and in production, each with its own values, and a credential is changed without touching the code.

Three mechanisms are used here, each for what it suits: properties for settings read by name, environment variables for a few values the runtime or a library expects, files for content the route reads as a whole.

Camel properties from a stored configuration​

Create the Properties Configuration​

In Secret Manager, create a configuration of the kind Properties Configuration, named orders-settings:

orders-settings — a Properties Configuration
orders.greeting=Hello from orders
orders.page-size=20

Make it available to the environment the integration runs in.

Mount the properties with the Configuration setting​

On orders-api, add the setting Configuration and choose orders-settings in Property files. You can also create the configuration from this field with New secret.

Property placeholders in the route​

A property is read between double braces:

orders.camel.yaml
- setBody:
simple: '{"greeting":"{{orders.greeting}}","pageSize":{{orders.page-size}}}'

Hot Reload makes the integration reload when the configuration changes, without being deployed again.

For passwords and other sensitive values, use a Secure Properties Configuration in the same way: its content is hidden in the console.

Environment variables​

Add the setting Environment. In Environment Variables, each line is one variable:

WrittenGives the variable
ORDERS_REGION=eu-westThe value as written
DB_PASSWORD=secret:orders-db/passwordOne value of a stored configuration

In the route, a variable is read with env::

orders.camel.yaml
- setBody:
simple: '{"region":"${env:ORDERS_REGION}"}'

Two switches are on by default: the integration is told its own namespace and instance name, and the proxy settings of the environment are passed on to it.

Properties or variables?

Use properties for the configuration of the application: they are grouped, named and stored once. Use variables for the few values a library expects as variables, or that you set by hand for one environment.

Environment variables typed in clear are not secret

A variable written in the setting is stored with the integration and shown to whoever can open it. For a password or a token, read the variable from a stored configuration, or use Secure Properties.

Mounted resource files​

Create the Resources File configuration​

In Secret Manager, create a configuration of the kind Resources File, named orders-catalog, and drop a file catalog.json in it:

catalog.json — in orders-catalog, a Resources File
{"items":["keyboard","mouse","screen"]}

Mount path of the resource​

On orders-api, add the setting Resources. In Files, choose orders-catalog, then set the place it is mounted at, /etc/orders for example. The file is then at /etc/orders/catalog.json.

Read the mounted file from the route​

orders.camel.yaml
- setBody:
simple: 'resource:file:/etc/orders/catalog.json'

A configuration can hold several files: they are all mounted in the same place.

Complete route with placeholders​

With the first two settings in place, the route reads its greeting from the property file and its region from a variable. The highlighted line is the one that changed since step 1.

orders.camel.yaml
- route:
id: orders
from:
uri: platform-http:/orders
parameters:
httpMethodRestrict: GET
steps:
- setHeader:
name: Content-Type
constant: application/json
- setBody:
simple: '{"greeting":"{{orders.greeting}}","region":"${env:ORDERS_REGION}"}'
Answer
{"greeting":"Hello from orders","region":"eu-west"}
The route readsIt comes fromSet in
{{orders.greeting}}orders-settingsThe Configuration setting
${env:ORDERS_REGION}ORDERS_REGION=eu-westThe Environment setting
/etc/orders/catalog.jsonorders-catalogThe Resources setting

Replicas, CPU and memory (Compute)​

Add the setting Compute.

FieldDefaultWhen to change it
Replicas1Two or more so that the integration keeps answering while one instance restarts
CPU Request, Memory Request125m, 128MiWhat the integration is guaranteed
CPU Limit, Memory Limit500m, 512MiThe most it may use. Raise the memory when the integration is stopped for using too much

Rollout without an image rebuild​

Choose Apply changes. None of these settings rebuilds the image: the integration restarts with its new configuration.

Give a default where one makes sense

{{orders.batch.size:200}} keeps the route working when the property is absent. Do not give a default to a credential or an address: failing at startup is better than calling the wrong system.

Per-environment overrides​

Everything above applies to every environment the integration is bound to. To give one environment a different value (more memory in production, another property file), select that environment on the integration and set the value there: it overrides the common one for that environment only.

Other integration settings​

This step used four settings. The others follow the same pattern: add the capability, fill it in, apply.

SettingWhat it does
Camel RuntimeRuntime properties added to the application
BuildBuild-time properties, and the CPU and memory of the build
JVMJVM flags, debugging, trusted certificate authorities (next step)
Health ProbesWhen the integration is considered alive and ready
StorageStorage that survives restarts, and temporary directories
Disruption BudgetHow many instances must stay up during maintenance

Several settings can mount files into the same integration: what each one mounts is added to what the others mount.

Next: add a private CA to the JVM truststore.