/
foundationdbcluster_types.go
1540 lines (1310 loc) · 50.9 KB
/
foundationdbcluster_types.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2019 FoundationDB project authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1beta1
import (
"encoding/json"
"fmt"
"math/rand"
"reflect"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// FoundationDBClusterSpec defines the desired state of a cluster.
type FoundationDBClusterSpec struct {
// Version defines the version of FoundationDB the cluster should run.
Version string `json:"version"`
// SidecarVersions defines the build version of the sidecar to run. This
// maps an FDB version to the corresponding sidecar build version.
SidecarVersions map[string]int `json:"sidecarVersions,omitempty"`
// RunningVersion defines the version of FoundationDB that the cluster is
// currently running.
RunningVersion string `json:"runningVersion,omitempty"`
// DatabaseConfiguration defines the database configuration.
DatabaseConfiguration `json:"databaseConfiguration,omitempty"`
// Configured defines whether we have configured the database yet.
Configured bool `json:"configured,omitempty"`
// ProcessCounts defines the number of processes to configure for each
// process class. You can generally omit this, to allow the operator to
// infer the process counts based on the database configuration.
ProcessCounts `json:"processCounts,omitempty"`
// ConnectionString defines the contents of the cluster file.
ConnectionString string `json:"connectionString,omitempty"`
// FaultDomain defines the rules for what fault domain to replicate across.
FaultDomain FoundationDBClusterFaultDomain `json:"faultDomain,omitempty"`
// CustomParameters defines additional parameters to pass to the fdbserver
// processes.
CustomParameters []string `json:"customParameters,omitempty"`
// PendingRemovals defines the processes that are pending removal.
// This maps the name of a pod to its IP address. If a value is left blank,
// the controller will provide the pod's current IP.
PendingRemovals map[string]string `json:"pendingRemovals,omitempty"`
// PodTemplate allows customizing the FoundationDB pods.
PodTemplate *corev1.PodTemplateSpec `json:"podTemplate,omitempty"`
// VolumeClaim allows customizing the persistent volume claim for the
// FoundationDB pods.
VolumeClaim *corev1.PersistentVolumeClaim
// ConfigMap allows customizing the config map the operator creates.
ConfigMap *corev1.ConfigMap
// MainContainer defines customization for the foundationdb container.
MainContainer ContainerOverrides `json:"mainContainer,omitempty"`
// SidecarContainer defines customization for the
// foundationdb-kubernetes-sidecar container.
SidecarContainer ContainerOverrides `json:"sidecarContainer,omitempty"`
// TrustedCAs defines a list of root CAs the cluster should trust, in PEM
// format.
TrustedCAs []string `json:"trustedCAs,omitempty"`
// SidecarVariables defines Ccustom variables that the sidecar should make
// available for substitution in the monitor conf file.
SidecarVariables []string `json:"sidecarVariables,omitempty"`
// LogGroup defines the log group to use for the trace logs for the cluster.
LogGroup string `json:"logGroup,omitempty"`
// DataCenter defines the data center where these processes are running.
DataCenter string `json:"dataCenter,omitempty"`
// AutomationOptions defines customization for enabling or disabling certain
// operations in the operator.
AutomationOptions FoundationDBClusterAutomationOptions `json:"automationOptions,omitempty"`
// InstanceIDPrefix defines a prefix to append to the instance IDs in the
// locality fields.
InstanceIDPrefix string `json:"instanceIDPrefix,omitempty"`
// SidecarVersion defines the build version of the sidecar to use.
//
// Deprecated: Use SidecarVersions instead.
SidecarVersion int `json:"sidecarVersion,omitempty"`
// PodLabels defines custom labels to apply to the FDB pods.
//
// Deprecated: Use the PodTemplate field instead.
PodLabels map[string]string `json:"podLabels,omitempty"`
// Resources defines the resource requirements for the foundationdb
// containers.
//
// Deprecated: Use the PodTemplate field instead.
Resources *corev1.ResourceRequirements `json:"resources,omitempty"`
// InitContainers defines custom init containers for the FDB pods.
//
// Deprecated: Use the PodTemplate field instead.
InitContainers []corev1.Container `json:"initContainers,omitempty"`
// Containers defines custom containers for the FDB pods.
//
// Deprecated: Use the PodTemplate field instead.
Containers []corev1.Container `json:"containers,omitempty"`
// Volumes defines custom volumes for the FDB pods.
//
// Deprecated: Use the PodTemplate field instead.
Volumes []corev1.Volume `json:"volumes,omitempty"`
// PodSecurityContext defines the security context to apply to the FDB pods.
//
// Deprecated: Use the PodTemplate field instead.
PodSecurityContext *corev1.PodSecurityContext `json:"podSecurityContext,omitempty"`
// AutomountServiceAccountToken defines whether we should automount the
// service account tokens in the FDB pods.
//
// Deprecated: Use the PodTemplate field instead.
AutomountServiceAccountToken *bool `json:"automountServiceAccountToken,omitempty"`
// NextInstanceID defines the ID to use when creating the next instance.
//
// Deprecated: This is no longer used.
NextInstanceID int `json:"nextInstanceID,omitempty"`
// StorageClass defines the storage class for the volumes in the cluster.
//
// Deprecated: Use the VolumeClaim field instead.
StorageClass *string `json:"storageClass,omitempty"`
// VolumeSize defines the size of the volume to use for stateful processes.
//
// Deprecated: Use the VolumeClaim field instead.
VolumeSize string `json:"volumeSize,omitempty"`
}
// FoundationDBClusterStatus defines the observed state of FoundationDBCluster
type FoundationDBClusterStatus struct {
// ProcessCounts defines the number of processes that are currently running
// in the cluster.
ProcessCounts `json:"processCounts,omitempty"`
// IncorrectProcesses provides the processes that do not have the correct
// configuration.
//
// This will map the instance ID to the timestamp when we observed the
// incorrect configuration.
IncorrectProcesses map[string]int64 `json:"incorrectProcesses,omitempty"`
// MissingProcesses provides the processes that are not reporting to the
// cluster.
// This will map the names of the pod to the timestamp when we observed
// that the process was missing.
MissingProcesses map[string]int64 `json:"missingProcesses,omitempty"`
// DatabaseConfiguration provides the running configuration of the database.
DatabaseConfiguration DatabaseConfiguration `json:"databaseConfiguration,omitempty"`
// Generations provides information about the latest generation to be
// reconciled, or to reach other stages at which reconciliation can halt.
Generations GenerationStatus `json:"generations,omitempty"`
// Health provides information about the health of the database.
Health ClusterHealth `json:"health,omitempty"`
// RequiredAddresses define that addresses that we need to enable for the
// processes in the cluster.
RequiredAddresses RequiredAddressSet `json:"requiredAddresses,omitempty"`
}
// GenerationStatus stores information on which generations have reached
// different stages in reconciliation.
type GenerationStatus struct {
// Reconciled provides the last generation that was fully reconciled.
Reconciled int64 `json:"reconciled,omitempty"`
// NeedsConfigurationChange provides the last generation that is pending
// a change to configuration.
NeedsConfigurationChange int64 `json:"needsConfigurationChange,omitempty"`
// NeedsBounce provides the last generation that is pending a bounce of
// fdbserver.
NeedsBounce int64 `json:"needsBounce,omitempty"`
// NeedsPodDeletion provides the last generation that is pending pods being
// deleted and recreated.
NeedsPodDeletion int64 `json:"needsPodDeletion,omitempty"`
}
// ClusterHealth represents different views into health in the cluster status.
type ClusterHealth struct {
// Available reports whether the database is accepting reads and writes.
Available bool `json:"available,omitempty"`
// Healthy reports whether the database is in a fully healthy state.
Healthy bool `json:"healthy,omitempty"`
// FullReplication reports whether all data are fully replicated according
// to the current replication policy.
FullReplication bool `json:"fullReplication,omitempty"`
// DataMovementPriority reports the priority of the highest-priority data
// movement in the cluster.
DataMovementPriority int `json:"dataMovementPriority,omitempty"`
}
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// FoundationDBCluster is the Schema for the foundationdbclusters API
// +k8s:openapi-gen=true
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Generation",type="integer",JSONPath=".metadata.generation",description="Latest generation of the spec",priority=0
// +kubebuilder:printcolumn:name="Reconciled",type="integer",JSONPath=".status.generations.reconciled",description="Last reconciled generation of the spec",priority=0
// +kubebuilder:printcolumn:name="Healthy",type="boolean",JSONPath=".status.health.healthy",description="Database health",priority=0
type FoundationDBCluster struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
// Spec defines the desired state of the cluster.
Spec FoundationDBClusterSpec `json:"spec,omitempty"`
// Status defines the current state of the cluster.
Status FoundationDBClusterStatus `json:"status,omitempty"`
}
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// FoundationDBClusterList contains a list of FoundationDBCluster
type FoundationDBClusterList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
// Items defines the clusters in the list.
Items []FoundationDBCluster `json:"items"`
}
// RoleCounts represents the roles whose counts can be customized.
type RoleCounts struct {
Storage int `json:"storage,omitempty"`
Logs int `json:"logs,omitempty"`
Proxies int `json:"proxies,omitempty"`
Resolvers int `json:"resolvers,omitempty"`
LogRouters int `json:"log_routers,omitempty"`
RemoteLogs int `json:"remote_logs,omitempty"`
}
// Map returns a map from process classes to the desired count for that role
func (counts RoleCounts) Map() map[string]int {
countMap := make(map[string]int, len(roleIndices))
countValue := reflect.ValueOf(counts)
for role, index := range roleIndices {
if role != "storage" {
value := int(countValue.Field(index).Int())
countMap[role] = value
}
}
return countMap
}
// ProcessCounts represents the number of processes we have for each valid
// process class.
//
// If one of the counts in the spec is set to 0, we will infer the process count
// for that class from the role counts. If one of the counts in the spec is set
// to -1, we will not create any processes for that class. See
// GetProcessCountsWithDefaults for more information on the rules for inferring
// process counts.
type ProcessCounts struct {
// Storage defines the number of storage class processes.
Storage int `json:"storage,omitempty"`
// Transaction defines the number of transaction class processes.
Transaction int `json:"transaction,omitempty"`
// Stateless defines the number of stateless class processes.
Stateless int `json:"stateless,omitempty"`
// Resolution defines the number of resolution class processes.
Resolution int `json:"resolution,omitempty"`
Unset int `json:"unset,omitempty"`
Log int `json:"log,omitempty"`
Master int `json:"master,omitempty"`
ClusterController int `json:"cluster_controller,omitempty"`
Proxy int `json:"proxy,omitempty"`
Resolver int `json:"resolver,omitempty"`
Router int `json:"router,omitempty"`
}
// Map returns a map from process classes to the number of processes with that
// class.
func (counts ProcessCounts) Map() map[string]int {
countMap := make(map[string]int, len(processClassIndices))
countValue := reflect.ValueOf(counts)
for processClass, index := range processClassIndices {
value := int(countValue.Field(index).Int())
if value > 0 {
countMap[processClass] = value
}
}
return countMap
}
// IncreaseCount adds to one of the process counts based on the name.
func (counts *ProcessCounts) IncreaseCount(name string, amount int) {
index, present := processClassIndices[name]
if present {
countValue := reflect.ValueOf(counts)
value := countValue.Elem().Field(index)
value.SetInt(value.Int() + int64(amount))
}
}
// fieldNames provides the names of fields on a structure.
func fieldNames(value interface{}) []string {
countType := reflect.TypeOf(value)
names := make([]string, 0, countType.NumField())
for index := 0; index < countType.NumField(); index++ {
tag := strings.Split(countType.Field(index).Tag.Get("json"), ",")
names = append(names, tag[0])
}
return names
}
// fieldIndices provides a map from the names of fields in a structure to the
// index of each field in the list of fields.
func fieldIndices(value interface{}) map[string]int {
countType := reflect.TypeOf(value)
indices := make(map[string]int, countType.NumField())
for index := 0; index < countType.NumField(); index++ {
tag := strings.Split(countType.Field(index).Tag.Get("json"), ",")
indices[tag[0]] = index
}
return indices
}
// ProcessClasses provides a consistent ordered list of the supported process
// classes.
var ProcessClasses = fieldNames(ProcessCounts{})
// processClassIndices provides the indices of each process class in the list
// of process classes.
var processClassIndices = fieldIndices(ProcessCounts{})
// roleNames provides a consistent ordered list of the supported roles.
var roleNames = fieldNames(RoleCounts{})
// roleIndices provides the indices of each role in the list of roles.
var roleIndices = fieldIndices(RoleCounts{})
// FoundationDBClusterAutomationOptions provides flags for enabling or disabling
// operations that can be performed on a cluster.
type FoundationDBClusterAutomationOptions struct {
// ConfigureDatabase defines whether the operator is allowed to reconfigure
// the database.
ConfigureDatabase *bool `json:"configureDatabase,omitempty"`
// KillProcesses defines whether the operator is allowed to bounce fdbserver
// processes.
KillProcesses *bool `json:"killProcesses,omitempty"`
// DeletePods defines whether the operator is allowed to delete pods in
// order to recreate them.
DeletePods *bool `json:"deletePods,omitempty"`
}
// GetRoleCountsWithDefaults gets the role counts from the cluster spec and
// fills in default values for any role counts that are 0.
//
// The default Storage value will be 2F + 1, where F is the cluster's fault
// tolerance.
//
// The default Logs value will be 3.
//
// The default Proxies value will be 3.
//
// The default Resolvers value will be 1.
//
// The default RemoteLogs value will be equal to the Logs value when the
// UsableRegions is greater than 1. It will be equal to -1 when the
// UsableRegions is less than or equal to 1.
//
// The default LogRouters value will be equal to 3 times the Logs value when
// the UsableRegions is greater than 1. It will be equal to -1 when the
// UsableRegions is less than or equal to 1.
func (cluster *FoundationDBCluster) GetRoleCountsWithDefaults() RoleCounts {
counts := cluster.Spec.RoleCounts.DeepCopy()
if counts.Storage == 0 {
counts.Storage = 2*cluster.DesiredFaultTolerance() + 1
}
if counts.Logs == 0 {
counts.Logs = 3
}
if counts.Proxies == 0 {
counts.Proxies = 3
}
if counts.Resolvers == 0 {
counts.Resolvers = 1
}
if counts.RemoteLogs == 0 {
if cluster.Spec.UsableRegions > 1 {
counts.RemoteLogs = counts.Logs
} else {
counts.RemoteLogs = -1
}
}
if counts.LogRouters == 0 {
if cluster.Spec.UsableRegions > 1 {
counts.LogRouters = 3 * counts.Logs
} else {
counts.LogRouters = -1
}
}
return *counts
}
// calculateProcessCount determines the process count from a given role count.
//
// alternatives provides a list of other process counts that can fulfill this
// role instead. If any of those process counts is positive, then this will
// return 0.
func (cluster *FoundationDBCluster) calculateProcessCountFromRole(count int, alternatives ...int) int {
for _, value := range alternatives {
if value > 0 {
return 0
}
}
if count < 0 {
return 0
}
return count
}
// calculateProcessCount calculates the process count for a process class based
// on the counts for the roles it can fulfill.
//
// If addFaultTolerance is true, this will add the cluster's desired fault
// tolerance to the result.
//
// If the cluster is using multi-KC replication, this will divide the total
// count across the number of KCs in the data center.
func (cluster *FoundationDBCluster) calculateProcessCount(addFaultTolerance bool, counts ...int) int {
var count = 0
if cluster.Spec.FaultDomain.ZoneIndex < 0 {
return -1
}
for _, possibleCount := range counts {
if possibleCount > count {
count = possibleCount
}
}
if count > 0 {
if addFaultTolerance {
count += cluster.DesiredFaultTolerance()
}
if cluster.Spec.FaultDomain.Key == "foundationdb.org/kubernetes-cluster" {
zoneCount := cluster.Spec.FaultDomain.ZoneCount
if zoneCount < 1 {
zoneCount = cluster.MinimumFaultDomains() + cluster.DesiredFaultTolerance()
}
overflow := count % zoneCount
count = count / zoneCount
if cluster.Spec.FaultDomain.ZoneIndex < overflow {
count++
}
}
return count
}
return -1
}
// GetProcessCountsWithDefaults gets the process counts from the cluster spec
// and fills in default values for any counts that are 0.
func (cluster *FoundationDBCluster) GetProcessCountsWithDefaults() ProcessCounts {
roleCounts := cluster.GetRoleCountsWithDefaults()
processCounts := cluster.Spec.ProcessCounts.DeepCopy()
isSatellite := false
isMain := false
satelliteLogs := 0
for _, region := range cluster.Spec.DatabaseConfiguration.Regions {
for _, dataCenter := range region.DataCenters {
if dataCenter.ID == cluster.Spec.DataCenter {
if dataCenter.Satellite == 0 {
isMain = true
} else {
isSatellite = true
if region.SatelliteLogs > satelliteLogs {
satelliteLogs = region.SatelliteLogs
}
}
}
}
}
if isSatellite && !isMain {
if processCounts.Log == 0 {
processCounts.Log = 1 + satelliteLogs
return *processCounts
}
}
if processCounts.Storage == 0 {
processCounts.Storage = cluster.calculateProcessCount(false,
roleCounts.Storage)
}
if processCounts.Log == 0 {
processCounts.Log = cluster.calculateProcessCount(true,
cluster.calculateProcessCountFromRole(roleCounts.Logs+satelliteLogs, processCounts.Log),
cluster.calculateProcessCountFromRole(roleCounts.RemoteLogs, processCounts.Log),
)
}
if processCounts.Stateless == 0 {
processCounts.Stateless = cluster.calculateProcessCount(true,
cluster.calculateProcessCountFromRole(1, processCounts.Master)+
cluster.calculateProcessCountFromRole(1, processCounts.ClusterController)+
cluster.calculateProcessCountFromRole(roleCounts.Proxies, processCounts.Proxy)+
cluster.calculateProcessCountFromRole(roleCounts.Resolvers, processCounts.Resolution, processCounts.Resolver),
cluster.calculateProcessCountFromRole(roleCounts.LogRouters),
)
}
return *processCounts
}
// DesiredFaultTolerance returns the number of replicas we should be able to
// lose when the cluster is at full replication health.
func (cluster *FoundationDBCluster) DesiredFaultTolerance() int {
switch cluster.Spec.RedundancyMode {
case "single":
return 0
case "double":
return 1
case "triple":
return 2
default:
return 0
}
}
// MinimumFaultDomains returns the number of fault domains the cluster needs
// to function.
func (cluster *FoundationDBCluster) MinimumFaultDomains() int {
switch cluster.Spec.RedundancyMode {
case "single":
return 1
case "double":
return 2
case "triple":
return 3
default:
return 1
}
}
// DesiredCoordinatorCount returns the number of coordinators to recruit for
// a cluster.
func (cluster *FoundationDBCluster) DesiredCoordinatorCount() int {
if cluster.Spec.UsableRegions > 1 {
return 9
}
return cluster.MinimumFaultDomains() + cluster.DesiredFaultTolerance()
}
// CountsAreSatisfied checks whether the current counts of processes satisfy
// a desired set of counts.
func (counts ProcessCounts) CountsAreSatisfied(currentCounts ProcessCounts) bool {
desiredValue := reflect.ValueOf(counts)
currentValue := reflect.ValueOf(currentCounts)
for _, index := range processClassIndices {
desired := desiredValue.Field(index).Int()
current := currentValue.Field(index).Int()
if (desired > 0 || current > 0) && desired != current {
return false
}
}
return true
}
// FoundationDBStatus describes the status of the cluster as provided by
// FoundationDB itself.
type FoundationDBStatus struct {
// Client provides the client section of the status.
Client FoundationDBStatusLocalClientInfo `json:"client,omitempty"`
// Cluster provides the cluster section of the status.
Cluster FoundationDBStatusClusterInfo `json:"cluster,omitempty"`
}
// FoundationDBStatusLocalClientInfo contains information about the
// client connection from the process getting the status.
type FoundationDBStatusLocalClientInfo struct {
// Coordinators provides information about the cluster's coordinators.
Coordinators FoundationDBStatusCoordinatorInfo `json:"coordinators,omitempty"`
// DatabaseStatus provides a summary of the database's health.
DatabaseStatus FoundationDBStatusClientDBStatus `json:"database_status,omitempty"`
}
// FoundationDBStatusCoordinatorInfo contains information about the client's
// connection to the coordinators.
type FoundationDBStatusCoordinatorInfo struct {
// Coordinators provides a list with coordinator details.
Coordinators []FoundationDBStatusCoordinator `json:"coordinators,omitempty"`
}
// FoundationDBStatusCoordinator contains information about one of the
// coordinators.
type FoundationDBStatusCoordinator struct {
// Address provides the coordinator's address.
Address string `json:"address,omitempty"`
// Reachable indicates whether the coordinator is reachable.
Reachable bool `json:"reachable,omitempty"`
}
// FoundationDBStatusClusterInfo describes the "cluster" portion of the
// cluster status
type FoundationDBStatusClusterInfo struct {
// DatabaseConfiguration describes the current configuration of the
// database.
DatabaseConfiguration DatabaseConfiguration `json:"configuration,omitempty"`
// Processes provides details on the processes that are reporting to the
// cluster.
Processes map[string]FoundationDBStatusProcessInfo `json:"processes,omitempty"`
// Data provides information about the data in the database.
Data FoundationDBStatusDataStatistics `json:"data,omitempty"`
// FullReplication indicates whether the database is fully replicated.
FullReplication bool `json:"full_replication,omitempty"`
// Clients provides information about clients that are connected to the
// database.
Clients FoundationDBStatusClusterClientInfo `json:"clients,omitemtpy"`
}
// FoundationDBStatusProcessInfo describes the "processes" portion of the
// cluster status
type FoundationDBStatusProcessInfo struct {
// Address provides the address of the process.
Address string `json:"address,omitempty"`
// ProcessClass provides the process class the process has been given.
ProcessClass string `json:"class_type,omitempty"`
// CommandLine provides the command-line invocation for the process.
CommandLine string `json:"command_line,omitempty"`
// Excluded indicates whether the process has been excluded.
Excluded bool `json:"excluded,omitempty"`
// The locality information for the process.
Locality map[string]string `json:"locality,omitempty"`
}
// FoundationDBStatusDataStatistics provides information about the data in
// the database
type FoundationDBStatusDataStatistics struct {
// KVBytes provides the total Key Value Bytes in the database.
KVBytes int `json:"total_kv_size_bytes,omitempty"`
// MovingData provides information about the current data movement.
MovingData FoundationDBStatusMovingData `json:"moving_data,omitempty"`
}
// FoundationDBStatusMovingData provides information about the current data
// movement
type FoundationDBStatusMovingData struct {
// HighestPriority provides the priority of the highest-priority data
// movement.
HighestPriority int `json:"highest_priority,omitempty"`
// InFlightBytes provides how many bytes are being actively moved.
InFlightBytes int `json:"in_flight_bytes,omitempty"`
// InQueueBytes provides how many bytes are pending data movement.
InQueueBytes int `json:"in_queue_bytes,omitempty"`
}
// alphanum provides the characters that are used for the generation ID in the
// connection string.
var alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
// connectionStringPattern provides a regular expression for parsing the
// connection string.
var connectionStringPattern = regexp.MustCompile("(?m)^([^#][^:@]+):([^:@]+)@(.*)$")
// ConnectionString models the contents of a cluster file in a structured way
type ConnectionString struct {
// DatabaseName provides an identifier for the database which persists
// across coordinator changes.
DatabaseName string
// GenerationID provides a unique ID for the current generation of
// coordinators.
GenerationID string
// Coordinators provides the addresses of the current coordinators.
Coordinators []string
}
// ParseConnectionString parses a connection string from its string
// representation
func ParseConnectionString(str string) (ConnectionString, error) {
components := connectionStringPattern.FindStringSubmatch(str)
if components == nil {
return ConnectionString{}, fmt.Errorf("Invalid connection string %s", str)
}
return ConnectionString{
components[1],
components[2],
strings.Split(components[3], ","),
}, nil
}
// String formats a connection string as a string
func (str *ConnectionString) String() string {
return fmt.Sprintf("%s:%s@%s", str.DatabaseName, str.GenerationID, strings.Join(str.Coordinators, ","))
}
// GenerateNewGenerationID builds a new generation ID
func (str *ConnectionString) GenerateNewGenerationID() error {
id := strings.Builder{}
for i := 0; i < 32; i++ {
err := id.WriteByte(alphanum[rand.Intn(len(alphanum))])
if err != nil {
return err
}
}
str.GenerationID = id.String()
return nil
}
// ProcessAddress provides a structured address for a process.
type ProcessAddress struct {
IPAddress string
Port int
Flags map[string]bool
}
// ParseProcessAddress parses a structured address from its string
// representation.
func ParseProcessAddress(address string) (ProcessAddress, error) {
result := ProcessAddress{}
components := strings.Split(address, ":")
result.IPAddress = components[0]
port, err := strconv.Atoi(components[1])
if err != nil {
return result, err
}
result.Port = port
if len(components) > 2 {
result.Flags = make(map[string]bool, len(components)-2)
for _, flag := range components[2:] {
result.Flags[flag] = true
}
}
return result, nil
}
// String gets the string representation of an address.
func (address ProcessAddress) String() string {
result := address.IPAddress + ":" + strconv.Itoa(address.Port)
flags := make([]string, 0, len(address.Flags))
for flag, set := range address.Flags {
if set {
flags = append(flags, flag)
}
}
sort.Slice(flags, func(i int, j int) bool {
return flags[i] < flags[j]
})
for _, flag := range flags {
result = result + ":" + flag
}
return result
}
// GetFullAddress gets the full public address we should use for a process.
// This will include the IP address, the port, and any additional flags.
func (cluster *FoundationDBCluster) GetFullAddress(ipAddress string) string {
return cluster.GetFullAddressList(ipAddress, true)
}
// GetFullAddress gets the full list of public address we should use for a
//process.
//
// This will include the IP address, the port, and any additional flags.
//
// If a process needs multiple addresses, this will include all of them,
// separated by commas. If you pass false for primaryOnly, this will return only
// the primary address.
func (cluster *FoundationDBCluster) GetFullAddressList(ipAddress string, primaryOnly bool) string {
addressMap := make(map[string]bool)
if cluster.Status.RequiredAddresses.TLS {
addressMap[fmt.Sprintf("%s:4500:tls", ipAddress)] = cluster.Spec.MainContainer.EnableTLS
}
if cluster.Status.RequiredAddresses.NonTLS {
addressMap[fmt.Sprintf("%s:4501", ipAddress)] = !cluster.Spec.MainContainer.EnableTLS
}
addresses := make([]string, 1, len(addressMap))
for address, primary := range addressMap {
if primary {
addresses[0] = address
} else if !primaryOnly {
addresses = append(addresses, address)
}
}
return strings.Join(addresses, ",")
}
// GetFullSidecarVersion gets the version of the image for the sidecar,
// including the main FoundationDB version and the sidecar version suffix.
func (cluster *FoundationDBCluster) GetFullSidecarVersion(useRunningVersion bool) string {
version := ""
if useRunningVersion {
version = cluster.Spec.RunningVersion
}
if version == "" {
version = cluster.Spec.Version
}
sidecarVersion := cluster.Spec.SidecarVersions[version]
if sidecarVersion < 1 {
sidecarVersion = cluster.Spec.SidecarVersion
}
if sidecarVersion < 1 {
sidecarVersion = 1
}
return fmt.Sprintf("%s-%d", version, sidecarVersion)
}
// HasCoordinators checks whether this connection string matches a set of
// coordinators.
func (str *ConnectionString) HasCoordinators(coordinators []string) bool {
matchedCoordinators := make(map[string]bool, len(str.Coordinators))
for _, address := range str.Coordinators {
matchedCoordinators[address] = false
}
for _, address := range coordinators {
_, matched := matchedCoordinators[address]
if matched {
matchedCoordinators[address] = true
} else {
return false
}
}
for _, matched := range matchedCoordinators {
if !matched {
return false
}
}
return true
}
// FoundationDBClusterFaultDomain describes the fault domain that a cluster is
// replicated across.
type FoundationDBClusterFaultDomain struct {
// Key provides a topology key for the fault domain to replicate across.
Key string `json:"key,omitempty"`
// Value provides a harcoded value to use for the zoneid for the pods.
Value string `json:"value,omitempty"`
// ValueFrom provides a field selector to use as the source of the fault
// domain.
ValueFrom string `json:"valueFrom,omitempty"`
// ZoneCount provides the number of fault domains in the data center where
// these processes are running. This is only used in the
// `kubernetes-cluster` fault domain strategy.
ZoneCount int `json:"zoneCount,omitempty"`
// ZoneIndex provides the index of this Kubernetes cluster in the list of
// KCs in the data center. This is only used in the `kubernetes-cluster`
// fault domain strategy.
ZoneIndex int `json:"zoneIndex,omitempty"`
}
// DatabaseConfiguration represents the configuration of the database
type DatabaseConfiguration struct {
// RedundancyMode defines the core replication factor for the database.
RedundancyMode string `json:"redundancy_mode,omitempty"`
// StorageEngine defines the storage engine the database uses.
StorageEngine string `json:"storage_engine,omitempty"`
// UsableRegions defines how many regions the database should store data in.
UsableRegions int `json:"usable_regions,omitempty"`
// Regions defines the regions that the database can replicate in.
Regions []Region `json:"regions,omitempty"`
// RoleCounts defines how many processes the database should recruit for
// each role.
RoleCounts
}
// Region represents a region in the database configuration
type Region struct {
// The data centers in this region.
DataCenters []DataCenter `json:"datacenters,omitempty"`
// The number of satellite logs that we should recruit.
SatelliteLogs int `json:"satellite_logs,omitempty"`
// The replication strategy for satellite logs.
SatelliteRedundancyMode string `json:"satellite_redundancy_mode,omitempty"`
}
// DataCenter represents a data center in the region configuration
type DataCenter struct {
// The ID of the data center. This must match the dcid locality field.
ID string `json:"id,omitempty"`
// The priority of this data center when we have to choose a location.
// Higher priorities are preferred over lower priorities.
Priority int `json:"priority,omitempty"`
// Satellite indicates whether the data center is serving as a satellite for
// the region. A value of 1 indicates that it is a satellite, and a value of
// 0 indicates that it is not a satellite.
Satellite int `json:"satellite,omitempty"`
}
// FoundationDBStatusClientDBStatus represents the databaseStatus field in the
// JSON database status
type FoundationDBStatusClientDBStatus struct {
// Available indicates whether the database is accepting traffic.
Available bool `json:"available,omitempty"`
// Healthy indicates whether the database is fully healthy.
Healthy bool `json:"healthy,omitempty"`
}
// FoundationDBStatusClusterClientInfo represents the connected client details in the
// cluster status.
type FoundationDBStatusClusterClientInfo struct {
// Count provides the number of clients connected to the database.
Count int `json:"count,omitempty"`
// SupportedVersions provides information about the versions supported by
// the connected clients.
SupportedVersions []FoundationDBStatusSupportedVersion `json:"supported_versions,omitempty"`
}
// FoundationDBStatusSupportedVersion provides information about a version of
// FDB supported by the connected clients.
type FoundationDBStatusSupportedVersion struct {
// ClientVersion provides the version of FDB the client is connecting
// through.
ClientVersion string `json:"client_version,omitempty"`
// ConnectedClient provides the clients that are using this version.