Skip to content

Commit

Permalink
cli: set -hcl2-strict to false if -hcl1 is defined (#14426)
Browse files Browse the repository at this point in the history
These options are mutually exclusive but, since `-hcl2-strict` defaults
to `true` users had to explicitily set it to `false` when using `-hcl1`.

Also return `255` when job plan fails validation as this is the expected 
code in this situation.
  • Loading branch information
lgfa29 committed Sep 1, 2022
1 parent 3a4345e commit 99ebd0a
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 15 deletions.
7 changes: 7 additions & 0 deletions .changelog/14426.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:improvement
cli: ignore `-hcl2-strict` when -hcl1 is set.
```

```release-note:bug
cli: return exit code `255` when `nomad job plan` fails job validation.
```
10 changes: 7 additions & 3 deletions command/job_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ Plan Options:
used as the job.
-hcl1
Parses the job file as HCLv1.
Parses the job file as HCLv1. Takes precedence over "-hcl2-strict".
-hcl2-strict
Whether an error should be produced from the HCL2 parser where a variable
has been supplied which is not defined within the root variables. Defaults
to true.
to true, but ignored if "-hcl1" is also defined.
-policy-override
Sets the flag to force override any soft mandatory Sentinel policies.
Expand Down Expand Up @@ -183,9 +183,13 @@ func (c *JobPlanCommand) Run(args []string) int {
return 255
}

if c.JobGetter.HCL1 {
c.JobGetter.Strict = false
}

if err := c.JobGetter.Validate(); err != nil {
c.Ui.Error(fmt.Sprintf("Invalid job options: %s", err))
return 1
return 255
}

path := args[0]
Expand Down
19 changes: 19 additions & 0 deletions command/job_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ job "job1" {
}
}

func TestPlanCommand_hcl1_hcl2_strict(t *testing.T) {
ci.Parallel(t)

_, _, addr := testServer(t, false, nil)

t.Run("-hcl1 implies -hcl2-strict is false", func(t *testing.T) {
ui := cli.NewMockUi()
cmd := &JobPlanCommand{Meta: Meta{Ui: ui}}
got := cmd.Run([]string{
"-hcl1", "-hcl2-strict",
"-address", addr,
"assets/example-short.nomad",
})
// Exit code 1 here means that an alloc will be created, which is
// expected.
require.Equal(t, 1, got)
})
}

func TestPlanCommand_From_STDIN(t *testing.T) {
ci.Parallel(t)
stdinR, stdinW, err := os.Pipe()
Expand Down
8 changes: 6 additions & 2 deletions command/job_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ Run Options:
used as the job.
-hcl1
Parses the job file as HCLv1.
Parses the job file as HCLv1. Takes precedence over "-hcl2-strict".
-hcl2-strict
Whether an error should be produced from the HCL2 parser where a variable
has been supplied which is not defined within the root variables. Defaults
to true.
to true, but ignored if "-hcl1" is also defined.
-output
Output the JSON that would be submitted to the HTTP API without submitting
Expand Down Expand Up @@ -223,6 +223,10 @@ func (c *JobRunCommand) Run(args []string) int {
return 1
}

if c.JobGetter.HCL1 {
c.JobGetter.Strict = false
}

if err := c.JobGetter.Validate(); err != nil {
c.Ui.Error(fmt.Sprintf("Invalid job options: %s", err))
return 1
Expand Down
18 changes: 18 additions & 0 deletions command/job_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ job "job1" {
}
}

func TestRunCommand_hcl1_hcl2_strict(t *testing.T) {
ci.Parallel(t)

_, _, addr := testServer(t, false, nil)

t.Run("-hcl1 implies -hcl2-strict is false", func(t *testing.T) {
ui := cli.NewMockUi()
cmd := &JobRunCommand{Meta: Meta{Ui: ui}}
got := cmd.Run([]string{
"-hcl1", "-hcl2-strict",
"-address", addr,
"-detach",
"assets/example-short.nomad",
})
require.Equal(t, 0, got, ui.ErrorWriter.String())
})
}

func TestRunCommand_Fails(t *testing.T) {
ci.Parallel(t)

Expand Down
8 changes: 6 additions & 2 deletions command/job_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ Validate Options:
used as the job.
-hcl1
Parses the job file as HCLv1.
Parses the job file as HCLv1. Takes precedence over "-hcl2-strict".
-hcl2-strict
Whether an error should be produced from the HCL2 parser where a variable
has been supplied which is not defined within the root variables. Defaults
to true.
to true, but ignored if "-hcl1" is also defined.
-vault-token
Used to validate if the user submitting the job has permission to run the job
Expand Down Expand Up @@ -130,6 +130,10 @@ func (c *JobValidateCommand) Run(args []string) int {
return 1
}

if c.JobGetter.HCL1 {
c.JobGetter.Strict = false
}

if err := c.JobGetter.Validate(); err != nil {
c.Ui.Error(fmt.Sprintf("Invalid job options: %s", err))
return 1
Expand Down
16 changes: 16 additions & 0 deletions command/job_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ func TestValidateCommand_Files(t *testing.T) {
require.Equal(t, 1, code)
})
}
func TestValidateCommand_hcl1_hcl2_strict(t *testing.T) {
ci.Parallel(t)

_, _, addr := testServer(t, false, nil)

t.Run("-hcl1 implies -hcl2-strict is false", func(t *testing.T) {
ui := cli.NewMockUi()
cmd := &JobValidateCommand{Meta: Meta{Ui: ui}}
got := cmd.Run([]string{
"-hcl1", "-hcl2-strict",
"-address", addr,
"assets/example-short.nomad",
})
require.Equal(t, 0, got, ui.ErrorWriter.String())
})
}

func TestValidateCommand_Fails(t *testing.T) {
ci.Parallel(t)
Expand Down
5 changes: 3 additions & 2 deletions website/content/docs/commands/job/plan.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ capability for the job's namespace.
such as from "nomad job inspect" or "nomad run -output", the value of the
field is used as the job.

- `-hcl1`: If set, HCL1 parser is used for parsing the job spec.
- `-hcl1`: If set, HCL1 parser is used for parsing the job spec. Takes
precedence over `-hcl2-strict`.

- `-hcl2-strict`: Whether an error should be produced from the HCL2 parser where
a variable has been supplied which is not defined within the root variables.
Defaults to true.
Defaults to true, but ignored if `-hcl1` is defined.

- `-vault-token`: Used to validate if the user submitting the job has
permission to run the job according to its Vault policies. A Vault token must
Expand Down
5 changes: 3 additions & 2 deletions website/content/docs/commands/job/run.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ that volume.
such as from "nomad job inspect" or "nomad run -output", the value of the
field is used as the job. See [JSON Jobs] for details.

- `-hcl1`: If set, HCL1 parser is used for parsing the job spec.
- `-hcl1`: If set, HCL1 parser is used for parsing the job spec. Takes
precedence over `-hcl2-strict`.

- `-hcl2-strict`: Whether an error should be produced from the HCL2 parser where
a variable has been supplied which is not defined within the root variables.
Defaults to true.
Defaults to true, but ignored if `-hcl1` is defined.

- `-output`: Output the JSON that would be submitted to the HTTP API without
submitting the job.
Expand Down
9 changes: 5 additions & 4 deletions website/content/docs/commands/job/validate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ capability for the job's namespace.
such as from "nomad job inspect" or "nomad run -output", the value of the
field is used as the job.

- `-hcl1`: If set, HCL1 parser is used for parsing the job spec.
- `-hcl1`: If set, HCL1 parser is used for parsing the job spec. Takes
precedence over `-hcl2-strict`.

- `-hcl2-strict`: Whether an error should be produced from the HCL2 parser where
a variable has been supplied which is not defined within the root variables.
Defaults to true.
a variable has been supplied which is not defined within the root variables.
Defaults to true, but ignored if `-hcl1` is defined.

- `-vault-token`: Used to validate if the user submitting the job has
permission to run the job according to its Vault policies. A Vault token must
Expand Down Expand Up @@ -98,4 +99,4 @@ Job validation successful
[`go-getter`]: https://github.com/hashicorp/go-getter
[job specification]: /docs/job-specification
[`vault` stanza `allow_unauthenticated`]: /docs/configuration/vault#allow_unauthenticated
[`vault_token`]: /docs/job-specification/job#vault_token
[`vault_token`]: /docs/job-specification/job#vault_token

0 comments on commit 99ebd0a

Please sign in to comment.