diff --git a/.changelog/11927.txt b/.changelog/11927.txt new file mode 100644 index 000000000..86a212935 --- /dev/null +++ b/.changelog/11927.txt @@ -0,0 +1,4 @@ +``` +release-note:bug +Fixed a bug where connect sidecar resources were ignored when using HCL1 +``` diff --git a/jobspec/parse_service.go b/jobspec/parse_service.go index c4c1d33d8..fe679805b 100644 --- a/jobspec/parse_service.go +++ b/jobspec/parse_service.go @@ -104,12 +104,10 @@ func parseService(o *ast.ObjectItem) (*api.Service, error) { if len(co.Items) > 1 { return nil, fmt.Errorf("connect '%s': cannot have more than 1 connect stanza", service.Name) } - c, err := parseConnect(co.Items[0]) if err != nil { return nil, multierror.Prefix(err, fmt.Sprintf("'%s',", service.Name)) } - service.Connect = c } @@ -191,33 +189,29 @@ func parseConnect(co *ast.ObjectItem) (*api.ConsulConnect, error) { // Parse the sidecar_service o = connectList.Filter("sidecar_service") - if len(o.Items) == 0 { - return &connect, nil - } if len(o.Items) > 1 { return nil, fmt.Errorf("only one 'sidecar_service' block allowed per task") } - - r, err := parseSidecarService(o.Items[0]) - if err != nil { - return nil, fmt.Errorf("sidecar_service, %v", err) + if len(o.Items) == 1 { + r, err := parseSidecarService(o.Items[0]) + if err != nil { + return nil, fmt.Errorf("sidecar_service, %v", err) + } + connect.SidecarService = r } - connect.SidecarService = r // Parse the sidecar_task o = connectList.Filter("sidecar_task") - if len(o.Items) == 0 { - return &connect, nil - } if len(o.Items) > 1 { return nil, fmt.Errorf("only one 'sidecar_task' block allowed per task") } - - t, err := parseSidecarTask(o.Items[0]) - if err != nil { - return nil, fmt.Errorf("sidecar_task, %v", err) + if len(o.Items) == 1 { + t, err := parseSidecarTask(o.Items[0]) + if err != nil { + return nil, fmt.Errorf("sidecar_task, %v", err) + } + connect.SidecarTask = t } - connect.SidecarTask = t return &connect, nil } diff --git a/jobspec/parse_test.go b/jobspec/parse_test.go index 63cac85df..aa219c117 100644 --- a/jobspec/parse_test.go +++ b/jobspec/parse_test.go @@ -1285,6 +1285,30 @@ func TestParse(t *testing.T) { }, false, }, + { + "tg-service-connect-resources.hcl", + &api.Job{ + ID: stringToPtr("sidecar_task_resources"), + Name: stringToPtr("sidecar_task_resources"), + Type: stringToPtr("service"), + TaskGroups: []*api.TaskGroup{{ + Name: stringToPtr("group"), + Services: []*api.Service{{ + Name: "example", + Connect: &api.ConsulConnect{ + SidecarTask: &api.SidecarTask{ + Resources: &api.Resources{ + CPU: intToPtr(111), + MemoryMB: intToPtr(222), + MemoryMaxMB: intToPtr(333), + }, + }, + }, + }}, + }}, + }, + false, + }, { "tg-service-connect-proxy.hcl", &api.Job{ diff --git a/jobspec/test-fixtures/tg-service-connect-resources.hcl b/jobspec/test-fixtures/tg-service-connect-resources.hcl new file mode 100644 index 000000000..166c17fb9 --- /dev/null +++ b/jobspec/test-fixtures/tg-service-connect-resources.hcl @@ -0,0 +1,20 @@ +job "sidecar_task_resources" { + type = "service" + + group "group" { + service { + name = "example" + + connect { + # should still work without sidecar_service being set (i.e. connect gateway) + sidecar_task { + resources { + cpu = 111 + memory = 222 + memory_max = 333 + } + } + } + } + } +}