From 587a5d4a8db54a84b3ba901ebc82701afd830218 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Thu, 30 Jan 2020 10:49:07 -0600 Subject: [PATCH] nomad: make TaskGroup.UsesConnect helper a public helper --- .../taskrunner/envoybootstrap_hook.go | 9 ----- nomad/node_endpoint.go | 13 +------ nomad/node_endpoint_test.go | 38 ------------------- nomad/structs/structs.go | 11 ++++++ nomad/structs/structs_test.go | 38 +++++++++++++++++++ 5 files changed, 50 insertions(+), 59 deletions(-) diff --git a/client/allocrunner/taskrunner/envoybootstrap_hook.go b/client/allocrunner/taskrunner/envoybootstrap_hook.go index d945a2912..09efccb79 100644 --- a/client/allocrunner/taskrunner/envoybootstrap_hook.go +++ b/client/allocrunner/taskrunner/envoybootstrap_hook.go @@ -202,15 +202,6 @@ func (h *envoyBootstrapHook) writeConfig(filename, config string) error { return nil } -func (_ *envoyBootstrapHook) retry(ctx context.Context) bool { - select { - case <-ctx.Done(): - return false - case <-time.After(2 * time.Second): - return true - } -} - func (h *envoyBootstrapHook) execute(cmd *exec.Cmd) (string, error) { var ( stdout bytes.Buffer diff --git a/nomad/node_endpoint.go b/nomad/node_endpoint.go index 671e5778c..d4753027f 100644 --- a/nomad/node_endpoint.go +++ b/nomad/node_endpoint.go @@ -1702,7 +1702,7 @@ func (n *Node) DeriveSIToken(args *structs.DeriveSITokenRequest, reply *structs. setError(errors.Errorf("Allocation %q does not contain TaskGroup %q", args.AllocID, alloc.TaskGroup), false) return nil } - if !tgUsesConnect(tg) { + if !tg.UsesConnect() { setError(errors.Errorf("TaskGroup %q does not use Connect", tg.Name), false) return nil } @@ -1831,17 +1831,6 @@ func (n *Node) DeriveSIToken(args *structs.DeriveSITokenRequest, reply *structs. return nil } -func tgUsesConnect(tg *structs.TaskGroup) bool { - for _, service := range tg.Services { - if service.Connect != nil { - if service.Connect.Native || service.Connect.SidecarService != nil { - return true - } - } - } - return false -} - func tasksNotUsingConnect(tg *structs.TaskGroup, tasks []string) []string { var unneeded []string for _, task := range tasks { diff --git a/nomad/node_endpoint_test.go b/nomad/node_endpoint_test.go index 5ee1e3a44..a3fb9d5b6 100644 --- a/nomad/node_endpoint_test.go +++ b/nomad/node_endpoint_test.go @@ -3034,44 +3034,6 @@ func TestClientEndpoint_DeriveVaultToken_VaultError(t *testing.T) { } } -func TestClientEndpoint_tgUsesConnect(t *testing.T) { - t.Parallel() - - try := func(t *testing.T, tg *structs.TaskGroup, exp bool) { - result := tgUsesConnect(tg) - require.Equal(t, exp, result) - } - - t.Run("tg uses native", func(t *testing.T) { - try(t, &structs.TaskGroup{ - Services: []*structs.Service{ - {Connect: nil}, - {Connect: &structs.ConsulConnect{Native: true}}, - }, - }, true) - }) - - t.Run("tg uses sidecar", func(t *testing.T) { - try(t, &structs.TaskGroup{ - Services: []*structs.Service{{ - Connect: &structs.ConsulConnect{ - SidecarService: &structs.ConsulSidecarService{ - Port: "9090", - }, - }, - }}, - }, true) - }) - - t.Run("tg does not use connect", func(t *testing.T) { - try(t, &structs.TaskGroup{ - Services: []*structs.Service{ - {Connect: nil}, - }, - }, false) - }) -} - func TestClientEndpoint_taskUsesConnect(t *testing.T) { t.Parallel() diff --git a/nomad/structs/structs.go b/nomad/structs/structs.go index 97e5b324b..1869cda0a 100644 --- a/nomad/structs/structs.go +++ b/nomad/structs/structs.go @@ -5252,6 +5252,17 @@ func (tg *TaskGroup) LookupTask(name string) *Task { return nil } +func (tg *TaskGroup) UsesConnect() bool { + for _, service := range tg.Services { + if service.Connect != nil { + if service.Connect.Native || service.Connect.SidecarService != nil { + return true + } + } + } + return false +} + func (tg *TaskGroup) GoString() string { return fmt.Sprintf("*%#v", *tg) } diff --git a/nomad/structs/structs_test.go b/nomad/structs/structs_test.go index e94d258a9..f549adead 100644 --- a/nomad/structs/structs_test.go +++ b/nomad/structs/structs_test.go @@ -754,6 +754,44 @@ func TestTask_UsesConnect(t *testing.T) { // todo(shoenig): add native case } +func TestTaskGroup_UsesConnect(t *testing.T) { + t.Parallel() + + try := func(t *testing.T, tg *TaskGroup, exp bool) { + result := tg.UsesConnect() + require.Equal(t, exp, result) + } + + t.Run("tg uses native", func(t *testing.T) { + try(t, &TaskGroup{ + Services: []*Service{ + {Connect: nil}, + {Connect: &ConsulConnect{Native: true}}, + }, + }, true) + }) + + t.Run("tg uses sidecar", func(t *testing.T) { + try(t, &TaskGroup{ + Services: []*Service{{ + Connect: &ConsulConnect{ + SidecarService: &ConsulSidecarService{ + Port: "9090", + }, + }, + }}, + }, true) + }) + + t.Run("tg does not use connect", func(t *testing.T) { + try(t, &TaskGroup{ + Services: []*Service{ + {Connect: nil}, + }, + }, false) + }) +} + func TestTaskGroup_Validate(t *testing.T) { j := testJob() tg := &TaskGroup{