nomad: make TaskGroup.UsesConnect helper a public helper

This commit is contained in:
Seth Hoenig 2020-01-30 10:49:07 -06:00
parent ee89a754f1
commit 587a5d4a8d
5 changed files with 50 additions and 59 deletions

View File

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

View File

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

View File

@ -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()

View File

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

View File

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