2016-06-13 22:56:23 -04:00
|
|
|
package idresolver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2016-09-06 14:46:37 -04:00
|
|
|
"github.com/docker/docker/client"
|
2016-11-04 22:23:07 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-06-13 22:56:23 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// IDResolver provides ID to Name resolution.
|
|
|
|
type IDResolver struct {
|
|
|
|
client client.APIClient
|
|
|
|
noResolve bool
|
|
|
|
cache map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new IDResolver.
|
|
|
|
func New(client client.APIClient, noResolve bool) *IDResolver {
|
|
|
|
return &IDResolver{
|
|
|
|
client: client,
|
|
|
|
noResolve: noResolve,
|
|
|
|
cache: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IDResolver) get(ctx context.Context, t interface{}, id string) (string, error) {
|
2016-11-04 22:23:07 -04:00
|
|
|
switch t := t.(type) {
|
2016-06-13 22:56:23 -04:00
|
|
|
case swarm.Node:
|
2016-06-30 17:04:02 -04:00
|
|
|
node, _, err := r.client.NodeInspectWithRaw(ctx, id)
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
if node.Spec.Annotations.Name != "" {
|
|
|
|
return node.Spec.Annotations.Name, nil
|
|
|
|
}
|
|
|
|
if node.Description.Hostname != "" {
|
|
|
|
return node.Description.Hostname, nil
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
case swarm.Service:
|
2016-06-16 18:47:22 -04:00
|
|
|
service, _, err := r.client.ServiceInspectWithRaw(ctx, id)
|
2016-06-13 22:56:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
return service.Spec.Annotations.Name, nil
|
2016-11-04 22:23:07 -04:00
|
|
|
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
|
2016-06-13 22:56:23 -04:00
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unsupported type")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve will attempt to resolve an ID to a Name by querying the manager.
|
|
|
|
// Results are stored into a cache.
|
|
|
|
// If the `-n` flag is used in the command-line, resolution is disabled.
|
|
|
|
func (r *IDResolver) Resolve(ctx context.Context, t interface{}, id string) (string, error) {
|
|
|
|
if r.noResolve {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
if name, ok := r.cache[id]; ok {
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
name, err := r.get(ctx, t, id)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
r.cache[id] = name
|
|
|
|
return name, nil
|
|
|
|
}
|