Merge pull request #28088 from aluzzardi/service-ps-task-resolution

service ps: Truncate Task IDs
This commit is contained in:
Victor Vieux 2016-11-08 17:44:15 -08:00 committed by GitHub
commit 383a2f046b
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.