Skip to content

Commit

Permalink
Parse job > group > consul block in HCL1 (#11423)
Browse files Browse the repository at this point in the history
  • Loading branch information
angrycub committed Nov 3, 2021
1 parent 0e3cd60 commit 6efa55e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/11423.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
cli: Fix support for `group.consul` field in the HCLv1 parser
```
40 changes: 40 additions & 0 deletions jobspec/parse_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func parseGroups(result *api.Job, list *ast.ObjectList) error {
valid := []string{
"count",
"constraint",
"consul",
"affinity",
"restart",
"meta",
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
7 changes: 7 additions & 0 deletions jobspec/test-fixtures/consul-namespace.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
job "consul-namespace" {
group "group" {
consul {
namespace = "foo"
}
}
}

0 comments on commit 6efa55e

Please sign in to comment.