-
Notifications
You must be signed in to change notification settings - Fork 785
/
consul_partitions.go
114 lines (93 loc) · 3.04 KB
/
consul_partitions.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
package dependency
import (
"context"
"fmt"
"log"
"net/url"
"slices"
"strings"
"time"
"github.com/hashicorp/consul/api"
)
// Ensure implements
var (
_ Dependency = (*ListPartitionsQuery)(nil)
// ListPartitionsQuerySleepTime is the amount of time to sleep between
// queries, since the endpoint does not support blocking queries.
ListPartitionsQuerySleepTime = DefaultNonBlockingQuerySleepTime
)
// Partition is a partition in Consul.
type Partition struct {
Name string
Description string
}
// ListPartitionsQuery is the representation of a requested partitions
// dependency from inside a template.
type ListPartitionsQuery struct {
stopCh chan struct{}
}
func NewListPartitionsQuery() (*ListPartitionsQuery, error) {
return &ListPartitionsQuery{
stopCh: make(chan struct{}, 1),
}, nil
}
func (c *ListPartitionsQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interface{}, *ResponseMetadata, error) {
opts = opts.Merge(&QueryOptions{})
log.Printf("[TRACE] %s: GET %s", c, &url.URL{
Path: "/v1/partitions",
RawQuery: opts.String(),
})
// This is certainly not elegant, but the partitions endpoint does not support
// blocking queries, so we are going to "fake it until we make it". When we
// first query, the LastIndex will be "0", meaning we should immediately
// return data, but future calls will include a LastIndex. If we have a
// LastIndex in the query metadata, sleep for 15 seconds before asking Consul
// again.
//
// This is probably okay given the frequency in which partitions actually
// change, but is technically not edge-triggering.
if opts.WaitIndex != 0 {
log.Printf("[TRACE] %s: long polling for %s", c, ListPartitionsQuerySleepTime)
select {
case <-c.stopCh:
return nil, nil, ErrStopped
case <-time.After(ListPartitionsQuerySleepTime):
}
}
partitions, _, err := clients.Consul().Partitions().List(context.Background(), opts.ToConsulOpts())
if err != nil {
if strings.Contains(err.Error(), "Invalid URL path") {
return nil, nil, fmt.Errorf("%s: Partitions are an enterprise feature: %w", c.String(), err)
}
return nil, nil, fmt.Errorf("%s: %w", c.String(), err)
}
log.Printf("[TRACE] %s: returned %d results", c, len(partitions))
slices.SortFunc(partitions, func(i, j *api.Partition) int {
return strings.Compare(i.Name, j.Name)
})
resp := []*Partition{}
for _, partition := range partitions {
if partition != nil {
resp = append(resp, &Partition{
Name: partition.Name,
Description: partition.Description,
})
}
}
// Use respWithMetadata which always increments LastIndex and results
// in fetching new data for endpoints that don't support blocking queries
return respWithMetadata(resp)
}
// CanShare returns if this dependency is shareable when consul-template is running in de-duplication mode.
func (c *ListPartitionsQuery) CanShare() bool {
return true
}
func (c *ListPartitionsQuery) String() string {
return "list.partitions"
}
func (c *ListPartitionsQuery) Stop() {
close(c.stopCh)
}
func (c *ListPartitionsQuery) Type() Type {
return TypeConsul
}