- 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>
| ... | ... |
@@ -7,6 +7,7 @@ import ( |
| 7 | 7 |
|
| 8 | 8 |
"github.com/docker/docker/api/types/swarm" |
| 9 | 9 |
"github.com/docker/docker/client" |
| 10 |
+ "github.com/docker/docker/pkg/stringid" |
|
| 10 | 11 |
) |
| 11 | 12 |
|
| 12 | 13 |
// IDResolver provides ID to Name resolution. |
| ... | ... |
@@ -26,7 +27,7 @@ func New(client client.APIClient, noResolve bool) *IDResolver {
|
| 26 | 26 |
} |
| 27 | 27 |
|
| 28 | 28 |
func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string, error) {
|
| 29 |
- switch t.(type) {
|
|
| 29 |
+ switch t := t.(type) {
|
|
| 30 | 30 |
case swarm.Node: |
| 31 | 31 |
node, _, err := r.client.NodeInspectWithRaw(ctx, id) |
| 32 | 32 |
if err != nil {
|
| ... | ... |
@@ -45,6 +46,25 @@ func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string,
|
| 45 | 45 |
return id, nil |
| 46 | 46 |
} |
| 47 | 47 |
return service.Spec.Annotations.Name, nil |
| 48 |
+ case swarm.Task: |
|
| 49 |
+ // If the caller passes the full task there's no need to do a lookup. |
|
| 50 |
+ if t.ID == "" {
|
|
| 51 |
+ var err error |
|
| 52 |
+ |
|
| 53 |
+ t, _, err = r.client.TaskInspectWithRaw(ctx, id) |
|
| 54 |
+ if err != nil {
|
|
| 55 |
+ return id, nil |
|
| 56 |
+ } |
|
| 57 |
+ } |
|
| 58 |
+ taskID := stringid.TruncateID(t.ID) |
|
| 59 |
+ if t.ServiceID == "" {
|
|
| 60 |
+ return taskID, nil |
|
| 61 |
+ } |
|
| 62 |
+ service, err := r.Resolve(ctx, swarm.Service{}, t.ServiceID)
|
|
| 63 |
+ if err != nil {
|
|
| 64 |
+ return "", err |
|
| 65 |
+ } |
|
| 66 |
+ return fmt.Sprintf("%s.%d.%s", service, t.Slot, taskID), nil
|
|
| 48 | 67 |
default: |
| 49 | 68 |
return "", fmt.Errorf("unsupported type")
|
| 50 | 69 |
} |
| ... | ... |
@@ -74,38 +74,24 @@ func PrintQuiet(dockerCli *command.DockerCli, tasks []swarm.Task) error {
|
| 74 | 74 |
} |
| 75 | 75 |
|
| 76 | 76 |
func print(out io.Writer, ctx context.Context, tasks []swarm.Task, resolver *idresolver.IDResolver, noTrunc bool) error {
|
| 77 |
- prevServiceName := "" |
|
| 77 |
+ prevService := "" |
|
| 78 | 78 |
prevSlot := 0 |
| 79 | 79 |
for _, task := range tasks {
|
| 80 |
- serviceName, err := resolver.Resolve(ctx, swarm.Service{}, task.ServiceID)
|
|
| 81 |
- if err != nil {
|
|
| 82 |
- return err |
|
| 83 |
- } |
|
| 80 |
+ name, err := resolver.Resolve(ctx, task, task.ID) |
|
| 81 |
+ |
|
| 84 | 82 |
nodeValue, err := resolver.Resolve(ctx, swarm.Node{}, task.NodeID)
|
| 85 | 83 |
if err != nil {
|
| 86 | 84 |
return err |
| 87 | 85 |
} |
| 88 | 86 |
|
| 89 |
- name := task.Annotations.Name |
|
| 90 |
- // TODO: This is the fallback <ServiceName>.<Slot>.<taskID> in case task name is not present in |
|
| 91 |
- // Annotations (upgraded from 1.12). |
|
| 92 |
- // We may be able to remove the following in the future. |
|
| 93 |
- if name == "" {
|
|
| 94 |
- if task.Slot != 0 {
|
|
| 95 |
- name = fmt.Sprintf("%v.%v.%v", serviceName, task.Slot, task.ID)
|
|
| 96 |
- } else {
|
|
| 97 |
- name = fmt.Sprintf("%v.%v.%v", serviceName, task.NodeID, task.ID)
|
|
| 98 |
- } |
|
| 99 |
- } |
|
| 100 |
- |
|
| 101 | 87 |
// Indent the name if necessary |
| 102 | 88 |
indentedName := name |
| 103 | 89 |
// Since the new format of the task name is <ServiceName>.<Slot>.<taskID>, we should only compare |
| 104 | 90 |
// <ServiceName> and <Slot> here. |
| 105 |
- if prevServiceName == serviceName && prevSlot == task.Slot {
|
|
| 91 |
+ if prevService == task.ServiceID && prevSlot == task.Slot {
|
|
| 106 | 92 |
indentedName = fmt.Sprintf(" \\_ %s", indentedName)
|
| 107 | 93 |
} |
| 108 |
- prevServiceName = serviceName |
|
| 94 |
+ prevService = task.ServiceID |
|
| 109 | 95 |
prevSlot = task.Slot |
| 110 | 96 |
|
| 111 | 97 |
// Trim and quote the error message. |