Skip to content

Commit

Permalink
connect: fix bug where sidecar_task.resources was ignored with hcl1
Browse files Browse the repository at this point in the history
The HCL1 parser did not respect connect.sidecar_task.resources if the
connect.sidecar_service block was not set (an optimiztion that no longer
makes sense with connect gateways).

Fixes #10899
  • Loading branch information
shoenig committed Jan 25, 2022
1 parent 358a468 commit 15442b3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .changelog/10899.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
```
release-note:bug
Fixed a bug where connect sidecar resources were ignored when using HCL1
```
30 changes: 12 additions & 18 deletions jobspec/parse_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
24 changes: 24 additions & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
20 changes: 20 additions & 0 deletions jobspec/test-fixtures/tg-service-connect-resources.hcl
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
}
}

0 comments on commit 15442b3

Please sign in to comment.