service ps: Truncate Task IDs

- Refactored to move resolution code into the idresolver
- Made `ps` output more bearable by shortening service IDs in task names

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2016-11-04 19:23:07 -07:00
parent a3d806f0bb
commit 5834d378e0
2 changed files with 26 additions and 20 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/docker/docker/api/types/swarm"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/stringid"
)
// IDResolver provides ID to Name resolution.
@ -26,7 +27,7 @@ func New(client client.APIClient, noResolve bool) *IDResolver {
}
func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string, error) {
switch t.(type) {
switch t := t.(type) {
case swarm.Node:
node, _, err := r.client.NodeInspectWithRaw(ctx, id)
if err != nil {
@ -45,6 +46,25 @@ func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string,
return id, nil
}
return service.Spec.Annotations.Name, nil
case swarm.Task:
// If the caller passes the full task there's no need to do a lookup.
if t.ID == "" {
var err error
t, _, err = r.client.TaskInspectWithRaw(ctx, id)
if err != nil {
return id, nil
}
}
taskID := stringid.TruncateID(t.ID)
if t.ServiceID == "" {
return taskID, nil
}
service, err := r.Resolve(ctx, swarm.Service{}, t.ServiceID)
if err != nil {
return "", err
}
return fmt.Sprintf("%s.%d.%s", service, t.Slot, taskID), nil
default:
return "", fmt.Errorf("unsupported type")
}

View File

@ -74,38 +74,24 @@ func PrintQuiet(dockerCli *command.DockerCli, tasks []swarm.Task) error {
}
func print(out io.Writer, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
prevServiceName := ""
prevService := ""
prevSlot := 0
for _, task := range tasks {
serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID)
if err != nil {
return err
}
name, err := resolver.Resolve(ctx, task, task.ID)
nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID)
if err != nil {
return err
}
name := task.Annotations.Name
// TODO: This is the fallback <ServiceName>.<Slot>.<taskID> in case task name is not present in
// 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
indentedName := 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 {
if prevService == task.ServiceID && prevSlot == task.Slot {
indentedName = fmt.Sprintf(" \\_ %s", indentedName)
}
prevServiceName = serviceName
prevService = task.ServiceID
prevSlot = task.Slot
// Trim and quote the error message.