ReasonDiscarded because discussion stalled and there's no intention to work on this for the moment.

Status

Current state: Discarded

Discussion threadhttp://apache-flink-mailing-list-archive.1008284.n3.nabble.com/DISCUSS-FLIP-161-Configuration-through-environment-variables-td48094.html (discussion prior to FLIP)

JIRA: –

Released: 

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

Motivation

Flink currently requires configuration to be written to file. By allowing to override this configuration through environment variables, configuration can be made much more flexible. This becomes particularly useful in Kubernetes scenarios where some of the configuration can be defined through secrets exposed as environment variables, e.g. access keys. Furthermore, Flink can benefit from this internally as well as this mechanism provides an easy way to randomize end-to-end test configuration, see FLINK-19520.

Public Interfaces

No public interfaces apart from configuration are affected. Flink configuration is also not affected directly, but indirectly by virtue of allowing environment variable to override entries in the Flink configuration.

Proposed Changes

After the Flink configuration has been parsed from the file, environment variables (following a clearly defined naming schema, see below) are taken into consideration and are allowed to amend or even replace the parsed configuration. Allowing environment variables to take precedence over the configured values is a deliberate and important choice.

For example, with an environment

FLINK_CONFIG_KEY_A="Environment=A"
FLINK_CONFIG_KEY_B="Environment=B"

and a configuration file

key.a: File=A
key.c: File=C

the resulting, effective configuration would be equivalent to a configuration of 

key.a: Environment=A
key.c: File=C
key.b: Environment=B

Naming of environment variables

A key limitation is that environment variables cannot be enforced to be named exactly like their configuration key counter-part. This stems from two reason:

  1. Some systems / shells do not support "." or "-" in variable names.
  2. Environment variables are idiomatically named using uppercase (e.g., $HOME and not $Home or $home), and are actually case-sensitive.

Due to this, a convention needs to be established on how configuration keys are looked up in the environment. We propose that environment variables for the Flink configuration (e.g. key.A-b ) follow the following schema:

  1. Prefix "FLINK_CONFIG_" → FLINK_CONFIG_key.A-b 
  2. Replace "." (period) with "_" (underscore) → FLINK_CONFIG_key_A-b 
  3. Replace "-" (dash) with "__" (double underscore) → FLINK_CONFIG_key_A__b 
  4. Uppercase → FLINK_CONFIG_KEY_A__B 

This provides a (semi-)bijective function between environment variable name and configuration key. More specifically, it allows parsing configuration keys from the environment without having to have prior knowledge of available configuration keys. Given an environment, we can look for all environment variables starting with the FLINK_CONFIG_ prefix and map them to their configuration key counterpart by following the inverse procedure:

  1. Remove FLINK_CONFIG_ prefix → KEY_A__B 
  2. Replace "__" (double underscore) with "-" (dash) → KEY_A-B 
  3. Replace "_" (underscore) with "." (period) → KEY.A-B 
  4. Lowercase → key.a-b 

As we can see, this yields the original (intended) configuration key, with the only difference being the casing. Configuration currently treats keys case-sensitively, but we propose to relax this requirement and treat them case-insensitively during the lookup of a specific key.

This mapping is not strictly bijective, but cases with consecutive periods or dashes in the key name are not considered here and should not (reasonably) be allowed. This should therefore be enforced in the implementation as well to prevent further development to run into such scenarios.

Implementation Notes

This proposal affects two code paths:

  1. In GlobalConfiguration, parsing the environment given the procedure above will be implemented. This overrides any configuration present from the configuration file.
  2. In Configuration, looking up a key in the internal data structure is changed to become case-insensitive.

Compatibility, Deprecation, and Migration Plan

No impact on existing users is expected. Hypothetically, if the environment happens to include a variable with a matching name, this change would cause a change in behavior. We consider this risk to be negligibly low due to the chosen naming schema, however. 

Test Plan

These changes can be covered entirely through unit tests against Configuration and GlobalConfiguration.

Rejected Alternatives

Substitution

Initially, we also discussed a substitution solution where users modify their Flink configuration to use an environment variable substitution syntax such as 

key.a: ${ENV_VAR_A}
key.b: ${ENV_BAR_B} ms


This approach has been rejected for several reasons:

  1. It changes the syntax of the configuration and would require additional details to be added to the syntax, i.e. to define default/fallback values and to escape variables so that they are not replaced.
  2. It requires the introduction of a new set of environment variables users have to memorize.
  3. If users want full flexibility to override any value, a configuration file would have to be maintained which simply maps all keys to some environment variable.


Lazy Evaluation

We initially proposed a naming schema for environment variables which closely follows how Spring does it, which includes several different alternatives per configuration key. However, this approach requires either complete knowledge of all configuration keys upfront, or lazy evaluation of the environment when a configuration key is looked up. During the discussion it was decided that neither approach seems favorable or very feasible.

2 Comments

  1. This whole thing is very similar to typesafe.config which gives most of the functionalities described here out of the box. Maybe just use it

    1. Thanks, Boris! I'm personally not opposed to using a library, but we should first make sure we reach consensus on how things behave and then we can certainly also revisit whether an external library is a good fit.