Fix partial/full filter issue in `service tasks --filter`

This fix tries to address the issue related to 24108 and 24790, and
also the case from 24620#issuecomment-233715656

The reason for the failure case in the above mentioned issues is that
currently Task names are actually indexed by Service Name
(`e.ServiceAnnotations.Name`)

To fix it, a pull request in swarmkit (swarmkit/pull/1193) has been
opened separately.

This fix adds the integration tests for the above mentioned issues.
Swarmkit revendoring is needed to completely fix the issues.

This fix fixes 24108.
This fix fixes 24790.
This fix is related to 24620.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang 2016-07-23 09:58:58 -07:00
parent 53c51901d7
commit 2d844ea5c8
1 changed files with 20 additions and 10 deletions

View File

@ -16,7 +16,7 @@ import (
) )
const ( const (
psTaskItemFmt = "%s\t%s\t%s\t%s\t%s\t%s %s ago\t%s\n" psTaskItemFmt = "%s\t%s\t%s\t%s\t%s %s ago\t%s\n"
maxErrLength = 30 maxErrLength = 30
) )
@ -48,11 +48,12 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
// Ignore flushing errors // Ignore flushing errors
defer writer.Flush() defer writer.Flush()
fmt.Fprintln(writer, strings.Join([]string{"ID", "NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR"}, "\t")) fmt.Fprintln(writer, strings.Join([]string{"NAME", "IMAGE", "NODE", "DESIRED STATE", "CURRENT STATE", "ERROR"}, "\t"))
prevName := "" prevServiceName := ""
prevSlot := 0
for _, task := range tasks { for _, task := range tasks {
serviceValue, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID) serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID)
if err != nil { if err != nil {
return err return err
} }
@ -61,17 +62,27 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
return err return err
} }
name := serviceValue name := task.Annotations.Name
if task.Slot > 0 { // TODO: This is the fallback <ServiceName>.<Slot>.<taskID> in case task name is not present in
name = fmt.Sprintf("%s.%d", name, task.Slot) // Annotations (upgraded from 1.12).
// We may be able to remove the following in the future.
if name == "" {
if task.Slot != 0 {
name = fmt.Sprintf("%v.%v.%v", serviceName, task.Slot, task.ID)
} else {
name = fmt.Sprintf("%v.%v.%v", serviceName, task.NodeID, task.ID)
}
} }
// Indent the name if necessary // Indent the name if necessary
indentedName := name indentedName := name
if prevName == name { // Since the new format of the task name is <ServiceName>.<Slot>.<taskID>, we should only compare
// <ServiceName> and <Slot> here.
if prevServiceName == serviceName && prevSlot == task.Slot {
indentedName = fmt.Sprintf(" \\_ %s", indentedName) indentedName = fmt.Sprintf(" \\_ %s", indentedName)
} }
prevName = name prevServiceName = serviceName
prevSlot = task.Slot
// Trim and quote the error message. // Trim and quote the error message.
taskErr := task.Status.Err taskErr := task.Status.Err
@ -85,7 +96,6 @@ func Print(dockerCli *command.DockerCli, ctx context.Context, tasks []swarm.Task
fmt.Fprintf( fmt.Fprintf(
writer, writer,
psTaskItemFmt, psTaskItemFmt,
task.ID,
indentedName, indentedName,
task.Spec.ContainerSpec.Image, task.Spec.ContainerSpec.Image,
nodeValue, nodeValue,