From 392ec3e6973831fb982abdd1b436c4a7eabea531 Mon Sep 17 00:00:00 2001 From: Chris Baker <1675087+cgbaker@users.noreply.github.com> Date: Thu, 23 Apr 2020 12:37:45 +0000 Subject: [PATCH 1/2] return parsing error if scaling policy includes more than one policy block also, check that parsing a minimal scaling block doesn't throw any errors --- jobspec/parse_group.go | 19 +++++++------- jobspec/parse_test.go | 26 +++++++++++++++++++ .../tg-scaling-policy-minimal.hcl | 6 +++++ .../tg-scaling-policy-multi-policy.hcl | 20 ++++++++++++++ 4 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 jobspec/test-fixtures/tg-scaling-policy-minimal.hcl create mode 100644 jobspec/test-fixtures/tg-scaling-policy-multi-policy.hcl diff --git a/jobspec/parse_group.go b/jobspec/parse_group.go index 7285551b1..731018c6b 100644 --- a/jobspec/parse_group.go +++ b/jobspec/parse_group.go @@ -366,15 +366,16 @@ func parseScalingPolicy(out **api.ScalingPolicy, list *ast.ObjectList) error { // If we have policy, then parse that if o := listVal.Filter("policy"); len(o.Items) > 0 { - for _, o := range o.Elem().Items { - var m map[string]interface{} - if err := hcl.DecodeObject(&m, o.Val); err != nil { - return err - } - - if err := mapstructure.WeakDecode(m, &result.Policy); err != nil { - return err - } + if len(o.Elem().Items) > 1 { + return fmt.Errorf("only one 'policy' block allowed per 'scaling' block") + } + p := o.Elem().Items[0] + var m map[string]interface{} + if err := hcl.DecodeObject(&m, p.Val); err != nil { + return err + } + if err := mapstructure.WeakDecode(m, &result.Policy); err != nil { + return err } } diff --git a/jobspec/parse_test.go b/jobspec/parse_test.go index 8cd61d7ed..88fca2894 100644 --- a/jobspec/parse_test.go +++ b/jobspec/parse_test.go @@ -1310,6 +1310,32 @@ func TestParse(t *testing.T) { }, false, }, + + { + "tg-scaling-policy-minimal.hcl", + &api.Job{ + ID: helper.StringToPtr("elastic"), + Name: helper.StringToPtr("elastic"), + TaskGroups: []*api.TaskGroup{ + { + Name: helper.StringToPtr("group"), + Scaling: &api.ScalingPolicy{ + Min: nil, + Max: 0, + Policy: nil, + Enabled: nil, + }, + }, + }, + }, + false, + }, + + { + "tg-scaling-policy-multi-policy.hcl", + nil, + true, + }, } for _, tc := range cases { diff --git a/jobspec/test-fixtures/tg-scaling-policy-minimal.hcl b/jobspec/test-fixtures/tg-scaling-policy-minimal.hcl new file mode 100644 index 000000000..32d5ba719 --- /dev/null +++ b/jobspec/test-fixtures/tg-scaling-policy-minimal.hcl @@ -0,0 +1,6 @@ +job "elastic" { + group "group" { + scaling { + } + } +} diff --git a/jobspec/test-fixtures/tg-scaling-policy-multi-policy.hcl b/jobspec/test-fixtures/tg-scaling-policy-multi-policy.hcl new file mode 100644 index 000000000..1ad7b0edf --- /dev/null +++ b/jobspec/test-fixtures/tg-scaling-policy-multi-policy.hcl @@ -0,0 +1,20 @@ +job "elastic" { + group "group" { + scaling { + enabled = false + min = 5 + max = 100 + + policy { + foo = "right" + b = true + } + + policy { + foo = "wrong" + c = false + } + + } + } +} From 1e52f77b4afed5b743c672852d3106d932c7810a Mon Sep 17 00:00:00 2001 From: Chris Baker <1675087+cgbaker@users.noreply.github.com> Date: Thu, 23 Apr 2020 12:45:52 +0000 Subject: [PATCH 2/2] changelog entries for 7772 and 7788 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54c66535f..e92450dd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## 0.11.2 (Unreleased) +BUG FIXES: + + * api: autoscaling policies should not be returned for stopped jobs [[GH-7768](https://github.com/hashicorp/nomad/issues/7768)] + * jobspec: autoscaling policy block should return a parsing error multiple `policy` blocks are provided [[GH-7716](https://github.com/hashicorp/nomad/issues/7716)] + ## 0.11.1 (April 22, 2020) BUG FIXES: