-
Notifications
You must be signed in to change notification settings - Fork 780
/
consul_exported_services.go
127 lines (99 loc) · 3.03 KB
/
consul_exported_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
package dependency
import (
"fmt"
"log"
"net/url"
"slices"
"strings"
capi "github.com/hashicorp/consul/api"
)
const exportedServicesEndpointLabel = "list.exportedServices"
// Ensure implements
var _ Dependency = (*ListExportedServicesQuery)(nil)
// ListExportedServicesQuery is the representation of a requested exported services
// dependency from inside a template.
type ListExportedServicesQuery struct {
stopCh chan struct{}
partition string
}
type ExportedService struct {
// Name of the service
Service string
// Partition of the service
Partition string
// Namespace of the service
Namespace string
// Consumers is a list of downstream consumers of the service.
Consumers ResolvedConsumers
}
type ResolvedConsumers struct {
Peers []string
Partitions []string
}
func fromConsulExportedService(svc capi.ResolvedExportedService) ExportedService {
exportedService := ExportedService{
Service: svc.Service,
Consumers: ResolvedConsumers{
Partitions: []string{},
Peers: []string{},
},
}
if len(svc.Consumers.Partitions) > 0 {
exportedService.Consumers.Partitions = slices.Clone(svc.Consumers.Partitions)
}
if len(svc.Consumers.Peers) > 0 {
exportedService.Consumers.Peers = slices.Clone(svc.Consumers.Peers)
}
return exportedService
}
// NewListExportedServicesQuery parses a string of the format @dc.
func NewListExportedServicesQuery(s string) (*ListExportedServicesQuery, error) {
return &ListExportedServicesQuery{
stopCh: make(chan struct{}),
partition: s,
}, nil
}
func (c *ListExportedServicesQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interface{}, *ResponseMetadata, error) {
select {
case <-c.stopCh:
return nil, nil, ErrStopped
default:
}
opts = opts.Merge(&QueryOptions{
ConsulPartition: c.partition,
})
log.Printf("[TRACE] %s: GET %s", c, &url.URL{
Path: "/v1/exported-services",
RawQuery: opts.String(),
})
consulExportedServices, qm, err := clients.Consul().ExportedServices(opts.ToConsulOpts())
if err != nil {
return nil, nil, fmt.Errorf("%s: %w", c.String(), err)
}
exportedServices := make([]ExportedService, 0, len(consulExportedServices))
for _, exportedService := range consulExportedServices {
exportedServices = append(exportedServices, fromConsulExportedService(exportedService))
}
log.Printf("[TRACE] %s: returned %d results", c, len(exportedServices))
slices.SortStableFunc(exportedServices, func(i, j ExportedService) int {
return strings.Compare(i.Service, j.Service)
})
rm := &ResponseMetadata{
LastContact: qm.LastContact,
LastIndex: qm.LastIndex,
}
return exportedServices, rm, nil
}
// CanShare returns if this dependency is shareable when consul-template is running in de-duplication mode.
func (c *ListExportedServicesQuery) CanShare() bool {
return true
}
func (c *ListExportedServicesQuery) String() string {
return fmt.Sprintf("%s(%s)", exportedServicesEndpointLabel, c.partition)
}
func (c *ListExportedServicesQuery) Stop() {
close(c.stopCh)
}
func (c *ListExportedServicesQuery) Type() Type {
return TypeConsul
}