Parse `job > group > consul` block in HCL1 (#11423)

This commit is contained in:
Charlie Voiselle 2021-11-03 13:49:32 -04:00 committed by GitHub
parent 4fb5b8b6e7
commit 71643263a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 0 deletions

3
.changelog/11423.txt Normal file
View File

@ -0,0 +1,3 @@
```release-note:bug
cli: Fix support for `group.consul` field in the HCLv1 parser
```

View File

@ -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 {

View File

@ -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{

View File

@ -0,0 +1,7 @@
job "consul-namespace" {
group "group" {
consul {
namespace = "foo"
}
}
}