balena-io / balena-cli

The official balena CLI tool.
Apache License 2.0
453 stars 138 forks source link

Handle environment variable interpolation in compositions #2818

Open ab77 opened 1 month ago

ab77 commented 1 month ago

See, https://github.com/balena-io/open-balena/pull/271

balena-cli chokes when presented with a composition containing a valid compose syntax (e.g.):

...
    environment:
      <<: *default-environment
      VPN_PORT: ${VPN_PORT:-443}

.. instead it should parse this by checking if there is an override set for VPN_PORT in build-variables, substituting the interpolation syntax;

if no override is presented, use the default value

ab77 commented 1 month ago

Also blocks https://github.com/balena-io/balena-cloud/pull/3466 (e.g.):

x-default-healthcheck: &default-healthcheck
  test: /usr/src/app/docker-hc
  interval: ${INTERVAL:-45s}
  timeout: ${TIMEOUT:-15s}
  retries: ${RETRIES:-3}

In order to override composition variables like health-check timeouts in balena-compose, we would need to implement runtime resolution of these (if defined) and fall back to default values, if not. Since balena compositions are evaluated at runtime by balena-supervisor, the Supervisor would need to know where to look for these values. It may chose to do this from "device" env-vars (not application/fleet) i.e. same modelling as BALENA_SUPERVISOR_DELTA*, using an appropriate env-var naming scheme (e.g. BALENA_FLEET_200882_INTERVAL). So if a "device" env-var (not fleet override) is set, that value is used to interpolate interval: ${INTERVAL:-45s}.

Ref: https://docs.docker.com/reference/compose-file/interpolation/