Search code examples
quarkusmicroprofilesmallryeeclipse-microprofile-config

How to convert a collection in config to environment variable in Microprofile/Quarkus/Smallrye


We are running our apps in a K8 Cluster and rely on the configuration by environment variables. For the conversion of application.properties/application.yaml parameters in Quarkus, the following conversion rules apply: https://github.com/eclipse/microprofile-config/blob/master/spec/src/main/asciidoc/configsources.asciidoc#default-configsources

In this rule it is not mentioned how to convert collections.

Let's say I have the following config:

server.environments[0].name=dev
server.environments[0].apps[0].name=rest
server.environments[0].apps[0].services=bookstore,registration
server.environments[0].apps[0].databases=pg,h2
server.environments[0].apps[1].name=batch
server.environments[0].apps[1].services=stock,warehouse

How would I convert it to an environment variable?

I've tried the following:

SERVER_ENVIRONMENT_0_APPS_0_DATABASES
SERVER_ENVIRONMENT[0]_APPS[0]_DATABASES

No chance to make it work. Does anyone know how to do this? Is this supported anyway?


Solution

  • You were pretty close, just follow the rules mentioned in the docu:

    1. Replace each character that is neither alphanumeric nor _ with _; then convert the name to upper case (i.e. COM_ACME_SIZE)

    So given we have a config property named server.environments[0].apps[0].name when you replace each non-alfanumeric character with _ and convert to upper case you end up with: SERVER_ENVIRONMENTS_0__APPS_0__NAME. Note the double underscore between 0 and APPS as you substitute both . and [ for _. That will certainly not win any prize for the prettiest env var name but it does the job :).

    You can check how exactly it is done in the Smallrye implementation of MP config - which is the implementation used by Quarkus.