Variable indirection in `env {}` stanzas
- Dominant language
- Go
- Stars
- 17k
- Forks
- 2.1k
- Avg merge
- 1d 9h
- Merged PRs (30d)
- 105
Description
### Proposal
Bash has a feature called variable indirection that lets you read _the value_ of a value. The following example exemplifies it:
```
original_value="hey, im the original value"
original_value_env_var_name="original_value"
echo "${!original_value_env_var_name}"
>> hey, im the original value
echo "${original_value_env_var_name}"
>> original_value
```
I propose that this could potentially be useful in Nomad env{} stanzas (or perhaps elsewhere in its interpolation engine)!
### Use-cases
I have an upstream with a name that changes based on `var`s. This makes it difficult to statically pass its NOMAD_UPSTREAM_ADDR into an `env{}`. Concretely, this looks like the following:
```
upstreams {
destination_name = "plugin-${var.plugin_id}"
# port unique but arbitrary - https://github.com/hashicorp/nomad/issues/7135
local_bind_port = 1001
}
```
if I wanted to pass this upstream's NOMAD_UPSTREAM_ADDR in as an env, i'm out of luck! The following obviously fails:
```
env {
GRAPH_QUERY_CLIENT_ADDRESS = "http://${NOMAD_UPSTREAM_ADDR_graph-query-sidecar-${var.plugin_id}}"
}
```
### Attempted Solutions
My attempt, which led to this ticket, is the following, where I try to do the string interpolation as one step and then rely on
the shell reading ${!VARIABLE_NAME} to do some variable indirection. (In this case, it means 'replace this {} with _the value_ of GRAPH_QUERY_UPSTREAM_ADDR - so, the full NOMAD_UPSTREAM_ADDR.)
```
GRAPH_QUERY_UPSTREAM_ADDR = "NOMAD_UPSTREAM_ADDR_graph-query-sidecar-${var.plugin_id}"
GRAPH_QUERY_CLIENT_ADDRESS = "http://${!GRAPH_QUERY_UPSTREAM_ADDR}"
```
resulting in
```
NonzeroExitStatus stderr=\"Error getting job struct: Error parsing job file from /tmp/.tmpRgkLfK:\\n.tmpRgkLfK
:294,49-74: Invalid operand; Unsuitable value for unary operand: a bool is required.\\n.tmpRgkLfK:294,39-46: Unsuitable value type; Unsuitable value: value must be known\\n\""
```
If I construct the name of the UPSTREAM_ADDR at runtime, and grab it from runtime env variables, it works just fine.
However, it'd be nice and clean if I could pass it in as part of the `env`!
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by tracing Nomad's interpolation handling for job `env` stanzas and compare it with the variable-indirection examples in the issue. No implementation file or test is named; done would require an agreed interpolation behavior that supports dynamically constructed environment-variable names and coverage for the upstream-address use case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- bash, go
- Domain
- devops
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100