/
service.go
333 lines (270 loc) · 7.2 KB
/
service.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package config
import (
"fmt"
"strings"
)
// ServiceConfig defines the explicit configuration for CTS to monitor
// a service. This block may be specified multiple times to configure multiple
// services.
type ServiceConfig struct {
// Datacenter is the datacenter the service is deployed in.
Datacenter *string `mapstricture:"datacenter"`
// Description is the human readable text to describe the service.
Description *string `mapstructure:"description"`
// ID identifies the service for CTS. This is used to explicitly
// identify the service config for a task to use.
ID *string `mapstructure:"id"`
// Name is the Consul logical name of the service (required).
Name *string `mapstructure:"name"`
// Namespace is the namespace of the service (Consul Enterprise only). If not
// provided, the namespace will be inferred from the CTS ACL token, or
// default to the `default` namespace.
Namespace *string `mapstructure:"namespace"`
// Filter is used to filter nodes based on a Consul compatible filter expression.
Filter *string `mapstructure:"filter"`
// CTSUserDefinedMeta is metadata added to a service automated by CTS for
// network infrastructure automation.
CTSUserDefinedMeta map[string]string `mapstructure:"cts_user_defined_meta"`
}
// ServiceConfigs is a collection of ServiceConfig
type ServiceConfigs []*ServiceConfig
// ServicesMeta is a useful type to abstract from the nested map of string which
// represents the user defined meta for each service a task monitors
type ServicesMeta map[string]map[string]string
// Copy returns a deep copy of this configuration.
func (c *ServiceConfig) Copy() *ServiceConfig {
if c == nil {
return nil
}
var o ServiceConfig
o.Datacenter = StringCopy(c.Datacenter)
o.Description = StringCopy(c.Description)
o.ID = StringCopy(c.ID)
o.Name = StringCopy(c.Name)
o.Namespace = StringCopy(c.Namespace)
o.Filter = StringCopy(c.Filter)
if c.CTSUserDefinedMeta != nil {
o.CTSUserDefinedMeta = make(map[string]string)
for k, v := range c.CTSUserDefinedMeta {
o.CTSUserDefinedMeta[k] = v
}
}
return &o
}
// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *ServiceConfig) Merge(o *ServiceConfig) *ServiceConfig {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}
if o == nil {
return c.Copy()
}
r := c.Copy()
if o.Datacenter != nil {
r.Datacenter = StringCopy(o.Datacenter)
}
if o.Description != nil {
r.Description = StringCopy(o.Description)
}
if o.ID != nil {
r.ID = StringCopy(o.ID)
}
if o.Name != nil {
r.Name = StringCopy(o.Name)
}
if o.Namespace != nil {
r.Namespace = StringCopy(o.Namespace)
}
if o.Filter != nil {
r.Filter = StringCopy(o.Filter)
}
if o.CTSUserDefinedMeta != nil {
if r.CTSUserDefinedMeta == nil {
r.CTSUserDefinedMeta = make(map[string]string)
}
for k, v := range o.CTSUserDefinedMeta {
r.CTSUserDefinedMeta[k] = v
}
}
return r
}
// Finalize ensures there no nil pointers.
func (c *ServiceConfig) Finalize() {
if c == nil {
return
}
if c.Datacenter == nil {
c.Datacenter = String("")
}
if c.Description == nil {
c.Description = String("")
}
if c.Name == nil {
c.Name = String("")
}
if c.ID == nil {
c.ID = StringCopy(c.Name)
}
if c.Namespace == nil {
c.Namespace = String("")
}
if c.Filter == nil {
c.Filter = String("")
}
if c.CTSUserDefinedMeta == nil {
c.CTSUserDefinedMeta = make(map[string]string)
}
}
// Validate validates the values and nested values of the configuration struct
func (c *ServiceConfig) Validate() error {
if c == nil {
return fmt.Errorf("missing service configuration")
}
if c.Name == nil || len(*c.Name) == 0 {
return fmt.Errorf("logical name for the Consul service is required")
}
return nil
}
// GoString defines the printable version of this struct.
// Sensitive information is redacted.
func (c *ServiceConfig) GoString() string {
if c == nil {
return "(*ServiceConfig)(nil)"
}
return fmt.Sprintf("&ServiceConfig{"+
"Name:%s, "+
"Namespace:%s, "+
"Datacenter:%s, "+
"Filter:%s, "+
"Description:%s, "+
"CTSUserDefinedMeta:%s"+
"}",
StringVal(c.Name),
StringVal(c.Namespace),
StringVal(c.Datacenter),
StringVal(c.Filter),
StringVal(c.Description),
c.CTSUserDefinedMeta,
)
}
// DefaultServiceConfigs returns a configuration that is populated with the
// default values.
func DefaultServiceConfigs() *ServiceConfigs {
return &ServiceConfigs{}
}
// Len is a helper method to get the length of the underlying config list
func (c *ServiceConfigs) Len() int {
if c == nil {
return 0
}
return len(*c)
}
// Copy returns a deep copy of this configuration.
func (c *ServiceConfigs) Copy() *ServiceConfigs {
if c == nil {
return nil
}
o := make(ServiceConfigs, c.Len())
for i, t := range *c {
o[i] = t.Copy()
}
return &o
}
// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *ServiceConfigs) Merge(o *ServiceConfigs) *ServiceConfigs {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}
if o == nil {
return c.Copy()
}
r := c.Copy()
*r = append(*r, *o...)
return r
}
// Finalize ensures the configuration has no nil pointers and sets default
// values.
func (c *ServiceConfigs) Finalize() {
if c == nil {
*c = *DefaultServiceConfigs()
}
for _, t := range *c {
t.Finalize()
}
}
// Validate validates the values and nested values of the configuration struct
func (c *ServiceConfigs) Validate() error {
if c == nil {
return fmt.Errorf("missing services configuration")
}
ids := make(map[string]bool)
for _, s := range *c {
if err := s.Validate(); err != nil {
return err
}
id := *s.Name
if s.ID != nil {
id = *s.ID
}
if ids[id] {
return fmt.Errorf("unique service IDs are required: %s", id)
}
ids[id] = true
}
return nil
}
// CTSUserDefinedMeta generates a map of service name to user defined metadata
// from a list of service IDs or service names.
func (c *ServiceConfigs) CTSUserDefinedMeta(serviceList []string) ServicesMeta {
if c == nil {
return nil
}
services := make(map[string]bool)
for _, s := range serviceList {
services[s] = true
}
m := make(ServicesMeta)
for _, s := range *c {
if len(s.CTSUserDefinedMeta) == 0 {
continue
}
serviceName := *s.Name
if StringPresent(s.ID) {
if _, ok := services[*s.ID]; ok {
m[serviceName] = s.CTSUserDefinedMeta
continue
}
}
if _, ok := services[serviceName]; ok {
if !StringPresent(s.ID) {
m[serviceName] = s.CTSUserDefinedMeta
}
}
}
return m
}
// GoString defines the printable version of this struct.
func (c *ServiceConfigs) GoString() string {
if c == nil {
return "(*ServiceConfigs)(nil)"
}
s := make([]string, len(*c))
for i, t := range *c {
s[i] = t.GoString()
}
return "{" + strings.Join(s, ", ") + "}"
}