Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse job > group > consul block in HCL1 #11423

Merged
merged 3 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
}
}