forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
74 lines (68 loc) · 1.67 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package common
import (
"fmt"
"github.com/docker/swarmkit/api"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)
// Resolver provides ID to Name resolution.
type Resolver struct {
cmd *cobra.Command
c api.ControlClient
ctx context.Context
cache map[string]string
}
// NewResolver creates a new Resolver.
func NewResolver(cmd *cobra.Command, c api.ControlClient) *Resolver {
return &Resolver{
cmd: cmd,
c: c,
ctx: Context(cmd),
cache: make(map[string]string),
}
}
func (r *Resolver) get(t interface{}, id string) string {
switch t.(type) {
case api.Node:
res, err := r.c.GetNode(r.ctx, &api.GetNodeRequest{NodeID: id})
if err != nil {
return id
}
if res.Node.Spec.Annotations.Name != "" {
return res.Node.Spec.Annotations.Name
}
if res.Node.Description == nil {
return id
}
return res.Node.Description.Hostname
case api.Service:
res, err := r.c.GetService(r.ctx, &api.GetServiceRequest{ServiceID: id})
if err != nil {
return id
}
return res.Service.Spec.Annotations.Name
case api.Task:
res, err := r.c.GetTask(r.ctx, &api.GetTaskRequest{TaskID: id})
if err != nil {
return id
}
svc := r.get(api.Service{}, res.Task.ServiceID)
return fmt.Sprintf("%s.%d", svc, res.Task.Slot)
default:
return id
}
}
// 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 *Resolver) Resolve(t interface{}, id string) string {
if r.cmd.Flags().Changed("no-resolve") {
return id
}
if name, ok := r.cache[id]; ok {
return name
}
name := r.get(t, id)
r.cache[id] = name
return name
}