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

connect: fix bug where sidecar_task.resources was ignored with hcl1 #11927

Merged
merged 2 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .changelog/11927.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
}
}
}
}
}
}