forked from hashicorp/consul-template
/
catalog_node.go
179 lines (150 loc) · 4.18 KB
/
catalog_node.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package dependency
import (
"encoding/gob"
"fmt"
"log"
"net/url"
"regexp"
"sort"
"github.com/pkg/errors"
)
var (
// Ensure implements
_ Dependency = (*CatalogNodeQuery)(nil)
// CatalogNodeQueryRe is the regular expression to use.
CatalogNodeQueryRe = regexp.MustCompile(`\A` + nodeNameRe + dcRe + `\z`)
)
func init() {
gob.Register([]*CatalogNode{})
gob.Register([]*CatalogNodeService{})
}
// CatalogNodeQuery represents a single node from the Consul catalog.
type CatalogNodeQuery struct {
stopCh chan struct{}
dc string
name string
}
// CatalogNode is a wrapper around the node and its services.
type CatalogNode struct {
Node *Node
Services []*CatalogNodeService
}
// CatalogNodeService is a service on a single node.
type CatalogNodeService struct {
ID string
Service string
Tags ServiceTags
Port int
Address string
EnableTagOverride bool
}
// NewCatalogNodeQuery parses the given string into a dependency. If the name is
// empty then the name of the local agent is used.
func NewCatalogNodeQuery(s string) (*CatalogNodeQuery, error) {
if s != "" && !CatalogNodeQueryRe.MatchString(s) {
return nil, fmt.Errorf("catalog.node: invalid format: %q", s)
}
m := regexpMatch(CatalogNodeQueryRe, s)
return &CatalogNodeQuery{
dc: m["dc"],
name: m["name"],
stopCh: make(chan struct{}, 1),
}, nil
}
// Fetch queries the Consul API defined by the given client and returns a
// of CatalogNode object.
func (d *CatalogNodeQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interface{}, *ResponseMetadata, error) {
select {
case <-d.stopCh:
return nil, nil, ErrStopped
default:
}
opts = opts.Merge(&QueryOptions{
Datacenter: d.dc,
})
// Grab the name
name := d.name
if name == "" {
log.Printf("[TRACE] %s: getting local agent name", d)
var err error
name, err = clients.Consul().Agent().NodeName()
if err != nil {
return nil, nil, errors.Wrapf(err, d.String())
}
}
log.Printf("[TRACE] %s: GET %s", d, &url.URL{
Path: "/v1/catalog/node/" + name,
RawQuery: opts.String(),
})
node, qm, err := clients.Consul().Catalog().Node(name, opts.ToConsulOpts())
if err != nil {
return nil, nil, errors.Wrap(err, d.String())
}
log.Printf("[TRACE] %s: returned response", d)
rm := &ResponseMetadata{
LastIndex: qm.LastIndex,
LastContact: qm.LastContact,
}
if node == nil {
log.Printf("[WARN] %s: no node exists with the name %q", d, name)
var node CatalogNode
return &node, rm, nil
}
services := make([]*CatalogNodeService, 0, len(node.Services))
for _, v := range node.Services {
services = append(services, &CatalogNodeService{
ID: v.ID,
Service: v.Service,
Tags: ServiceTags(deepCopyAndSortTags(v.Tags)),
Port: v.Port,
Address: v.Address,
EnableTagOverride: v.EnableTagOverride,
})
}
sort.Stable(ByService(services))
detail := &CatalogNode{
Node: &Node{
ID: node.Node.ID,
Node: node.Node.Node,
Address: node.Node.Address,
Datacenter: node.Node.Datacenter,
TaggedAddresses: node.Node.TaggedAddresses,
Meta: node.Node.Meta,
},
Services: services,
}
return detail, rm, nil
}
// CanShare returns a boolean if this dependency is shareable.
func (d *CatalogNodeQuery) CanShare() bool {
return false
}
// String returns the human-friendly version of this dependency.
func (d *CatalogNodeQuery) String() string {
name := d.name
if d.dc != "" {
name = name + "@" + d.dc
}
if name == "" {
return "catalog.node"
}
return fmt.Sprintf("catalog.node(%s)", name)
}
// Stop halts the dependency's fetch function.
func (d *CatalogNodeQuery) Stop() {
close(d.stopCh)
}
// Type returns the type of this dependency.
func (d *CatalogNodeQuery) Type() Type {
return TypeConsul
}
// ByService is a sorter of node services by their service name and then ID.
type ByService []*CatalogNodeService
func (s ByService) Len() int { return len(s) }
func (s ByService) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByService) Less(i, j int) bool {
if s[i].Service == s[j].Service {
return s[i].ID <= s[j].ID
}
return s[i].Service <= s[j].Service
}