Merge pull request #7684 from hashicorp/b-connect-sidecar-name

connect: enable configuring sidecar_task.name
This commit is contained in:
Seth Hoenig 2020-04-10 10:04:25 -06:00 committed by GitHub
commit 43804d8ca9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 54 deletions

View File

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

View File

@ -12,6 +12,41 @@ import (
"github.com/mitchellh/mapstructure"
)
var (
commonTaskKeys = []string{
"driver",
"user",
"config",
"env",
"resources",
"meta",
"logs",
"kill_timeout",
"shutdown_delay",
"kill_signal",
}
normalTaskKeys = append(commonTaskKeys,
"artifact",
"constraint",
"affinity",
"dispatch_payload",
"lifecycle",
"leader",
"restart",
"service",
"template",
"vault",
"kind",
"volume_mount",
"csi_plugin",
)
sidecarTaskKeys = append(commonTaskKeys,
"name",
)
)
func parseTasks(result *[]*api.Task, list *ast.ObjectList) error {
list = list.Children()
if len(list.Items) == 0 {
@ -29,7 +64,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))
}
@ -42,7 +77,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 {
@ -52,32 +87,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
}

View File

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

View File

@ -0,0 +1,15 @@
job "sidecar_task_name" {
type = "service"
group "group" {
service {
name = "example"
connect {
sidecar_service {}
sidecar_task {
name = "my-sidecar"
}
}
}
}
}