-
Notifications
You must be signed in to change notification settings - Fork 36
/
utils.go
538 lines (435 loc) · 14 KB
/
utils.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package v1
import (
"errors"
"fmt"
"os"
"regexp"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/utils/ptr"
internalerrors "github.com/aerospike/aerospike-kubernetes-operator/errors"
lib "github.com/aerospike/aerospike-management-lib"
)
const (
// DefaultRackID is the ID for the default rack created when no racks are specified.
DefaultRackID = 0
MaxRackID = 1000000
MinRackID = 1
ServiceTLSPortName = "tls-service"
ServicePortName = "service"
HeartbeatTLSPortName = "tls-heartbeat"
HeartbeatPortName = "heartbeat"
FabricTLSPortName = "tls-fabric"
FabricPortName = "fabric"
InfoPortName = "info"
)
const (
baseVersion = "4.9.0.3"
baseInitVersion = "1.0.0"
minInitVersionForDynamicConf = "2.2.0"
)
const (
// Namespace keys.
confKeyNamespace = "namespaces"
confKeyTLSName = "tls-name"
// Network section keys.
confKeyNetwork = "network"
confKeyNetworkService = "service"
confKeyNetworkHeartbeat = "heartbeat"
confKeyNetworkFabric = "fabric"
// XDR keys.
confKeyXdr = "xdr"
confKeyXdrDlogPath = "xdr-digestlog-path"
// Security keys.
confKeySecurity = "security"
confKeySecurityDefaultPasswordFile = "default-password-file"
// Service section keys.
confKeyService = "service"
confKeyWorkDirectory = "work-directory"
// Defaults.
DefaultWorkDirectory = "/opt/aerospike"
)
const (
AerospikeServerContainerName = "aerospike-server"
AerospikeInitContainerName = "aerospike-init"
AerospikeInitContainerRegistryEnvVar = "AEROSPIKE_KUBERNETES_INIT_REGISTRY"
AerospikeInitContainerDefaultRegistry = "docker.io"
AerospikeInitContainerDefaultRegistryNamespace = "aerospike"
AerospikeInitContainerDefaultRepoAndTag = "aerospike-kubernetes-init:2.2.0"
AerospikeAppLabel = "app"
AerospikeAppLabelValue = "aerospike-cluster"
AerospikeCustomResourceLabel = "aerospike.com/cr"
AerospikeRackIDLabel = "aerospike.com/rack-id"
AerospikeAPIVersionLabel = "aerospike.com/api-version"
AerospikeAPIVersion = "v1"
)
// ContainsString check whether list contains given string
func ContainsString(list []string, ele string) bool {
for _, listEle := range list {
if strings.EqualFold(ele, listEle) {
return true
}
}
return false
}
// GetConfiguredWorkDirectory returns the Aerospike work directory configured in aerospikeConfig.
func GetConfiguredWorkDirectory(aerospikeConfigSpec AerospikeConfigSpec) string {
// Get namespace config.
aerospikeConfig := aerospikeConfigSpec.Value
serviceTmp := aerospikeConfig[confKeyService]
if serviceTmp != nil {
serviceConf := serviceTmp.(map[string]interface{})
workDir, ok := serviceConf[confKeyWorkDirectory]
if ok {
return workDir.(string)
}
}
return ""
}
// GetWorkDirectory returns the Aerospike work directory to be used for aerospikeConfig.
func GetWorkDirectory(aerospikeConfigSpec AerospikeConfigSpec) string {
workDir := GetConfiguredWorkDirectory(aerospikeConfigSpec)
if workDir != "" {
return workDir
}
return DefaultWorkDirectory
}
func getInitContainerImage(registry string) string {
return fmt.Sprintf(
"%s/%s/%s", strings.TrimSuffix(registry, "/"),
strings.TrimSuffix(AerospikeInitContainerDefaultRegistryNamespace, "/"),
AerospikeInitContainerDefaultRepoAndTag,
)
}
func GetAerospikeInitContainerImage(aeroCluster *AerospikeCluster) string {
// Given in CR
registry := ""
if aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec != nil {
registry = aeroCluster.Spec.PodSpec.AerospikeInitContainerSpec.ImageRegistry
}
if registry != "" {
return getInitContainerImage(registry)
}
// Given in EnvVar
registry, found := os.LookupEnv(AerospikeInitContainerRegistryEnvVar)
if found {
return getInitContainerImage(registry)
}
// Use default
return getInitContainerImage(AerospikeInitContainerDefaultRegistry)
}
func ClusterNamespacedName(aeroCluster *AerospikeCluster) string {
return NamespacedName(aeroCluster.Namespace, aeroCluster.Name)
}
// NamespacedName return namespaced name
func NamespacedName(namespace, name string) string {
return fmt.Sprintf("%s/%s", namespace, name)
}
// ParseDockerImageTag parses input tag into registry, name and version.
func ParseDockerImageTag(tag string) (
registry string, name string, version string,
) {
if tag == "" {
return "", "", ""
}
r := regexp.MustCompile(`(?P<registry>[^/]+/)?(?P<image>[^:]+)(?P<version>:.+)?`)
matches := r.FindStringSubmatch(tag)
if matches == nil {
return "", "", ""
}
return matches[1], matches[2], strings.TrimPrefix(matches[3], ":")
}
// IsServiceTLSEnabled tells if service is tls enabled.
func IsServiceTLSEnabled(aerospikeConfigSpec *AerospikeConfigSpec) bool {
aerospikeConfig := aerospikeConfigSpec.Value
if networkConfInterface, ok := aerospikeConfig[confKeyNetwork]; ok {
if networkConf, ok := networkConfInterface.(map[string]interface{}); ok {
if serviceConfInterface, ok := networkConf[confKeyNetworkService]; ok {
if serviceConf, ok := serviceConfInterface.(map[string]interface{}); ok {
if _, ok := serviceConf[confKeyTLSName]; ok {
return true
}
}
}
}
}
return false
}
// IsSecurityEnabled tells if security is enabled in cluster
// TODO: can an invalid map come here
func IsSecurityEnabled(
version string, aerospikeConfig *AerospikeConfigSpec,
) (bool, error) {
retval, err := lib.CompareVersions(version, "5.7.0")
if err != nil {
return false, err
}
if retval == -1 {
return IsAttributeEnabled(
aerospikeConfig, "security", "enable-security",
)
}
if _, err := GetConfigContext(aerospikeConfig, "security"); err != nil {
if errors.Is(err, internalerrors.ErrNotFound) {
return false, nil
}
if errors.Is(err, internalerrors.ErrInvalidOrEmpty) {
return true, nil
}
return false, err
}
return true, nil
}
func IsAttributeEnabled(
aerospikeConfigSpec *AerospikeConfigSpec, context, key string,
) (bool, error) {
aerospikeConfig := aerospikeConfigSpec.Value
if len(aerospikeConfig) == 0 {
return false, fmt.Errorf("missing aerospike configuration in cluster state")
}
confMap, err := GetConfigContext(aerospikeConfigSpec, context)
if err != nil {
return false, err
}
enabled, err := GetBoolConfig(confMap, key)
if err != nil {
return false, fmt.Errorf(
"invalid aerospike.%s conf. %s", context, err.Error(),
)
}
return enabled, nil
}
func GetConfigContext(
aerospikeConfigSpec *AerospikeConfigSpec, context string,
) (map[string]interface{}, error) {
aerospikeConfig := aerospikeConfigSpec.Value
if len(aerospikeConfig) == 0 {
return nil, fmt.Errorf("missing aerospike configuration in cluster state")
}
if contextConfigMap, ok := aerospikeConfig[context]; ok {
if validConfigMap, ok := contextConfigMap.(map[string]interface{}); ok {
return validConfigMap, nil
}
return nil, fmt.Errorf(
"invalid aerospike.%s conf. %w", context,
internalerrors.ErrInvalidOrEmpty,
)
}
return nil, fmt.Errorf(
"context %s was %w", context, internalerrors.ErrNotFound,
)
}
func GetBoolConfig(configMap map[string]interface{}, key string) (bool, error) {
if enabled, ok := configMap[key]; ok {
if value, ok := enabled.(bool); ok {
return value, nil
}
return false, fmt.Errorf("%s: not valid", key)
}
return false, fmt.Errorf("%s: not present", key)
}
// IsAerospikeNamespacePresent indicates if the namespace is present in aerospikeConfig.
// Assumes the namespace section is validated.
func IsAerospikeNamespacePresent(
aerospikeConfigSpec AerospikeConfigSpec, namespaceName string,
) bool {
aerospikeConfig := aerospikeConfigSpec.Value
// Get namespace config.
if confs, ok := aerospikeConfig[confKeyNamespace].([]interface{}); ok {
for _, nsConf := range confs {
namespaceConf, ok := nsConf.(map[string]interface{})
if !ok {
// Should never happen
return false
}
if namespaceConf["name"] == namespaceName {
return true
}
}
}
return false
}
// IsXdrEnabled indicates if XDR is enabled in aerospikeConfig.
func IsXdrEnabled(aerospikeConfigSpec AerospikeConfigSpec) bool {
aerospikeConfig := aerospikeConfigSpec.Value
xdrConf := aerospikeConfig[confKeyXdr]
return xdrConf != nil
}
func ReadTLSAuthenticateClient(serviceConf map[string]interface{}) (
[]string, error,
) {
tlsAuthenticateClientConfig, ok := serviceConf["tls-authenticate-client"]
if !ok {
return nil, nil
}
switch value := tlsAuthenticateClientConfig.(type) {
case string:
return []string{value}, nil
case []interface{}:
tlsAuthenticateClientDomains := make([]string, len(value))
for i := 0; i < len(value); i++ {
item, ok := value[i].(string)
if !ok {
return nil, fmt.Errorf("invalid configuration element")
}
tlsAuthenticateClientDomains[i] = item
}
return tlsAuthenticateClientDomains, nil
}
return nil, fmt.Errorf("invalid configuration")
}
// GetDigestLogFile returns the xdr digest file path if configured.
func GetDigestLogFile(aerospikeConfigSpec AerospikeConfigSpec) (
*string, error,
) {
aerospikeConfig := aerospikeConfigSpec.Value
if xdrConfTmp, ok := aerospikeConfig[confKeyXdr]; ok {
xdrConf, ok := xdrConfTmp.(map[string]interface{})
if !ok {
return nil, fmt.Errorf(
"aerospikeConfig.xdr not a valid map %v",
aerospikeConfig[confKeyXdr],
)
}
dgLog, ok := xdrConf[confKeyXdrDlogPath]
if !ok {
return nil, fmt.Errorf(
"%s is missing in aerospikeConfig.xdr %v", confKeyXdrDlogPath,
xdrConf,
)
}
if _, ok := dgLog.(string); !ok {
return nil, fmt.Errorf(
"%s is not a valid string in aerospikeConfig.xdr %v",
confKeyXdrDlogPath, xdrConf,
)
}
// "/opt/aerospike/xdr/digestlog 100G"
if len(strings.Fields(dgLog.(string))) != 2 {
return nil, fmt.Errorf(
"%s is not in valid format (/opt/aerospike/xdr/digestlog 100G) in aerospikeConfig.xdr %v",
confKeyXdrDlogPath, xdrConf,
)
}
return &strings.Fields(dgLog.(string))[0], nil
}
return nil, fmt.Errorf("xdr not configured")
}
func GetServiceTLSNameAndPort(aeroConf *AerospikeConfigSpec) (tlsName string, port *int) {
return GetTLSNameAndPort(aeroConf, confKeyService)
}
func GetHeartbeatTLSNameAndPort(aeroConf *AerospikeConfigSpec) (tlsName string, port *int) {
return GetTLSNameAndPort(aeroConf, confKeyNetworkHeartbeat)
}
func GetFabricTLSNameAndPort(aeroConf *AerospikeConfigSpec) (tlsName string, port *int) {
return GetTLSNameAndPort(aeroConf, confKeyNetworkFabric)
}
func GetTLSNameAndPort(
aeroConf *AerospikeConfigSpec, connectionType string,
) (tlsName string, port *int) {
if networkConfTmp, ok := aeroConf.Value[confKeyNetwork]; ok {
networkConf := networkConfTmp.(map[string]interface{})
serviceConf := networkConf[connectionType].(map[string]interface{})
if tlsName, ok := serviceConf["tls-name"]; ok {
if tlsPort, portConfigured := serviceConf["tls-port"]; portConfigured {
intPort := int(tlsPort.(float64))
return tlsName.(string), &intPort
}
return tlsName.(string), nil
}
}
return "", nil
}
func GetServicePort(aeroConf *AerospikeConfigSpec) *int {
return GetPortFromConfig(aeroConf, confKeyNetworkService, "port")
}
func GetHeartbeatPort(aeroConf *AerospikeConfigSpec) *int {
return GetPortFromConfig(aeroConf, confKeyNetworkHeartbeat, "port")
}
func GetFabricPort(aeroConf *AerospikeConfigSpec) *int {
return GetPortFromConfig(aeroConf, confKeyNetworkFabric, "port")
}
func GetPortFromConfig(
aeroConf *AerospikeConfigSpec, connectionType string, paramName string,
) *int {
if networkConf, ok := aeroConf.Value[confKeyNetwork]; ok {
if connectionConfig, ok := networkConf.(map[string]interface{})[connectionType]; ok {
if port, ok := connectionConfig.(map[string]interface{})[paramName]; ok {
intPort := int(port.(float64))
return &intPort
}
}
}
return nil
}
// GetIntType typecasts the numeric value to the supported type
func GetIntType(value interface{}) (int, error) {
switch val := value.(type) {
case int64:
return int(val), nil
case int:
return val, nil
case float64:
return int(val), nil
default:
return 0, fmt.Errorf("value %v not valid int, int64 or float64", val)
}
}
// GetMigrateFillDelay returns the migrate-fill-delay from the Aerospike configuration
func GetMigrateFillDelay(asConfig *AerospikeConfigSpec) (int, error) {
serviceConfig := asConfig.Value["service"].(map[string]interface{})
fillDelayIFace, exists := serviceConfig["migrate-fill-delay"]
if !exists {
return 0, nil
}
fillDelay, err := GetIntType(fillDelayIFace)
if err != nil {
return 0, fmt.Errorf("migrate-fill-delay %v", err)
}
return fillDelay, nil
}
// IsClusterSCEnabled returns true if cluster has a sc namespace
func IsClusterSCEnabled(aeroCluster *AerospikeCluster) bool {
// Look inside only 1st rack. SC namespaces should be same across all the racks
rack := aeroCluster.Spec.RackConfig.Racks[0]
nsList := rack.AerospikeConfig.Value["namespaces"].([]interface{})
for _, nsConfInterface := range nsList {
isEnabled := IsNSSCEnabled(nsConfInterface.(map[string]interface{}))
if isEnabled {
return true
}
}
return false
}
func getContainerNames(containers []v1.Container) []string {
containerNames := make([]string, 0, len(containers))
for idx := range containers {
containerNames = append(containerNames, containers[idx].Name)
}
return containerNames
}
// GetBool returns the value of the given bool pointer. If the pointer is nil, it returns false.
func GetBool(boolPtr *bool) bool {
return ptr.Deref(boolPtr, false)
}
// GetDefaultPasswordFilePath returns the default-password-fille path if configured.
func GetDefaultPasswordFilePath(aerospikeConfigSpec *AerospikeConfigSpec) *string {
aerospikeConfig := aerospikeConfigSpec.Value
// Get security config.
securityConfTmp, ok := aerospikeConfig[confKeySecurity]
if !ok {
return nil
}
securityConf, ok := securityConfTmp.(map[string]interface{})
if !ok {
// Should never happen.
return nil
}
// Get password file.
passFileTmp, ok := securityConf[confKeySecurityDefaultPasswordFile]
if !ok {
return nil
}
passFile := passFileTmp.(string)
return &passFile
}