diff --git a/.changelog/11423.txt b/.changelog/11423.txt new file mode 100644 index 000000000..2f94018c6 --- /dev/null +++ b/.changelog/11423.txt @@ -0,0 +1,3 @@ +```release-note:bug +cli: Fix support for `group.consul` field in the HCLv1 parser +``` diff --git a/jobspec/parse_group.go b/jobspec/parse_group.go index 8977afd51..060af851f 100644 --- a/jobspec/parse_group.go +++ b/jobspec/parse_group.go @@ -40,6 +40,7 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error { valid := []string{ "count", "constraint", + "consul", "affinity", "restart", "meta", @@ -67,6 +68,7 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error { } delete(m, "constraint") + delete(m, "consul") delete(m, "affinity") delete(m, "meta") delete(m, "task") @@ -104,6 +106,13 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error { } } + // Parse consul + if o := listVal.Filter("consul"); len(o.Items) > 0 { + if err := parseConsul(&g.Consul, o); err != nil { + return multierror.Prefix(err, fmt.Sprintf("'%s', consul ->", n)) + } + } + // Parse affinities if o := listVal.Filter("affinity"); len(o.Items) > 0 { if err := parseAffinities(&g.Affinities, o); err != nil { @@ -228,6 +237,37 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error { return nil } +func parseConsul(result **api.Consul, list *ast.ObjectList) error { + list = list.Elem() + if len(list.Items) > 1 { + return fmt.Errorf("only one 'consul' block allowed") + } + + // Get our consul object + obj := list.Items[0] + + // Check for invalid keys + valid := []string{ + "namespace", + } + if err := checkHCLKeys(obj.Val, valid); err != nil { + return err + } + + var m map[string]interface{} + if err := hcl.DecodeObject(&m, obj.Val); err != nil { + return err + } + + var consul api.Consul + if err := mapstructure.WeakDecode(m, &consul); err != nil { + return err + } + *result = &consul + + return nil +} + func parseEphemeralDisk(result **api.EphemeralDisk, list *ast.ObjectList) error { list = list.Elem() if len(list.Items) > 1 { diff --git a/jobspec/parse_test.go b/jobspec/parse_test.go index 0f563045c..63cac85df 100644 --- a/jobspec/parse_test.go +++ b/jobspec/parse_test.go @@ -1671,6 +1671,22 @@ func TestParse(t *testing.T) { nil, true, }, + { + "consul-namespace.hcl", + &api.Job{ + ID: stringToPtr("consul-namespace"), + Name: stringToPtr("consul-namespace"), + TaskGroups: []*api.TaskGroup{ + { + Name: stringToPtr("group"), + Consul: &api.Consul{ + Namespace: "foo", + }, + }, + }, + }, + false, + }, { "multiregion.hcl", &api.Job{ diff --git a/jobspec/test-fixtures/consul-namespace.hcl b/jobspec/test-fixtures/consul-namespace.hcl new file mode 100644 index 000000000..7e808527c --- /dev/null +++ b/jobspec/test-fixtures/consul-namespace.hcl @@ -0,0 +1,7 @@ +job "consul-namespace" { + group "group" { + consul { + namespace = "foo" + } + } +}