Test environment to check Flexible configuration feature.
This repo contains a basic implementation for flexible configuration, including the use of variables, templates, code snippets and some basic logic to iterate over multiple endpoints.
krakend.tmpl: the base file, including calls to the variables, iteration over available endpoints and some code snippets.Dockerfile: A docker definition to build an immutable container with KrakenDdocker-compose.ymlan example docker compose definition file to be able to execute KrakenD enabling Flexible Configuration.config/partials/*: some code snippets referenced from base fileconfig/templates/*: a template referenced from base fileconfig/settings/{dev|prod}/endpoint.json: a collection of endpointsconfig/settings/{dev|prod}/service.json: basic configuration parameters for the service
$ docker run \
--rm -it -p "8080:8080" \
-v "$PWD:/etc/krakend" \
-e FC_ENABLE=1 \
-e FC_SETTINGS=config/settings/prod \
-e FC_PARTIALS=config/partials \
-e FC_TEMPLATES=config/templates \
-e FC_OUT=./tmp/out.json \
-e SERVICE_NAME="KrakenD API Gateway" \
devopsfaith/krakend check -tdc "krakend.tmpl"Based on the definition included in the docker-compose.yml definition.
$ docker-compose up$ FC_ENABLE=1 \
FC_SETTINGS=config/settings/prod \
FC_PARTIALS=config/partials \
FC_TEMPLATES=config/templates \
FC_OUT=out.json \
SERVICE_NAME="KrakenD API Gateway" \
krakend check -tdc "krakend.tmpl"Note: both above alternatives will output a out.json file with the compiled version of the config file, useful for debugging purposes.
If you use containers, the recommended approach is to write your own Dockerfile and deploy an immutable artifact (embedding the config).
$ docker build --build-arg ENV=prod -t mykrakend . This will generate a ready-to-use container named mykrakend with the configuration already compiled, checked and validated using the linter (based on the Dockerfile included in this repo).
To run this new container, you just need to execute:
$ docker run -p 8080:8080 mykrakend run -dc krakend.json| 💡 Bonus Track - Invoking a Dynamic List of Endpoints from the Main Configuration |
|---|
Go template functions do not allow the use of dynamic values as paths to templates, which means you cannot iterate a dynamic list of template paths and invoke them from your krakend.tmpl.However, you can easily overcome this limitation by generating a nesting template with a simple bash command, such as: tree -J -I "endpoints.tmpl" | jq -r ' ( .[0].contents[].name | "{{ template \"\(.)\" . }}," )' | sed '$s/,$//' > endpoints.tmplExecuting this command from the config/templates folder will generate a new endpoints.tmpl, which will invoke all available templates, comma-separated. You can then simply invoke that endpoints.tmpl from your main configuration. You will only need to execute that command again whenever you add new endpoints to the templates folder. |

