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 integration | Setting | Stored in |
|---|---|---|
| Values, such as a timeout or an address | Configuration | A Properties configuration |
| Sensitive values, such as a password | Configuration | A Secure Properties configuration |
| One or two values that differ by environment | Environment | The setting itself |
| A file | Resources | A Resources File configuration |
| More CPU, memory or instances | Compute | The 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.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:
- 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:
| Written | Gives the variable |
|---|---|
ORDERS_REGION=eu-west | The value as written |
DB_PASSWORD=secret:orders-db/password | One value of a stored configuration |
In the route, a variable is read with env::
- 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.
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.
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:
{"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
- 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.
- 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}"}'
{"greeting":"Hello from orders","region":"eu-west"}
| The route reads | It comes from | Set in |
|---|---|---|
{{orders.greeting}} | orders-settings | The Configuration setting |
${env:ORDERS_REGION} | ORDERS_REGION=eu-west | The Environment setting |
/etc/orders/catalog.json | orders-catalog | The Resources setting |
Replicas, CPU and memory (Compute)
Add the setting Compute.
| Field | Default | When to change it |
|---|---|---|
| Replicas | 1 | Two or more so that the integration keeps answering while one instance restarts |
| CPU Request, Memory Request | 125m, 128Mi | What the integration is guaranteed |
| CPU Limit, Memory Limit | 500m, 512Mi | The 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.
{{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.
| Setting | What it does |
|---|---|
| Camel Runtime | Runtime properties added to the application |
| Build | Build-time properties, and the CPU and memory of the build |
| JVM | JVM flags, debugging, trusted certificate authorities (next step) |
| Health Probes | When the integration is considered alive and ready |
| Storage | Storage that survives restarts, and temporary directories |
| Disruption Budget | How 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.