Flexible Configuration
KrakenD’s Flexible Configuration allows you to use templates, variables, and logic to build your configuration files dynamically. This is especially useful for managing multiple environments and reducing configuration duplication.Overview
Flexible Configuration uses Go template syntax to:- Use variables from environment-specific files
- Include partial configuration templates
- Implement loops and conditional logic
- Generate the final
krakend.jsonfrom templates
Template File
The main template file is located at:Example Template
Enabling Flexible Configuration
Set theFC_ENABLE environment variable to enable flexible configuration:
- Load the template file instead of
krakend.json - Process all template directives
- Generate the final configuration in memory
Settings Directory Structure
Environment-specific settings are stored in separate directories:Variables and Environment Settings
Using Variables
Variables fromenv.json are accessible via the .env object:
Development Settings
Fromsettings/dev/env.json:
Production Settings
Fromsettings/prod/env.json:
Partials and Templates
Including Partials
Useinclude to insert content from partial files:
Example Partial: extra_config.tmpl
Frompartials/extra_config.tmpl:
Using Templates
Usetemplate to invoke named templates:
. passes the current context (including variables) to the template.
Logic and Iterations
Loops
Iterate over arrays or objects:rangeiterates over.loop_example.dummyarray$idxis the index (0-based){{ if $idx }},{{ end }}adds commas between items (not before first item){{ $endpoint }}is the current array element
Conditionals
Useif statements for conditional logic:
Comparisons
Compiling Templates
The playground includes a Make command to compile templates and save the output:- Processes the template file
- Resolves all variables and includes
- Saves the compiled configuration to a file
Template Functions
Available Functions
| Function | Description | Example |
|---|---|---|
include | Include a partial file | {{ include "partial.tmpl" }} |
template | Invoke a named template | {{ template "tmpl.tmpl" . }} |
range | Iterate over collection | {{ range .items }}...{{ end }} |
if | Conditional logic | {{ if .condition }}...{{ end }} |
eq | Equality comparison | {{ if eq .env "prod" }} |
ne | Not equal | {{ if ne .env "dev" }} |
and | Logical AND | {{ if and .a .b }} |
or | Logical OR | {{ if or .a .b }} |
Best Practices
1. Organize by Environment
Keep environment-specific values insettings/ directories:
2. Use Partials for Reusability
Extract common configuration blocks into partials:3. Document Template Variables
Add comments to yourenv.json files explaining variable purposes.
4. Test Compiled Output
Always compile and validate your templates before deploying:Switching Environments
To switch between development and production:-
Set the
FC_SETTINGSenvironment variable: -
Or specify it when starting KrakenD:
Next Steps
- Learn about environment-specific settings
- View endpoint configuration examples
- Explore the main configuration file structure