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: enable configuring sidecar_task.name #7684

Merged
merged 2 commits into from
Apr 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 1 addition & 26 deletions jobspec/parse_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,32 +263,7 @@ func parseSidecarService(o *ast.ObjectItem) (*api.ConsulSidecarService, error) {
}

func parseSidecarTask(item *ast.ObjectItem) (*api.SidecarTask, error) {
// We need this later
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
listVal = ot.List
} else {
return nil, fmt.Errorf("should be an object")
}

// Check for invalid keys
valid := []string{
"config",
"driver",
"env",
"kill_timeout",
"logs",
"meta",
"resources",
"shutdown_delay",
"user",
"kill_signal",
}
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
return nil, err
}

task, err := parseTask(item)
task, err := parseTask(item, sidecarTaskKeys)
if err != nil {
return nil, err
}
Expand Down
73 changes: 45 additions & 28 deletions jobspec/parse_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,48 @@ import (
"github.com/mitchellh/mapstructure"
)

var (
normalTaskKeys = []string{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that the shared keys don’t fall out of sync maybe we should have a coreTaskKeys and have these two something like
normalTaskKeys = append(coreTaskKeys, ...)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea; done!

"artifact",
"config",
"constraint",
"affinity",
"dispatch_payload",
"lifecycle",
"driver",
"env",
"kill_timeout",
"leader",
"logs",
"meta",
"resources",
"restart",
"service",
"shutdown_delay",
"template",
"user",
"vault",
"kill_signal",
"kind",
"volume_mount",
"csi_plugin",
}

sidecarTaskKeys = []string{
"name",
"driver",
"user",
"config",
"env",
"resources",
"meta",
"logs",
"kill_timeout",
"shutdown_delay",
"kill_signal",
}
)

func parseTasks(result *[]*api.Task, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
Expand All @@ -29,7 +71,7 @@ func parseTasks(result *[]*api.Task, list *ast.ObjectList) error {
}
seen[n] = struct{}{}

t, err := parseTask(item)
t, err := parseTask(item, normalTaskKeys)
if err != nil {
return multierror.Prefix(err, fmt.Sprintf("'%s',", n))
}
Expand All @@ -42,7 +84,7 @@ func parseTasks(result *[]*api.Task, list *ast.ObjectList) error {
return nil
}

func parseTask(item *ast.ObjectItem) (*api.Task, error) {
func parseTask(item *ast.ObjectItem, keys []string) (*api.Task, error) {
// We need this later
var listVal *ast.ObjectList
if ot, ok := item.Val.(*ast.ObjectType); ok {
Expand All @@ -52,32 +94,7 @@ func parseTask(item *ast.ObjectItem) (*api.Task, error) {
}

// Check for invalid keys
valid := []string{
"artifact",
"config",
"constraint",
"affinity",
"dispatch_payload",
"lifecycle",
"driver",
"env",
"kill_timeout",
"leader",
"logs",
"meta",
"resources",
"restart",
"service",
"shutdown_delay",
"template",
"user",
"vault",
"kill_signal",
"kind",
"volume_mount",
"csi_plugin",
}
if err := helper.CheckHCLKeys(listVal, valid); err != nil {
if err := helper.CheckHCLKeys(listVal, keys); err != nil {
return nil, err
}

Expand Down
22 changes: 22 additions & 0 deletions jobspec/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,28 @@ func TestParse(t *testing.T) {
},
false,
},
{
"service-connect-sidecar_task-name.hcl",
&api.Job{
ID: helper.StringToPtr("sidecar_task_name"),
Name: helper.StringToPtr("sidecar_task_name"),
Type: helper.StringToPtr("service"),
TaskGroups: []*api.TaskGroup{{
Name: helper.StringToPtr("group"),
Services: []*api.Service{{
Name: "example",
Connect: &api.ConsulConnect{
Native: false,
SidecarService: &api.ConsulSidecarService{},
SidecarTask: &api.SidecarTask{
Name: "my-sidecar",
},
},
}},
}},
},
false,
},
{
"reschedule-job.hcl",
&api.Job{
Expand Down
15 changes: 15 additions & 0 deletions jobspec/test-fixtures/service-connect-sidecar_task-name.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
job "sidecar_task_name" {
type = "service"

group "group" {
service {
name = "example"
connect {
sidecar_service {}
sidecar_task {
name = "my-sidecar"
}
}
}
}
}