/
admin_client_mock.go
1100 lines (917 loc) · 35.1 KB
/
admin_client_mock.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
/*
* admin_client_mock.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2020-2022 Apple Inc. and the 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 mock
import (
"context"
"fmt"
"github.com/FoundationDB/fdb-kubernetes-operator/pkg/fdbstatus"
"net"
"strings"
"sync"
"time"
"github.com/FoundationDB/fdb-kubernetes-operator/pkg/podclient/mock"
"github.com/FoundationDB/fdb-kubernetes-operator/pkg/fdbadminclient"
fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
"github.com/FoundationDB/fdb-kubernetes-operator/internal"
"github.com/FoundationDB/fdb-kubernetes-operator/pkg/podmanager"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// AdminClient provides a mock implementation of the cluster admin interface
type AdminClient struct {
Cluster *fdbv1beta2.FoundationDBCluster
KubeClient client.Client
DatabaseConfiguration *fdbv1beta2.DatabaseConfiguration
ExcludedAddresses map[string]fdbv1beta2.None
KilledAddresses map[string]fdbv1beta2.None
Knobs map[string]fdbv1beta2.None
missingLocalities map[fdbv1beta2.ProcessGroupID]fdbv1beta2.None
missingProcessGroups map[fdbv1beta2.ProcessGroupID]fdbv1beta2.None
incorrectCommandLines map[fdbv1beta2.ProcessGroupID]fdbv1beta2.None
FrozenStatus *fdbv1beta2.FoundationDBStatus
Backups map[string]fdbv1beta2.FoundationDBBackupStatusBackupDetails
clientVersions map[string][]string
currentCommandLines map[string]string
VersionProcessGroups map[fdbv1beta2.ProcessGroupID]string
ReincludedAddresses map[string]bool
additionalProcesses []fdbv1beta2.ProcessGroupStatus
localityInfo map[fdbv1beta2.ProcessGroupID]map[string]string
MaxZoneFailuresWithoutLosingData *int
MaxZoneFailuresWithoutLosingAvailability *int
MaintenanceZone fdbv1beta2.FaultDomain
restoreURL string
maintenanceZoneStartTimestamp time.Time
uptimeSecondsForMaintenanceZone float64
TeamTracker []fdbv1beta2.FoundationDBStatusTeamTracker
Logs []fdbv1beta2.FoundationDBStatusLogInfo
mockError error
LagInfo map[string]fdbv1beta2.FoundationDBStatusLagInfo
processesUnderMaintenance map[fdbv1beta2.ProcessGroupID]int64
}
// adminClientCache provides a cache of mock admin clients.
var adminClientCache = make(map[string]*AdminClient)
var adminClientMutex sync.Mutex
// NewMockAdminClient creates an admin client for a cluster.
func NewMockAdminClient(cluster *fdbv1beta2.FoundationDBCluster, kubeClient client.Client) (fdbadminclient.AdminClient, error) {
return NewMockAdminClientUncast(cluster, kubeClient)
}
// NewMockAdminClientUncast creates a mock admin client for a cluster.
// nolint:unparam
// is required because we always return a nil error
func NewMockAdminClientUncast(cluster *fdbv1beta2.FoundationDBCluster, kubeClient client.Client) (*AdminClient, error) {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
cachedClient := adminClientCache[cluster.Name]
if cachedClient == nil {
cachedClient = &AdminClient{
Cluster: cluster.DeepCopy(),
KubeClient: kubeClient,
ExcludedAddresses: make(map[string]fdbv1beta2.None),
ReincludedAddresses: make(map[string]bool),
KilledAddresses: make(map[string]fdbv1beta2.None),
missingProcessGroups: make(map[fdbv1beta2.ProcessGroupID]fdbv1beta2.None),
missingLocalities: make(map[fdbv1beta2.ProcessGroupID]fdbv1beta2.None),
incorrectCommandLines: make(map[fdbv1beta2.ProcessGroupID]fdbv1beta2.None),
localityInfo: make(map[fdbv1beta2.ProcessGroupID]map[string]string),
currentCommandLines: make(map[string]string),
Knobs: make(map[string]fdbv1beta2.None),
VersionProcessGroups: make(map[fdbv1beta2.ProcessGroupID]string),
LagInfo: make(map[string]fdbv1beta2.FoundationDBStatusLagInfo),
processesUnderMaintenance: make(map[fdbv1beta2.ProcessGroupID]int64),
}
adminClientCache[cluster.Name] = cachedClient
cachedClient.Backups = make(map[string]fdbv1beta2.FoundationDBBackupStatusBackupDetails)
} else {
cachedClient.Cluster = cluster.DeepCopy()
}
return cachedClient, nil
}
// ClearMockAdminClients clears the cache of mock Admin clients
func ClearMockAdminClients() {
adminClientCache = map[string]*AdminClient{}
}
// GetStatus gets the database's status
func (client *AdminClient) GetStatus() (*fdbv1beta2.FoundationDBStatus, error) {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return nil, client.mockError
}
if client.FrozenStatus != nil {
return client.FrozenStatus, nil
}
pods := &corev1.PodList{}
err := client.KubeClient.List(context.TODO(), pods, internal.GetPodListOptions(client.Cluster, "", "")...)
if err != nil {
return nil, err
}
status := &fdbv1beta2.FoundationDBStatus{
Cluster: fdbv1beta2.FoundationDBStatusClusterInfo{
Processes: make(map[fdbv1beta2.ProcessGroupID]fdbv1beta2.FoundationDBStatusProcessInfo, len(pods.Items)),
},
}
coordinators := make(map[string]bool)
var coordinatorAddresses []string
if strings.Contains(client.Cluster.Status.ConnectionString, "@") {
coordinatorAddresses = strings.Split(strings.Split(client.Cluster.Status.ConnectionString, "@")[1], ",")
status.Cluster.ConnectionString = client.Cluster.Status.ConnectionString
} else {
coordinatorAddresses = []string{}
}
for _, address := range coordinatorAddresses {
coordinators[address] = false
}
for _, pod := range pods.Items {
processClass, err := podmanager.GetProcessClass(client.Cluster, &pod)
if err != nil {
return nil, err
}
processCount, err := internal.GetServersPerPodForPod(&pod, processClass)
if err != nil {
return nil, err
}
processGroupID := podmanager.GetProcessGroupID(client.Cluster, &pod)
if _, ok := client.missingProcessGroups[processGroupID]; ok {
continue
}
podClient, _ := mock.NewMockFdbPodClient(client.Cluster, &pod)
subs, err := podClient.GetVariableSubstitutions()
if err != nil {
return nil, err
}
processIP, ok := subs["FDB_PUBLIC_IP"]
if !ok {
processIP = pod.Status.PodIP
}
for processIndex := 1; processIndex <= processCount; processIndex++ {
var fdbRoles []fdbv1beta2.FoundationDBStatusProcessRoleInfo
fullAddress := client.Cluster.GetFullAddress(processIP, processIndex)
_, ipExcluded := client.ExcludedAddresses[processIP]
_, addressExcluded := client.ExcludedAddresses[fullAddress.String()]
excluded := ipExcluded || addressExcluded
pClass, err := podmanager.GetProcessClass(client.Cluster, &pod)
if err != nil {
return nil, err
}
command, hasCommandLine := client.currentCommandLines[fullAddress.StringWithoutFlags()]
if !hasCommandLine {
// We only set the command if we don't have the commandline "cached"
command, err = internal.GetStartCommand(client.Cluster, pClass, podClient, processIndex, processCount)
if err != nil {
return nil, err
}
client.currentCommandLines[fullAddress.StringWithoutFlags()] = command
}
if _, ok := client.incorrectCommandLines[processGroupID]; ok {
command += " --locality_incorrect=1"
}
var locality map[string]string
if _, ok := client.missingLocalities[processGroupID]; ok {
locality = map[string]string{}
} else {
locality = map[string]string{
fdbv1beta2.FDBLocalityInstanceIDKey: string(processGroupID),
fdbv1beta2.FDBLocalityZoneIDKey: pod.Name,
fdbv1beta2.FDBLocalityDCIDKey: client.Cluster.Spec.DataCenter,
}
for key, value := range client.localityInfo[processGroupID] {
locality[key] = value
}
for _, container := range pod.Spec.Containers {
for _, envVar := range container.Env {
if envVar.Name == "FDB_DNS_NAME" {
locality[fdbv1beta2.FDBLocalityDNSNameKey] = envVar.Value
if client.Cluster.UseDNSInClusterFile() {
fullAddress.StringAddress = envVar.Value
}
// TODO (johscheuer): This should be set to true. This will add additional information to the
// return address and needs some additional refactoring in the mock code.
// fullAddress.FromHostname = true
}
}
}
if processCount > 1 {
locality[fdbv1beta2.FDBLocalityProcessIDKey] = fmt.Sprintf("%s-%d", processGroupID, processIndex)
}
}
var uptimeSeconds float64 = 60000
underMaintenance := false
if string(client.MaintenanceZone) == locality[fdbv1beta2.FDBLocalityZoneIDKey] || client.MaintenanceZone == "simulation" {
if client.uptimeSecondsForMaintenanceZone != 0.0 {
uptimeSeconds = client.uptimeSecondsForMaintenanceZone
} else {
uptimeSeconds = time.Since(client.maintenanceZoneStartTimestamp).Seconds()
}
underMaintenance = true
}
_, isCoordinator := coordinators[fullAddress.String()]
if isCoordinator && !excluded && !underMaintenance {
coordinators[fullAddress.String()] = true
fdbRoles = append(fdbRoles, fdbv1beta2.FoundationDBStatusProcessRoleInfo{Role: string(fdbv1beta2.ProcessRoleCoordinator)})
}
version, ok := client.VersionProcessGroups[processGroupID]
if !ok {
if client.Cluster.VersionCompatibleUpgradeInProgress() {
for _, container := range pod.Spec.Containers {
if container.Name != fdbv1beta2.MainContainerName {
continue
}
parts := strings.Split(container.Image, ":")
if len(parts) > 1 {
parsedVersion, err := fdbv1beta2.ParseFdbVersion(parts[1])
if err != nil {
continue
}
version = parsedVersion.String()
}
}
}
if version == "" {
version = client.Cluster.Status.RunningVersion
}
}
status.Cluster.Processes[fdbv1beta2.ProcessGroupID(fmt.Sprintf("%s-%d", pod.Name, processIndex))] = fdbv1beta2.FoundationDBStatusProcessInfo{
Address: fullAddress,
ProcessClass: internal.GetProcessClassFromMeta(client.Cluster, pod.ObjectMeta),
CommandLine: command,
Excluded: excluded,
UnderMaintenance: underMaintenance,
Locality: locality,
Version: version,
UptimeSeconds: uptimeSeconds,
Roles: fdbRoles,
}
}
}
for _, processGroup := range client.additionalProcesses {
locality := map[string]string{
fdbv1beta2.FDBLocalityInstanceIDKey: string(processGroup.ProcessGroupID),
fdbv1beta2.FDBLocalityZoneIDKey: string(processGroup.ProcessGroupID),
}
for key, value := range client.localityInfo[processGroup.ProcessGroupID] {
locality[key] = value
}
var uptimeSeconds float64 = 60000
underMaintenance := false
if string(client.MaintenanceZone) == locality[fdbv1beta2.FDBLocalityZoneIDKey] || client.MaintenanceZone == "simulation" {
if client.uptimeSecondsForMaintenanceZone != 0.0 {
uptimeSeconds = client.uptimeSecondsForMaintenanceZone
} else {
uptimeSeconds = time.Since(client.maintenanceZoneStartTimestamp).Seconds()
}
underMaintenance = true
}
fullAddress := client.Cluster.GetFullAddress(processGroup.Addresses[0], 1)
status.Cluster.Processes[processGroup.ProcessGroupID] = fdbv1beta2.FoundationDBStatusProcessInfo{
Address: fullAddress,
ProcessClass: processGroup.ProcessClass,
Locality: locality,
UnderMaintenance: underMaintenance,
Version: client.Cluster.Status.RunningVersion,
UptimeSeconds: uptimeSeconds,
}
}
if client.clientVersions != nil {
supportedVersions := make([]fdbv1beta2.FoundationDBStatusSupportedVersion, 0, len(client.clientVersions))
for version, addresses := range client.clientVersions {
protocolVersion, err := client.GetProtocolVersion(version)
if err != nil {
return nil, err
}
protocolClients := make([]fdbv1beta2.FoundationDBStatusConnectedClient, 0, len(addresses))
for _, address := range addresses {
protocolClients = append(protocolClients, fdbv1beta2.FoundationDBStatusConnectedClient{
Address: address,
LogGroup: fdbv1beta2.LogGroup(client.Cluster.Name),
})
}
supportedVersions = append(supportedVersions, fdbv1beta2.FoundationDBStatusSupportedVersion{
ClientVersion: version,
ProtocolVersion: protocolVersion,
MaxProtocolClients: protocolClients,
})
}
status.Cluster.Clients.SupportedVersions = supportedVersions
}
var countReachableCoordinators int
for address, reachable := range coordinators {
pAddr, err := fdbv1beta2.ParseProcessAddress(address)
if err != nil {
return nil, err
}
status.Client.Coordinators.Coordinators = append(status.Client.Coordinators.Coordinators, fdbv1beta2.FoundationDBStatusCoordinator{
Address: pAddr,
Reachable: reachable,
})
if reachable {
countReachableCoordinators++
}
}
minReachableCoordinators := (client.Cluster.DesiredCoordinatorCount() + 1) / 2
status.Client.Coordinators.QuorumReachable = countReachableCoordinators >= minReachableCoordinators
status.Client.DatabaseStatus.Available = true
status.Client.DatabaseStatus.Healthy = true
if client.DatabaseConfiguration == nil {
status.Cluster.Layers.Error = "configurationMissing"
} else {
status.Cluster.DatabaseConfiguration = *client.DatabaseConfiguration
}
if status.Cluster.DatabaseConfiguration.LogSpill == 0 {
status.Cluster.DatabaseConfiguration.VersionFlags.LogSpill = 2
}
if len(client.ExcludedAddresses) > 0 {
status.Cluster.DatabaseConfiguration.ExcludedServers = make([]fdbv1beta2.ExcludedServers, 0, len(client.ExcludedAddresses))
}
for excludedAddresses := range client.ExcludedAddresses {
if net.ParseIP(excludedAddresses) != nil {
status.Cluster.DatabaseConfiguration.ExcludedServers = append(status.Cluster.DatabaseConfiguration.ExcludedServers, fdbv1beta2.ExcludedServers{Address: excludedAddresses})
} else {
status.Cluster.DatabaseConfiguration.ExcludedServers = append(status.Cluster.DatabaseConfiguration.ExcludedServers, fdbv1beta2.ExcludedServers{Locality: excludedAddresses})
}
}
status.Cluster.FullReplication = true
status.Cluster.Data.State.Healthy = true
status.Cluster.Data.State.Name = "healthy"
if len(client.TeamTracker) == 0 {
status.Cluster.Data.TeamTrackers = []fdbv1beta2.FoundationDBStatusTeamTracker{
{
Primary: true,
State: fdbv1beta2.FoundationDBStatusDataState{
Description: "primary",
Healthy: true,
MinReplicasRemaining: fdbv1beta2.MinimumFaultDomains(client.Cluster.Spec.DatabaseConfiguration.RedundancyMode),
},
},
}
if len(client.Cluster.Spec.DatabaseConfiguration.Regions) > 1 {
status.Cluster.Data.TeamTrackers = append(status.Cluster.Data.TeamTrackers, fdbv1beta2.FoundationDBStatusTeamTracker{
Primary: false,
State: fdbv1beta2.FoundationDBStatusDataState{
Description: "remote",
Healthy: true,
MinReplicasRemaining: fdbv1beta2.MinimumFaultDomains(client.Cluster.Spec.DatabaseConfiguration.RedundancyMode),
},
})
}
} else {
status.Cluster.Data.TeamTrackers = client.TeamTracker
}
if len(client.Logs) == 0 {
logInformation := fdbv1beta2.FoundationDBStatusLogInfo{
Current: true,
LogFaultTolerance: 2,
LogReplicationFactor: 3,
}
if len(client.Cluster.Spec.DatabaseConfiguration.Regions) > 1 {
logInformation.RemoteLogFaultTolerance = 2
logInformation.RemoteLogReplicationFactor = 3
logInformation.SatelliteLogFaultTolerance = 1
logInformation.SatelliteLogReplicationFactor = 2
}
status.Cluster.Logs = []fdbv1beta2.FoundationDBStatusLogInfo{
logInformation,
}
} else {
status.Cluster.Logs = client.Logs
}
if len(client.Backups) > 0 {
status.Cluster.Layers.Backup.Tags = make(map[string]fdbv1beta2.FoundationDBStatusBackupTag, len(client.Backups))
for tag, tagStatus := range client.Backups {
status.Cluster.Layers.Backup.Tags[tag] = fdbv1beta2.FoundationDBStatusBackupTag{
CurrentContainer: tagStatus.URL,
RunningBackup: tagStatus.Running,
Restorable: true,
}
status.Cluster.Layers.Backup.Paused = tagStatus.Paused
}
}
faultToleranceSubtractor := 0
if client.MaintenanceZone != "" {
faultToleranceSubtractor = 1
}
if client.MaxZoneFailuresWithoutLosingData == nil {
status.Cluster.FaultTolerance.MaxZoneFailuresWithoutLosingData = client.Cluster.DesiredFaultTolerance() - faultToleranceSubtractor
}
if client.MaxZoneFailuresWithoutLosingAvailability == nil {
status.Cluster.FaultTolerance.MaxZoneFailuresWithoutLosingAvailability = client.Cluster.DesiredFaultTolerance() - faultToleranceSubtractor
}
status.Cluster.MaintenanceZone = client.MaintenanceZone
if len(client.LagInfo) > 0 {
limitingDurabilityLag, ok := client.GetLimitingDurabilityLag()
if ok {
status.Cluster.Qos.LimitingDurabilityLagStorageServer = limitingDurabilityLag
}
worstDataLag, ok := client.GetWorstDataLag()
if ok {
status.Cluster.Qos.WorstDataLagStorageServer = worstDataLag
}
worstDurabilityLag, ok := client.GetWorstDurabilityLag()
if ok {
status.Cluster.Qos.WorstDurabilityLagStorageServer = worstDurabilityLag
}
}
status.Cluster.RecoveryState = fdbv1beta2.RecoveryState{
Name: "fully_recovered",
SecondsSinceLastRecovered: 600.0,
ActiveGenerations: 1,
}
return status, nil
}
// ConfigureDatabase changes the database configuration
func (client *AdminClient) ConfigureDatabase(configuration fdbv1beta2.DatabaseConfiguration, _ bool, version string) error {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return client.mockError
}
client.DatabaseConfiguration = configuration.DeepCopy()
ver, err := fdbv1beta2.ParseFdbVersion(version)
if err != nil {
return err
}
if !ver.HasSeparatedProxies() {
client.DatabaseConfiguration.GrvProxies = 0
client.DatabaseConfiguration.CommitProxies = 0
}
return nil
}
// ExcludeProcesses starts evacuating processes so that they can be removed
// from the database.
func (client *AdminClient) ExcludeProcesses(addresses []fdbv1beta2.ProcessAddress) error {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return client.mockError
}
for _, pAddr := range addresses {
address := pAddr.String()
client.ExcludedAddresses[address] = fdbv1beta2.None{}
}
return nil
}
// IncludeProcesses removes processes from the exclusion list and allows
// them to take on roles again.
func (client *AdminClient) IncludeProcesses(addresses []fdbv1beta2.ProcessAddress) error {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
for _, address := range addresses {
address := address.String()
_, ok := client.ExcludedAddresses[address]
if ok {
client.ReincludedAddresses[address] = true
delete(client.ExcludedAddresses, address)
}
}
return nil
}
// CanSafelyRemove checks whether it is safe to remove the process group from the
// cluster
//
// The list returned by this method will be the addresses that are *not*
// safe to remove.
func (client *AdminClient) CanSafelyRemove(addresses []fdbv1beta2.ProcessAddress) ([]fdbv1beta2.ProcessAddress, error) {
if client.mockError != nil {
return nil, client.mockError
}
skipExclude := map[string]fdbv1beta2.None{}
// Check which process groups have the skip exclusion flag or are already
// excluded
for _, pg := range client.Cluster.Status.ProcessGroups {
if !(pg.IsExcluded()) {
continue
}
for _, addr := range pg.Addresses {
skipExclude[addr] = fdbv1beta2.None{}
}
}
// Add all process groups that are excluded in the client
for addr := range client.ExcludedAddresses {
skipExclude[addr] = fdbv1beta2.None{}
}
// Filter out all excluded process groups and also all process groups
// that skip exclusion
remaining := make([]fdbv1beta2.ProcessAddress, 0, len(addresses))
for _, addr := range addresses {
// Is already excluded or skipped
if _, ok := skipExclude[addr.String()]; ok {
continue
}
remaining = append(remaining, addr)
}
return remaining, nil
}
// GetExclusions gets a list of the addresses currently excluded from the
// database.
func (client *AdminClient) GetExclusions() ([]fdbv1beta2.ProcessAddress, error) {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return nil, client.mockError
}
pAddrs := make([]fdbv1beta2.ProcessAddress, 0, len(client.ExcludedAddresses))
for addr := range client.ExcludedAddresses {
ip := net.ParseIP(addr)
if ip == nil {
pAddrs = append(pAddrs, fdbv1beta2.ProcessAddress{StringAddress: addr})
} else {
pAddrs = append(pAddrs, fdbv1beta2.ProcessAddress{
IPAddress: net.ParseIP(addr),
Port: 0,
Flags: nil,
})
}
}
return pAddrs, nil
}
// KillProcesses restarts processes
func (client *AdminClient) KillProcesses(addresses []fdbv1beta2.ProcessAddress) error {
adminClientMutex.Lock()
if client.mockError != nil {
return client.mockError
}
for _, addr := range addresses {
client.KilledAddresses[addr.String()] = fdbv1beta2.None{}
// Remove the commandline from the cached status and let it be recomputed in the next GetStatus request.
// This reflects that the commandline will only be updated if the processes are actually be restarted.
delete(client.currentCommandLines, addr.StringWithoutFlags())
}
adminClientMutex.Unlock()
if client.Cluster.Status.RunningVersion != client.Cluster.Spec.Version {
// We have to do this in the mock client, in the real world the tryConnectionOptions in update_status,
// will update the version.
client.Cluster.Status.RunningVersion = client.Cluster.Spec.Version
err := client.KubeClient.Status().Update(context.TODO(), client.Cluster)
if err != nil {
return err
}
}
client.UnfreezeStatus()
return nil
}
// ChangeCoordinators changes the coordinator set
func (client *AdminClient) ChangeCoordinators(addresses []fdbv1beta2.ProcessAddress) (string, error) {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return "", client.mockError
}
connectionString, err := fdbv1beta2.ParseConnectionString(client.Cluster.Status.ConnectionString)
if err != nil {
return "", err
}
err = connectionString.GenerateNewGenerationID()
if err != nil {
return "", err
}
newCoord := make([]string, len(addresses))
for idx, coord := range addresses {
newCoord[idx] = coord.String()
}
connectionString.Coordinators = newCoord
return connectionString.String(), err
}
// GetConnectionString fetches the latest connection string.
func (client *AdminClient) GetConnectionString() (string, error) {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
return client.Cluster.Status.ConnectionString, nil
}
// VersionSupported reports whether we can support a cluster with a given
// version.
func (client *AdminClient) VersionSupported(versionString string) (bool, error) {
if client.mockError != nil {
return false, client.mockError
}
version, err := fdbv1beta2.ParseFdbVersion(versionString)
if err != nil {
return false, err
}
if !version.IsSupported() {
return false, nil
}
return true, nil
}
// GetProtocolVersion determines the protocol version that is used by a
// version of FDB.
func (client *AdminClient) GetProtocolVersion(version string) (string, error) {
if client.mockError != nil {
return "", client.mockError
}
return version, nil
}
// StartBackup starts a new backup.
func (client *AdminClient) StartBackup(url string, snapshotPeriodSeconds int) error {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return client.mockError
}
client.Backups["default"] = fdbv1beta2.FoundationDBBackupStatusBackupDetails{
URL: url,
Running: true,
SnapshotPeriodSeconds: snapshotPeriodSeconds,
}
return nil
}
// PauseBackups pauses backups.
func (client *AdminClient) PauseBackups() error {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return client.mockError
}
for tag, backup := range client.Backups {
backup.Paused = true
client.Backups[tag] = backup
}
return nil
}
// ResumeBackups resumes backups.
func (client *AdminClient) ResumeBackups() error {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return client.mockError
}
for tag, backup := range client.Backups {
backup.Paused = false
client.Backups[tag] = backup
}
return nil
}
// ModifyBackup reconfigures the backup.
func (client *AdminClient) ModifyBackup(snapshotPeriodSeconds int) error {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return client.mockError
}
backup := client.Backups["default"]
backup.SnapshotPeriodSeconds = snapshotPeriodSeconds
client.Backups["default"] = backup
return nil
}
// StopBackup stops a backup.
func (client *AdminClient) StopBackup(url string) error {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return client.mockError
}
for tag, backup := range client.Backups {
if backup.URL == url {
backup.Running = false
client.Backups[tag] = backup
return nil
}
}
return fmt.Errorf("no backup found for URL %s", url)
}
// GetBackupStatus gets the status of the current backup.
func (client *AdminClient) GetBackupStatus() (*fdbv1beta2.FoundationDBLiveBackupStatus, error) {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return nil, client.mockError
}
status := &fdbv1beta2.FoundationDBLiveBackupStatus{}
tag := "default"
backup, present := client.Backups[tag]
if present {
status.DestinationURL = backup.URL
status.Status.Running = backup.Running
status.BackupAgentsPaused = backup.Paused
status.SnapshotIntervalSeconds = backup.SnapshotPeriodSeconds
}
return status, nil
}
// StartRestore starts a new restore.
func (client *AdminClient) StartRestore(url string, _ []fdbv1beta2.FoundationDBKeyRange) error {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return client.mockError
}
client.restoreURL = url
return nil
}
// GetRestoreStatus gets the status of the current restore.
func (client *AdminClient) GetRestoreStatus() (string, error) {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.mockError != nil {
return "", client.mockError
}
return fmt.Sprintf("%s\n", client.restoreURL), nil
}
// MockClientVersion returns a mocked client version
func (client *AdminClient) MockClientVersion(version string, clients []string) {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
if client.clientVersions == nil {
client.clientVersions = make(map[string][]string)
}
client.clientVersions[version] = clients
}
// MockAdditionalProcesses adds additional processes to the cluster status.
func (client *AdminClient) MockAdditionalProcesses(processes []fdbv1beta2.ProcessGroupStatus) {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
client.additionalProcesses = append(client.additionalProcesses, processes...)
}
// MockMissingProcessGroup updates the mock for whether a process group should
// be missing from the cluster status.
func (client *AdminClient) MockMissingProcessGroup(processGroupID fdbv1beta2.ProcessGroupID, missing bool) {
if missing {
client.missingProcessGroups[processGroupID] = fdbv1beta2.None{}
return
}
delete(client.missingProcessGroups, processGroupID)
}
// MockLocalityInfo sets mock locality information for a process.
func (client *AdminClient) MockLocalityInfo(processGroupID fdbv1beta2.ProcessGroupID, locality map[string]string) {
client.localityInfo[processGroupID] = locality
}
// MockIncorrectCommandLine updates the mock for whether a process group should
// be have an incorrect command-line.
func (client *AdminClient) MockIncorrectCommandLine(processGroupID fdbv1beta2.ProcessGroupID, incorrect bool) {
if incorrect {
client.incorrectCommandLines[processGroupID] = fdbv1beta2.None{}
return
}
delete(client.incorrectCommandLines, processGroupID)
}
// MockMissingLocalities updates the mock to remove the localities for the provided process group.
func (client *AdminClient) MockMissingLocalities(processGroupID fdbv1beta2.ProcessGroupID, missingLocalities bool) {
if missingLocalities {
client.missingLocalities[processGroupID] = fdbv1beta2.None{}
return
}
delete(client.missingLocalities, processGroupID)
}
// Close shuts down any resources for the client once it is no longer
// needed.
func (client *AdminClient) Close() error {
return client.mockError
}
// FreezeStatus causes the GetStatus method to return its current value until
// UnfreezeStatus is called, or another method is called which would invalidate
// the status.
func (client *AdminClient) FreezeStatus() error {
if client.mockError != nil {
return client.mockError
}
status, err := client.GetStatus()
if err != nil {
return err
}
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
client.FrozenStatus = status
return nil
}
// UnfreezeStatus causes the admin client to start recalculating the status
// on every call to GetStatus
func (client *AdminClient) UnfreezeStatus() {
adminClientMutex.Lock()
defer adminClientMutex.Unlock()
client.FrozenStatus = nil
}
// GetCoordinatorSet gets the current coordinators from the status
func (client *AdminClient) GetCoordinatorSet() (map[string]fdbv1beta2.None, error) {
if client.mockError != nil {
return nil, client.mockError
}
status, err := client.GetStatus()
if err != nil {
return nil, err
}
return fdbstatus.GetCoordinatorsFromStatus(status), nil
}
// SetKnobs sets the knobs that should be used for the commandline call.
func (client *AdminClient) SetKnobs(knobs []string) {
client.Knobs = make(map[string]fdbv1beta2.None, len(knobs))
for _, knob := range knobs {
client.Knobs[knob] = fdbv1beta2.None{}
}
}
// GetMaintenanceZone gets current maintenance zone, if any
func (client *AdminClient) GetMaintenanceZone() (string, error) {
if client.mockError != nil {
return "", client.mockError
}
return string(client.MaintenanceZone), nil
}
// SetMaintenanceZone places zone into maintenance mode
func (client *AdminClient) SetMaintenanceZone(zone string, _ int) error {
if client.mockError != nil {
return client.mockError
}
client.MaintenanceZone = fdbv1beta2.FaultDomain(zone)
client.maintenanceZoneStartTimestamp = time.Now()
return nil
}
// ResetMaintenanceMode resets the maintenance zone
func (client *AdminClient) ResetMaintenanceMode() error {
if client.mockError != nil {
return client.mockError
}
client.MaintenanceZone = ""
return nil
}
// MockUptimeSecondsForMaintenanceZone mocks the uptime for maintenance zone