/
ha_fdb_cluster.go
314 lines (272 loc) · 10.3 KB
/
ha_fdb_cluster.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
/*
* ha_fdb_cluster.go
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2023 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 fixtures
import (
"fmt"
fdbv1beta2 "github.com/FoundationDB/fdb-kubernetes-operator/api/v1beta2"
"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
"github.com/onsi/gomega"
"golang.org/x/sync/errgroup"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strings"
)
// This file contains fixtures to set up HA configurations.
const (
// PrimaryID is the suffix for the primary FoundationDBCluster
PrimaryID = "primary"
// RemoteID is the suffix for the remote FoundationDBCluster
RemoteID = "remote"
// PrimarySatelliteID is the suffix for the primary satellite FoundationDBCluster
PrimarySatelliteID = "primary-satellite"
// RemoteSatelliteID is the suffix for the remote satellite FoundationDBCluster
RemoteSatelliteID = "remote-satellite"
// SatelliteID is the suffix for the satellite FoundationDBCluster
SatelliteID = "satellite"
)
// HaFdbCluster is a struct around handling HA FoundationDBClusters.
type HaFdbCluster struct {
clusters []*FdbCluster
}
// GetPrimary returns the FoundationDBCluster matching the suffix from PrimaryID.
func (haFDBCluster *HaFdbCluster) GetPrimary() *FdbCluster {
return haFDBCluster.GetCluster(PrimaryID)
}
// GetRemote returns the FoundationDBCluster matching the suffix from RemoteID.
func (haFDBCluster *HaFdbCluster) GetRemote() *FdbCluster {
return haFDBCluster.GetCluster(RemoteID)
}
// GetPrimarySatellite returns the FoundationDBCluster matching the suffix from PrimarySatelliteID.
// This function is only valid for HA clusters using 2 satellites.
func (haFDBCluster *HaFdbCluster) GetPrimarySatellite() *FdbCluster {
if len(haFDBCluster.clusters) == 3 {
return nil
}
return haFDBCluster.GetCluster(PrimarySatelliteID)
}
// GetRemoteSatellite returns the FoundationDBCluster matching the suffix from RemoteSatelliteID.
// This function is only valid for HA clusters using 2 satellites.
func (haFDBCluster *HaFdbCluster) GetRemoteSatellite() *FdbCluster {
if len(haFDBCluster.clusters) == 3 {
return nil
}
return haFDBCluster.GetCluster(RemoteSatelliteID)
}
// GetSatellite this is only for threeZoneDoubleSat config, where `satellite` is primary satellite for both regions.
func (haFDBCluster *HaFdbCluster) GetSatellite() *FdbCluster {
if len(haFDBCluster.clusters) == 4 {
return nil
}
return haFDBCluster.GetCluster(SatelliteID)
}
// GetAllClusters Returns all FoundationDBClusters that span this HA cluster.
func (haFDBCluster *HaFdbCluster) GetAllClusters() []*FdbCluster {
return haFDBCluster.clusters
}
// GetCluster returns the FoundationDBCluster with the provided suffix.
func (haFDBCluster *HaFdbCluster) GetCluster(suffix string) *FdbCluster {
for _, fdbCluster := range haFDBCluster.clusters {
if strings.HasSuffix(fdbCluster.Name(), suffix) {
return fdbCluster
}
}
return nil
}
func (haFDBCluster *HaFdbCluster) addCluster(fdbCluster *FdbCluster) error {
if haFDBCluster.clusters == nil {
haFDBCluster.clusters = []*FdbCluster{}
}
// Check if the cluster is already in the slice
if haFDBCluster.GetCluster(fdbCluster.Name()) != nil {
return haFDBCluster.updateCluster(fdbCluster)
}
haFDBCluster.clusters = append(haFDBCluster.clusters, fdbCluster)
return nil
}
func (haFDBCluster *HaFdbCluster) updateCluster(fdbCluster *FdbCluster) error {
for idx, cluster := range haFDBCluster.clusters {
if strings.HasSuffix(cluster.Name(), fdbCluster.Name()) {
haFDBCluster.clusters[idx] = fdbCluster
return nil
}
}
return fmt.Errorf("cluster %s does not exist", fdbCluster.Name())
}
// GetNamespaces returns the namespaces where all FoundationDBClusters are running in.
func (haFDBCluster *HaFdbCluster) GetNamespaces() []string {
res := make([]string, 0, len(haFDBCluster.clusters))
for _, fdbCluster := range haFDBCluster.clusters {
res = append(res, fdbCluster.Namespace())
}
return res
}
// getLabelSelector returns the LabelSelectorRequirement that includes all selectors for the FDB cluster. The key will
// be the same and the value will be the 4 clusters that were created.
func (haFDBCluster *HaFdbCluster) getLabelSelector() []metav1.LabelSelectorRequirement {
var key string
var values = make([]string, 0, len(haFDBCluster.clusters))
for _, fdbCluster := range haFDBCluster.clusters {
// GetResourceLabels will return a map containing one key and one value
for labelKey, value := range fdbCluster.GetCachedCluster().GetResourceLabels() {
key = labelKey
values = append(values, value)
}
}
return []metav1.LabelSelectorRequirement{
{
Key: key,
Values: values,
Operator: metav1.LabelSelectorOpIn,
},
}
}
// GetNamespaceSelector returns the chaos mesh selector for this FDB HA cluster and all associated Pods.
func (haFDBCluster *HaFdbCluster) GetNamespaceSelector() v1alpha1.PodSelectorSpec {
return chaosNamespaceLabelRequirement(
haFDBCluster.GetNamespaces(),
haFDBCluster.getLabelSelector(),
)
}
// SetDatabaseConfiguration sets the new DatabaseConfiguration, without waiting for reconciliation.
func (haFDBCluster *HaFdbCluster) SetDatabaseConfiguration(
config fdbv1beta2.DatabaseConfiguration,
) {
for _, fdbCluster := range haFDBCluster.clusters {
gomega.Expect(fdbCluster.SetDatabaseConfiguration(config, false)).NotTo(gomega.HaveOccurred())
}
}
// WaitForReconciliation waits for all associated FoundationDBClusters to be reconciled
func (haFDBCluster *HaFdbCluster) WaitForReconciliation(
options ...func(*ReconciliationOptions),
) error {
g := new(errgroup.Group)
for _, cluster := range haFDBCluster.GetAllClusters() {
singleCluster := cluster // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
return singleCluster.WaitForReconciliation(options...)
})
}
return g.Wait()
}
func (factory Factory) createHaFdbClusterSpec(
config *ClusterConfig,
dcID string,
seedConnection string,
databaseConfiguration *fdbv1beta2.DatabaseConfiguration,
) *fdbv1beta2.FoundationDBCluster {
cluster := factory.createFDBClusterSpec(
config,
*databaseConfiguration,
)
cluster.Spec.ProcessGroupIDPrefix = dcID
cluster.Spec.DataCenter = dcID
cluster.Spec.SeedConnectionString = seedConnection
return cluster
}
// Delete removes all Clusters associated FoundationDBClusters.
func (haFDBCluster *HaFdbCluster) Delete() {
for _, cluster := range haFDBCluster.GetAllClusters() {
gomega.Expect(cluster.Destroy()).NotTo(gomega.HaveOccurred())
}
}
// UpgradeCluster upgrades the HA FoundationDBCluster to the specified version.
func (haFDBCluster *HaFdbCluster) UpgradeCluster(version string, waitForReconciliation bool) error {
return haFDBCluster.UpgradeClusterWithTimeout(version, waitForReconciliation, 0)
}
// UpgradeClusterWithTimeout upgrades the HA FoundationDBCluster to the specified version with the provided timeout.
func (haFDBCluster *HaFdbCluster) UpgradeClusterWithTimeout(
version string,
waitForReconciliation bool,
timeout int,
) error {
expectedGenerations := map[string]int64{}
for _, cluster := range haFDBCluster.GetAllClusters() {
curCluster := cluster.GetCluster()
generation := curCluster.ObjectMeta.Generation
// Only increase the expected generation if the cluster is not already upgraded.
if curCluster.Spec.Version != version {
generation++
}
expectedGenerations[cluster.Name()] = generation
cluster.cluster = curCluster
err := cluster.UpgradeCluster(version, false)
if err != nil {
return err
}
}
if !waitForReconciliation {
return nil
}
g := new(errgroup.Group)
for _, cluster := range haFDBCluster.GetAllClusters() {
singleCluster := cluster // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
return singleCluster.WaitForReconciliation(MinimumGenerationOption(expectedGenerations[singleCluster.Name()]), TimeOutInSecondsOption(timeout), PollTimeInSecondsOption(30))
})
}
return g.Wait()
}
// DumpState logs the current state of all FoundationDBClusters.
func (haFDBCluster *HaFdbCluster) DumpState() {
for _, cluster := range haFDBCluster.clusters {
cluster.factory.DumpState(cluster)
}
}
// SetCustomParameters sets the custom parameters for the provided process class.
func (haFDBCluster *HaFdbCluster) SetCustomParameters(processClass fdbv1beta2.ProcessClass,
customParameters fdbv1beta2.FoundationDBCustomParameters,
waitForReconcile bool) error {
for _, cluster := range haFDBCluster.clusters {
err := cluster.SetCustomParameters(processClass, customParameters, false)
if err != nil {
return err
}
}
if !waitForReconcile {
return nil
}
for _, cluster := range haFDBCluster.clusters {
err := cluster.WaitForReconciliation()
if err != nil {
return err
}
}
return nil
}
// VerifyVersion Checks if cluster is running at the expectedVersion. This is done by checking the status of the FoundationDBCluster status.
// Before that we checked the cluster status json by checking the reported version of all processes. This approach only worked for
// version compatible upgrades, since incompatible processes won't be part of the cluster anyway. To simplify the check
// we verify the reported running version from the operator.
func (haFDBCluster *HaFdbCluster) VerifyVersion(version string) {
g := new(errgroup.Group)
for _, cluster := range haFDBCluster.GetAllClusters() {
singleCluster := cluster // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
return singleCluster.WaitUntilWithForceReconcile(2, 600, func(cluster *fdbv1beta2.FoundationDBCluster) bool {
return cluster.Status.RunningVersion == version
})
})
}
// Add more context to the error.
err := g.Wait()
if err != nil {
err = fmt.Errorf("timeout waiting for all clusters to be upgraded to %s, original error: %w", version, err)
}
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}