-
Notifications
You must be signed in to change notification settings - Fork 0
/
routesfetcher.go
102 lines (87 loc) · 2.48 KB
/
routesfetcher.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
package fetchers
import (
"sort"
"time"
"github.com/hashicorp/consul/api"
"github.com/pkg/errors"
"github.com/orange-cloudfoundry/promconsulfetcher/config"
"github.com/orange-cloudfoundry/promconsulfetcher/models"
)
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . RoutesFetch
type RoutesFetch interface {
Routes(search models.ServiceSearch) (models.Routes, error)
}
type RoutesFetcher struct {
consulClient *api.Client
}
func NewRoutesFetcher(consulConfig config.ConsulConfig) (*RoutesFetcher, error) {
client, err := createClient(consulConfig)
if err != nil {
return nil, err
}
return &RoutesFetcher{
consulClient: client,
}, nil
}
func (f *RoutesFetcher) Routes(search models.ServiceSearch) (models.Routes, error) {
entries, _, err := f.consulClient.Catalog().Service(search.Name, search.Tag, &api.QueryOptions{
Datacenter: search.Datacenter,
Near: search.Near,
})
if err != nil {
return nil, errors.Wrap(err, search.String())
}
var list models.Routes
for _, s := range entries {
list = append(list, &models.Route{
ID: s.ID,
Node: s.Node,
Address: s.Address,
Datacenter: s.Datacenter,
TaggedAddresses: s.TaggedAddresses,
NodeMeta: s.NodeMeta,
ServiceID: s.ServiceID,
ServiceName: s.ServiceName,
ServiceAddress: s.ServiceAddress,
ServiceTags: deepCopyAndSortTags(s.ServiceTags),
ServiceMeta: s.ServiceMeta,
ServicePort: s.ServicePort,
})
}
return list, nil
}
func createClient(cfg config.ConsulConfig) (*api.Client, error) {
config := api.Config{
Address: cfg.Address,
Scheme: cfg.Scheme,
Datacenter: cfg.DataCenter,
WaitTime: time.Duration(cfg.EndpointWaitTime),
Token: cfg.Token,
}
if cfg.HTTPAuth != nil {
config.HttpAuth = &api.HttpBasicAuth{
Username: cfg.HTTPAuth.Username,
Password: cfg.HTTPAuth.Password,
}
}
if cfg.TLS != nil {
config.TLSConfig = api.TLSConfig{
Address: cfg.Address,
CAFile: cfg.TLS.CA,
CertFile: cfg.TLS.Cert,
KeyFile: cfg.TLS.Key,
InsecureSkipVerify: cfg.TLS.InsecureSkipVerify,
}
}
return api.NewClient(&config)
}
// deepCopyAndSortTags deep copies the tags in the given string slice and then
// sorts and returns the copied result.
func deepCopyAndSortTags(tags []string) []string {
newTags := make([]string, 0, len(tags))
for _, tag := range tags {
newTags = append(newTags, tag)
}
sort.Strings(newTags)
return newTags
}