forked from hashicorp/consul-template
/
catalog_services.go
187 lines (153 loc) · 3.9 KB
/
catalog_services.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
180
181
182
183
184
185
186
187
package dependency
import (
"encoding/gob"
"errors"
"fmt"
"log"
"regexp"
"sort"
"sync"
"github.com/hashicorp/consul/api"
)
func init() {
gob.Register([]*CatalogService{})
}
// CatalogService is a catalog entry in Consul.
type CatalogService struct {
Name string
Tags ServiceTags
}
// CatalogServices is the representation of a requested catalog service
// dependency from inside a template.
type CatalogServices struct {
sync.Mutex
rawKey string
Name string
Tags []string
DataCenter string
stopped bool
stopCh chan struct{}
}
// Fetch queries the Consul API defined by the given client and returns a slice
// of CatalogService objects.
func (d *CatalogServices) Fetch(clients *ClientSet, opts *QueryOptions) (interface{}, *ResponseMetadata, error) {
d.Lock()
if d.stopped {
defer d.Unlock()
return nil, nil, ErrStopped
}
d.Unlock()
if opts == nil {
opts = &QueryOptions{}
}
consulOpts := opts.consulQueryOptions()
if d.DataCenter != "" {
consulOpts.Datacenter = d.DataCenter
}
consul, err := clients.Consul()
if err != nil {
return nil, nil, fmt.Errorf("catalog services: error getting client: %s", err)
}
var entries map[string][]string
var qm *api.QueryMeta
dataCh := make(chan struct{})
go func() {
log.Printf("[DEBUG] (%s) querying Consul with %+v", d.Display(), consulOpts)
entries, qm, err = consul.Catalog().Services(consulOpts)
close(dataCh)
}()
select {
case <-d.stopCh:
return nil, nil, ErrStopped
case <-dataCh:
}
if err != nil {
return nil, nil, fmt.Errorf("catalog services: error fetching: %s", err)
}
log.Printf("[DEBUG] (%s) Consul returned %d catalog services", d.Display(), len(entries))
var catalogServices []*CatalogService
for name, tags := range entries {
tags = deepCopyAndSortTags(tags)
catalogServices = append(catalogServices, &CatalogService{
Name: name,
Tags: ServiceTags(tags),
})
}
sort.Stable(CatalogServicesList(catalogServices))
rm := &ResponseMetadata{
LastIndex: qm.LastIndex,
LastContact: qm.LastContact,
}
return catalogServices, rm, nil
}
// CanShare returns a boolean if this dependency is shareable.
func (d *CatalogServices) CanShare() bool {
return true
}
// HashCode returns a unique identifier.
func (d *CatalogServices) HashCode() string {
return fmt.Sprintf("CatalogServices|%s", d.rawKey)
}
// Display prints the human-friendly output.
func (d *CatalogServices) Display() string {
if d.rawKey == "" {
return fmt.Sprintf(`"services"`)
}
return fmt.Sprintf(`"services(%s)"`, d.rawKey)
}
// Stop halts the dependency's fetch function.
func (d *CatalogServices) Stop() {
d.Lock()
defer d.Unlock()
if !d.stopped {
close(d.stopCh)
d.stopped = true
}
}
// ParseCatalogServices parses a string of the format @dc.
func ParseCatalogServices(s ...string) (*CatalogServices, error) {
switch len(s) {
case 0:
cs := &CatalogServices{
rawKey: "",
stopCh: make(chan struct{}),
}
return cs, nil
case 1:
dc := s[0]
re := regexp.MustCompile(`\A` +
`(@(?P<datacenter>[[:word:]\.\-]+))?` +
`\z`)
names := re.SubexpNames()
match := re.FindAllStringSubmatch(dc, -1)
if len(match) == 0 {
return nil, errors.New("invalid catalog service dependency format")
}
r := match[0]
m := map[string]string{}
for i, n := range r {
if names[i] != "" {
m[names[i]] = n
}
}
nd := &CatalogServices{
rawKey: dc,
DataCenter: m["datacenter"],
stopCh: make(chan struct{}),
}
return nd, nil
default:
return nil, fmt.Errorf("expected 0 or 1 arguments, got %d", len(s))
}
}
/// --- Sorting
// CatalogServicesList is a sortable slice of CatalogService structs.
type CatalogServicesList []*CatalogService
func (s CatalogServicesList) Len() int { return len(s) }
func (s CatalogServicesList) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s CatalogServicesList) Less(i, j int) bool {
if s[i].Name <= s[j].Name {
return true
}
return false
}