hashicorp / nomad

Nomad is an easy-to-use, flexible, and performant workload orchestrator that can deploy a mix of microservice, batch, containerized, and non-containerized applications. Nomad is easy to operate and scale and has native Consul and Vault integrations.
https://www.nomadproject.io/
Other
14.92k stars 1.95k forks source link

HCL Variable Values mixes key-value and JSON format #23926

Open hashworks opened 2 months ago

hashworks commented 2 months ago

Nomad version

1.8.0

Issue

If you deploy a job with variables provided by both -var-file=foo.json and NOMAD_VAR_name environment variables the HCL Variable Values section in the WebUI will show both the correct key-value format (env variables) and JSON (-var-file). If one tries to edit the job definition and plan it, it results in the following error:

Failed to parse job: unable to parse var content: input.hcl:10,1-2: Argument or block definition required; An argument or block definition is required here.

The user then has to replace the JSON with the key-value format to fix this.

Reproduction steps

Deploy a job with both env and -var-file:

echo '{
    "domain": "example.com",
    "issuer": "https://auth.example.com/realms/example"
}' > foo.json

export NOMAD_VAR_version="3.3.1"
export NOMAD_VAR_commit_author="user <user@example.com>"

nomad run -var-file=foo.json some-job.hcl

Afterward open the job in the WebUI, edit the definition and try to plan your change.

Expected Result

The HCL Variable Values section should only show variables in the key-value format:

version="3.3.1"
commit_author="user <user@example.com>"
domain="example.com"
issuer="https://auth.example.com/realms/example"

Actual Result

It adds the JSON from -var-file=foo.json:

version="3.3.1"

commit_author="user <user@example.com>"
{
    "domain": "example.com",
    "issuer": "https://auth.example.com/realms/example"
}

image

hashworks commented 2 months ago

Note: I've checked the 1.8.{1,2,3} changelogs, they don't seem to include a fix for the bug I'm seeing.

jrasell commented 1 month ago

Hi @hashworks and thanks for raising this issue with the reproduction steps. I have used these to reproduce this locally using main at 4d6856a30619572ef43d0e6ffb803ddb459c4856. I'll move this issue to our backlog for roadmapping.

RSWilli commented 1 month ago

@jrasell I think the reason for this is purely in the UI, as far as I can see:

https://github.com/hashicorp/nomad/blob/ec81e7c57c1148600e53ed77bfd93a396a666a45/ui/app/components/job-page/parts/title.js#L89

_newDefinitionVariables is the string that gets used in the job editor component:

https://github.com/hashicorp/nomad/blob/main/ui/app/templates/components/job-editor/edit.hbs#L73-L94

But the Variables returns the content of the varfile. If I understand it correctly then the fix would be to do the following:

_newDefinitionVariables += jsonToHcl(JSON.parse(specification.Variables));

But that would require that the Variables field only returns JSON. If it returns the contents of the var file then there would have to be support for other formats.


An alternative would be to not concat the variables but instead use different input fields that get sent separately to the backend.