/
utils.go
428 lines (374 loc) · 12.1 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
package utils
import (
"context"
"fmt"
"io/ioutil"
"reflect"
"strconv"
"strings"
"testing"
"time"
"github.com/ViaQ/logerr/v2/log"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
loggingv1 "github.com/openshift/elasticsearch-operator/apis/logging/v1"
"github.com/openshift/elasticsearch-operator/internal/elasticsearch/esclient"
"github.com/openshift/elasticsearch-operator/internal/utils"
)
func GetFileContents(filePath string) []byte {
contents, err := ioutil.ReadFile(filePath)
if err != nil {
log.NewLogger("testing-utils").Error(err, "Unable to read file to get contents")
return nil
}
return contents
}
func ConfigMap(name, namespace string, labels, data map[string]string) *corev1.ConfigMap {
return &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: labels,
},
Data: data,
}
}
func Secret(secretName string, namespace string, data map[string][]byte) *corev1.Secret {
return &corev1.Secret{
TypeMeta: metav1.TypeMeta{
Kind: "Secret",
APIVersion: corev1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: namespace,
},
Type: "Opaque",
Data: data,
}
}
func WaitForDeployment(t *testing.T, f client.Client, namespace, name string, replicas int,
retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
lookupKey := types.NamespacedName{Name: name, Namespace: namespace}
deployment := &apps.Deployment{}
err = f.Get(context.Background(), lookupKey, deployment)
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of Deployment: %s in Namespace: %s \n", name, namespace)
return false, nil
}
return false, err
}
if int(deployment.Status.AvailableReplicas) >= replicas {
return true, nil
}
t.Logf("Waiting for full availability of %s deployment (%d/%d)\n", name,
deployment.Status.AvailableReplicas, replicas)
return false, nil
})
if err != nil {
return err
}
t.Logf("Deployment %s available (%d/%d)\n", name, replicas, replicas)
return nil
}
func WaitForPods(t *testing.T, f client.Client, namespace string, labels map[string]string, retryInterval, timeout time.Duration) (*corev1.PodList, error) {
pods := &corev1.PodList{}
err := wait.Poll(retryInterval, timeout, func() (bool, error) {
opts := []client.ListOption{
client.InNamespace(namespace),
client.MatchingLabels(labels),
}
err := f.List(context.TODO(), pods, opts...)
if err != nil {
if apierrors.IsNotFound(err) {
return false, err
}
return false, nil
}
return true, nil
})
if err != nil {
return nil, err
}
return pods, nil
}
func WaitForRolloutComplete(t *testing.T, f client.Client, namespace string, labels map[string]string, excludePods []string, expectedPodCount int, retryInterval, timeout time.Duration) (*corev1.PodList, error) {
pods := &corev1.PodList{}
opts := []client.ListOption{
client.InNamespace(namespace),
client.MatchingLabels(labels),
}
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
err = f.List(context.TODO(), pods, opts...)
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of pods with labels: %v in Namespace: %s \n", labels, namespace)
return false, nil
}
return false, err
}
readyPods := 0
for _, pod := range pods.Items {
for _, excluded := range excludePods {
if pod.GetName() == excluded {
// Retry we matched at least one excluded pod
return false, nil
}
}
for _, cond := range pod.Status.Conditions {
if cond.Type == corev1.PodReady {
readyPods = readyPods + 1
}
}
}
if readyPods == expectedPodCount && len(pods.Items) == readyPods {
return true, nil
}
t.Logf("Waiting for availability of pods with labels: %v in Namespace: %s (%d/%d)\n",
labels, namespace, readyPods, len(pods.Items),
)
return false, nil
})
if err != nil {
return nil, err
}
t.Logf("Pods ready")
return pods, nil
}
func WaitForNodeStatusCondition(t *testing.T, f client.Client, namespace, name string, condition loggingv1.ElasticsearchNodeUpgradeStatus, retryInterval, timeout time.Duration) error {
elasticsearchCR := &loggingv1.Elasticsearch{}
elasticsearchName := types.NamespacedName{Name: name, Namespace: namespace}
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
err = f.Get(context.TODO(), elasticsearchName, elasticsearchCR)
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of %s elasticsearch\n", name)
return false, nil
}
return false, err
}
allMatch := true
for _, node := range elasticsearchCR.Status.Nodes {
t.Log("\tActual status", node.UpgradeStatus)
t.Log("\tDesired status", condition)
if !reflect.DeepEqual(node.UpgradeStatus, condition) {
t.Log("\t\tDid not match")
allMatch = false
} else {
t.Log("\t\tMatch!")
break
}
}
if allMatch {
return true, nil
}
t.Logf("Waiting for full condition match of %s elasticsearch\n", name)
return false, nil
})
if err != nil {
return err
}
t.Logf("Full condition matches\n")
return nil
}
func WaitForClusterStatusCondition(t *testing.T, f client.Client, namespace, name string, condition loggingv1.ClusterCondition, retryInterval, timeout time.Duration) error {
elasticsearchCR := &loggingv1.Elasticsearch{}
elasticsearchName := types.NamespacedName{Name: name, Namespace: namespace}
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
err = f.Get(context.TODO(), elasticsearchName, elasticsearchCR)
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of %s elasticsearch\n", name)
return false, nil
}
return false, err
}
contained := false
for _, clusterCondition := range elasticsearchCR.Status.Conditions {
t.Log("\tExpected condition", condition)
t.Log("\tReal condition", clusterCondition)
if reflect.DeepEqual(clusterCondition, condition) {
contained = true
}
}
if contained {
return true, nil
}
t.Logf("Waiting for full condition match of %s elasticsearch\n", name)
return false, nil
})
if err != nil {
return err
}
t.Logf("Full condition matches\n")
return nil
}
func WaitForReadyDeployment(t *testing.T, kubeclient client.Client, namespace, name string, replicas int,
retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
lookupKey := types.NamespacedName{Name: name, Namespace: namespace}
deployment := &apps.Deployment{}
err = kubeclient.Get(context.Background(), lookupKey, deployment)
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of Deployment: %s in Namespace: %s \n", name, namespace)
return false, nil
}
return false, err
}
if int(deployment.Status.ReadyReplicas) >= replicas {
return true, nil
}
t.Logf("Waiting for full readiness of %s deployment (%d/%d)\n", name,
deployment.Status.ReadyReplicas, replicas)
return false, nil
})
if err != nil {
return err
}
t.Logf("Deployment %s ready (%d/%d)\n", name, replicas, replicas)
return nil
}
func WaitForStatefulset(t *testing.T, kubeclient client.Client, namespace, name string, replicas int, retryInterval, timeout time.Duration) error {
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
lookupKey := types.NamespacedName{Name: name, Namespace: namespace}
statefulset := &apps.StatefulSet{}
err = kubeclient.Get(context.Background(), lookupKey, statefulset)
if err != nil {
if apierrors.IsNotFound(err) {
t.Logf("Waiting for availability of %s statefulset\n", name)
return false, nil
}
return false, err
}
if int(statefulset.Status.ReadyReplicas) == replicas {
return true, nil
}
t.Logf("Waiting for full availability of %s statefulset (%d/%d)\n", name, statefulset.Status.ReadyReplicas, replicas)
return false, nil
})
if err != nil {
return err
}
t.Logf("Statefulset %s available (%d/%d)\n", name, replicas, replicas)
return nil
}
func GenerateUUID() string {
uuid, err := utils.RandStringBytes(8)
if err != nil {
return ""
}
return uuid
}
func WaitForIndexTemplateReplicas(t *testing.T, kubeclient kubernetes.Interface, namespace, clusterName string, replicas int32, retryInterval, timeout time.Duration) error {
// mock out Secret response from client
mockClient := fake.NewFakeClient(getMockedSecret(clusterName, namespace))
esClient := esclient.NewClient(log.NewLogger("testing-utils"), clusterName, namespace, mockClient)
stringReplicas := fmt.Sprintf("%d", replicas)
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
// get all index replica count
indexTemplates, err := esClient.GetIndexTemplates()
if err != nil {
t.Logf("Received error: %v", err)
return false, nil
}
// for each index -- check replica count
for templateName, template := range indexTemplates {
currentReplicas := template.Settings.Index.NumberOfReplicas
if currentReplicas == stringReplicas {
continue
}
t.Logf("Index template %s did not have correct replica count (%s/%d)", templateName, currentReplicas, replicas)
return false, nil
}
return true, nil
})
if err != nil {
return err
}
t.Logf("All index templates have correct replica count of %d\n", replicas)
return nil
}
func WaitForIndexReplicas(t *testing.T, kubeclient kubernetes.Interface, namespace, clusterName string, replicas int32, retryInterval, timeout time.Duration) error {
// mock out Secret response from client
mockClient := fake.NewFakeClient(getMockedSecret(clusterName, namespace))
esClient := esclient.NewClient(log.NewLogger("testing-utils"), clusterName, namespace, mockClient)
err := wait.Poll(retryInterval, timeout, func() (done bool, err error) {
// get all index replica count
indexHealth, err := esClient.GetIndexReplicaCounts()
if err != nil {
return false, nil
}
// for each index -- check replica count
for index, health := range indexHealth {
if numberOfReplicas := parseString("settings.index.number_of_replicas", health.(map[string]interface{})); numberOfReplicas != "" {
currentReplicas, err := strconv.ParseInt(numberOfReplicas, 10, 32)
if err != nil {
return false, err
}
if int32(currentReplicas) == replicas {
continue
}
t.Logf("Index %s did not have correct replica count (%d/%d)", index, currentReplicas, replicas)
return false, nil
} else {
return false, nil
}
}
return true, nil
})
if err != nil {
return err
}
t.Logf("All indices have correct replica count of %d\n", replicas)
return nil
}
func getMockedSecret(clusterName, namespace string) *corev1.Secret {
return Secret(
clusterName,
namespace,
map[string][]byte{
"elasticsearch.key": GetFileContents("test/files/elasticsearch.key"),
"elasticsearch.crt": GetFileContents("test/files/elasticsearch.crt"),
"logging-es.key": GetFileContents("test/files/logging-es.key"),
"logging-es.crt": GetFileContents("test/files/logging-es.crt"),
"admin-key": GetFileContents("test/files/system.admin.key"),
"admin-cert": GetFileContents("test/files/system.admin.crt"),
"admin-ca": GetFileContents("test/files/ca.crt"),
},
)
}
func parseString(path string, interfaceMap map[string]interface{}) string {
value := walkInterfaceMap(path, interfaceMap)
if parsedString, ok := value.(string); ok {
return parsedString
} else {
return ""
}
}
func walkInterfaceMap(path string, interfaceMap map[string]interface{}) interface{} {
current := interfaceMap
keys := strings.Split(path, ".")
keyCount := len(keys)
for index, key := range keys {
if current[key] != nil {
if index+1 < keyCount {
current = current[key].(map[string]interface{})
} else {
return current[key]
}
} else {
return nil
}
}
return nil
}