generated from kedacore/github-template
-
Notifications
You must be signed in to change notification settings - Fork 97
/
helper.go
687 lines (560 loc) · 20.9 KB
/
helper.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
//go:build e2e
// +build e2e
package helper
import (
"bytes"
"context"
"fmt"
"io"
"math/rand"
"os"
"os/exec"
"regexp"
"strings"
"testing"
"text/template"
"time"
"github.com/kedacore/keda/v2/pkg/generated/clientset/versioned/typed/keda/v1alpha1"
"github.com/stretchr/testify/assert"
autoscalingv2 "k8s.io/api/autoscaling/v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"sigs.k8s.io/controller-runtime/pkg/client/config"
gatewayapiv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayapiv1clientset "sigs.k8s.io/gateway-api/pkg/client/clientset/versioned/typed/apis/v1"
)
const (
KEDANamespace = "keda"
ArgoRolloutsNamespace = "argo-rollouts"
ArgoRolloutsName = "argo-rollouts"
IngressNamespace = "ingress"
IngressReleaseName = "ingress"
EnvoyNamespace = "envoy-gateway-system"
EnvoyReleaseName = "eg"
StringFalse = "false"
StringTrue = "true"
)
var random = rand.New(rand.NewSource(time.Now().UnixNano()))
var (
KubeClient *kubernetes.Clientset
KedaKubeClient *v1alpha1.KedaV1alpha1Client
GWClient *gatewayapiv1clientset.GatewayV1Client
KubeConfig *rest.Config
)
type ExecutionError struct {
StdError []byte
}
func (ee ExecutionError) Error() string {
return string(ee.StdError)
}
func ParseCommand(cmdWithArgs string) *exec.Cmd {
quoted := false
splitCmd := strings.FieldsFunc(cmdWithArgs, func(r rune) bool {
if r == '\'' {
quoted = !quoted
}
return !quoted && r == ' '
})
for i, s := range splitCmd {
if strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'") {
splitCmd[i] = s[1 : len(s)-1]
}
}
return exec.Command(splitCmd[0], splitCmd[1:]...)
}
func ParseCommandWithDir(cmdWithArgs, dir string) *exec.Cmd {
cmd := ParseCommand(cmdWithArgs)
cmd.Dir = dir
return cmd
}
func ExecuteCommand(cmdWithArgs string) ([]byte, error) {
out, err := ParseCommand(cmdWithArgs).Output()
if err != nil {
exitError, ok := err.(*exec.ExitError)
if ok {
return out, ExecutionError{StdError: exitError.Stderr}
}
}
return out, err
}
func ExecuteCommandWithDir(cmdWithArgs, dir string) ([]byte, error) {
out, err := ParseCommandWithDir(cmdWithArgs, dir).Output()
if err != nil {
exitError, ok := err.(*exec.ExitError)
if ok {
return out, ExecutionError{StdError: exitError.Stderr}
}
}
return out, err
}
func ExecCommandOnSpecificPod(t *testing.T, podName string, namespace string, command string) (string, string, error) {
cmd := []string{
"sh",
"-c",
command,
}
buf := &bytes.Buffer{}
errBuf := &bytes.Buffer{}
request := KubeClient.CoreV1().RESTClient().Post().
Resource("pods").Name(podName).Namespace(namespace).
SubResource("exec").Timeout(time.Second*20).
VersionedParams(&corev1.PodExecOptions{
Command: cmd,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: true,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(KubeConfig, "POST", request.URL())
assert.NoErrorf(t, err, "cannot execute command - %s", err)
if err != nil {
return "", "", err
}
err = exec.StreamWithContext(context.Background(), remotecommand.StreamOptions{
Stdout: buf,
Stderr: errBuf,
})
out := buf.String()
errOut := errBuf.String()
return out, errOut, err
}
func WaitForSuccessfulExecCommandOnSpecificPod(t *testing.T, podName string, namespace string, command string, iterations, intervalSeconds int) (bool, string, string, error) {
var out, errOut string
var err error
for i := 0; i < iterations; i++ {
out, errOut, err = ExecCommandOnSpecificPod(t, podName, namespace, command)
t.Logf("Waiting for successful execution of command on Pod; Output: %s, Error: %s", out, errOut)
if err == nil {
return true, out, errOut, err
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return false, out, errOut, err
}
func GetKubernetesClient(t *testing.T) *kubernetes.Clientset {
if KubeClient != nil && KubeConfig != nil {
return KubeClient
}
var err error
KubeConfig, err = config.GetConfig()
assert.NoErrorf(t, err, "cannot fetch kube config file - %s", err)
KubeClient, err = kubernetes.NewForConfig(KubeConfig)
assert.NoErrorf(t, err, "cannot create kubernetes client - %s", err)
return KubeClient
}
func GetGatewayClient(t *testing.T) *gatewayapiv1clientset.GatewayV1Client {
if GWClient != nil && KubeConfig != nil {
return GWClient
}
var err error
KubeConfig, err = config.GetConfig()
assert.NoErrorf(t, err, "cannot fetch kube config file - %s", err)
GWClient, err = gatewayapiv1clientset.NewForConfig(KubeConfig)
assert.NoErrorf(t, err, "cannot create gateway client - %s", err)
return GWClient
}
func GetKedaKubernetesClient(t *testing.T) *v1alpha1.KedaV1alpha1Client {
if KedaKubeClient != nil && KubeConfig != nil {
return KedaKubeClient
}
var err error
KubeConfig, err = config.GetConfig()
assert.NoErrorf(t, err, "cannot fetch kube config file - %s", err)
KedaKubeClient, err = v1alpha1.NewForConfig(KubeConfig)
assert.NoErrorf(t, err, "cannot create keda kubernetes client - %s", err)
return KedaKubeClient
}
// Creates a new namespace. If it already exists, make sure it is deleted first.
func CreateNamespace(t *testing.T, kc *kubernetes.Clientset, nsName string) {
DeleteNamespace(t, nsName)
WaitForNamespaceDeletion(t, nsName)
t.Logf("Creating namespace - %s", nsName)
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: nsName,
Labels: map[string]string{"type": "e2e"},
},
}
_, err := kc.CoreV1().Namespaces().Create(context.Background(), namespace, metav1.CreateOptions{})
assert.NoErrorf(t, err, "cannot create kubernetes namespace - %s", err)
}
func DeleteNamespace(t *testing.T, nsName string) {
t.Logf("deleting namespace %s", nsName)
period := int64(0)
kc := GetKubernetesClient(t)
err := kc.CoreV1().Namespaces().Delete(context.Background(), nsName, metav1.DeleteOptions{
GracePeriodSeconds: &period,
})
if errors.IsNotFound(err) {
err = nil
}
assert.NoErrorf(t, err, "cannot delete kubernetes namespace - %s", err)
}
func WaitForJobSuccess(t *testing.T, kc *kubernetes.Clientset, jobName, namespace string, iterations, interval int) bool {
for i := 0; i < iterations; i++ {
job, err := kc.BatchV1().Jobs(namespace).Get(context.Background(), jobName, metav1.GetOptions{})
if err != nil {
t.Logf("cannot run job - %s", err)
}
if job.Status.Succeeded > 0 {
t.Logf("job %s ran successfully!", jobName)
return true // Job ran successfully
}
time.Sleep(time.Duration(interval) * time.Second)
}
return false
}
func WaitForAllJobsSuccess(t *testing.T, kc *kubernetes.Clientset, namespace string, iterations, interval int) bool {
for i := 0; i < iterations; i++ {
jobs, err := kc.BatchV1().Jobs(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
t.Logf("cannot list jobs - %s", err)
}
allJobsSuccess := true
for _, job := range jobs.Items {
if job.Status.Succeeded == 0 {
allJobsSuccess = false
break
}
}
if allJobsSuccess {
t.Logf("all jobs ran successfully!")
return true // Job ran successfully
}
time.Sleep(time.Duration(interval) * time.Second)
}
return false
}
func WaitForNamespaceDeletion(t *testing.T, nsName string) bool {
for i := 0; i < 120; i++ {
t.Logf("waiting for namespace %s deletion", nsName)
_, err := KubeClient.CoreV1().Namespaces().Get(context.Background(), nsName, metav1.GetOptions{})
if err != nil && errors.IsNotFound(err) {
return true
}
time.Sleep(time.Second * 5)
}
return false
}
func WaitForJobCount(t *testing.T, kc *kubernetes.Clientset, namespace string,
target, iterations, intervalSeconds int) bool {
for i := 0; i < iterations; i++ {
jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), metav1.ListOptions{})
count := len(jobList.Items)
t.Logf("Waiting for job count to hit target. Namespace - %s, Current - %d, Target - %d",
namespace, count, target)
if count == target {
return true
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return false
}
func WaitForJobCountUntilIteration(t *testing.T, kc *kubernetes.Clientset, namespace string,
target, iterations, intervalSeconds int) bool {
var isTargetAchieved = false
for i := 0; i < iterations; i++ {
jobList, _ := kc.BatchV1().Jobs(namespace).List(context.Background(), metav1.ListOptions{})
count := len(jobList.Items)
t.Logf("Waiting for job count to hit target. Namespace - %s, Current - %d, Target - %d",
namespace, count, target)
if count == target {
isTargetAchieved = true
} else {
isTargetAchieved = false
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return isTargetAchieved
}
// Waits until deployment count hits target or number of iterations are done.
func WaitForPodCountInNamespace(t *testing.T, kc *kubernetes.Clientset, namespace string,
target, iterations, intervalSeconds int) bool {
for i := 0; i < iterations; i++ {
pods, _ := kc.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{})
t.Logf("Waiting for pods in namespace to hit target. Namespace - %s, Current - %d, Target - %d",
namespace, len(pods.Items), target)
if len(pods.Items) == target {
return true
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return false
}
// Waits until all the pods in the namespace have a running status.
func WaitForAllPodRunningInNamespace(t *testing.T, kc *kubernetes.Clientset, namespace string, iterations, intervalSeconds int) bool {
for i := 0; i < iterations; i++ {
runningCount := 0
pods, _ := kc.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{})
for _, pod := range pods.Items {
if pod.Status.Phase != corev1.PodRunning {
break
}
runningCount++
}
t.Logf("Waiting for pods in namespace to be in 'Running' status. Namespace - %s, Current - %d, Target - %d",
namespace, runningCount, len(pods.Items))
if runningCount == len(pods.Items) {
return true
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return false
}
// Waits until deployment ready replica count hits target or number of iterations are done.
func WaitForDeploymentReplicaReadyCount(t *testing.T, kc *kubernetes.Clientset, name, namespace string,
target, iterations, intervalSeconds int) bool {
for i := 0; i < iterations; i++ {
deployment, _ := kc.AppsV1().Deployments(namespace).Get(context.Background(), name, metav1.GetOptions{})
replicas := deployment.Status.ReadyReplicas
t.Logf("Waiting for deployment replicas to hit target. Deployment - %s, Current - %d, Target - %d",
name, replicas, target)
if replicas == int32(target) {
return true
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return false
}
// Waits until ingress resource is ready or number of iterations are done.
func WaitForIngressReady(t *testing.T, kc *kubernetes.Clientset, name, namespace string,
iterations, intervalSeconds int) bool {
for i := 0; i < iterations; i++ {
ingress, _ := kc.NetworkingV1().Ingresses(namespace).Get(context.Background(), name, metav1.GetOptions{})
if ingress.Status.LoadBalancer.Ingress != nil {
return true
}
t.Log("Waiting for ready ingress")
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return false
}
func WaitForHTTPRouteAccepted(t *testing.T, gc *gatewayapiv1clientset.GatewayV1Client, name, namespace string, iterations, intervalSeconds int) bool {
for i := 0; i < iterations; i++ {
httpRoute, _ := gc.HTTPRoutes(namespace).Get(context.Background(), name, metav1.GetOptions{})
for _, parent := range httpRoute.Status.Parents {
for _, condition := range parent.Conditions {
if condition.Type == string(gatewayapiv1.RouteConditionAccepted) && condition.Status == metav1.ConditionTrue {
return true
}
}
}
t.Log("Waiting for accepted HTTPRoute")
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return false
}
// Waits until statefulset count hits target or number of iterations are done.
func WaitForStatefulsetReplicaReadyCount(t *testing.T, kc *kubernetes.Clientset, name, namespace string,
target, iterations, intervalSeconds int) bool {
for i := 0; i < iterations; i++ {
statefulset, _ := kc.AppsV1().StatefulSets(namespace).Get(context.Background(), name, metav1.GetOptions{})
replicas := statefulset.Status.ReadyReplicas
t.Logf("Waiting for statefulset replicas to hit target. Statefulset - %s, Current - %d, Target - %d",
name, replicas, target)
if replicas == int32(target) {
return true
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return false
}
// Waits for number of iterations and returns replica count.
func WaitForDeploymentReplicaCountChange(t *testing.T, kc *kubernetes.Clientset, name, namespace string, iterations, intervalSeconds int) int {
t.Log("Waiting for some time to see if deployment replica count changes")
var replicas, prevReplicas int32
prevReplicas = -1
for i := 0; i < iterations; i++ {
deployment, _ := kc.AppsV1().Deployments(namespace).Get(context.Background(), name, metav1.GetOptions{})
replicas = deployment.Status.Replicas
t.Logf("Deployment - %s, Current - %d", name, replicas)
if replicas != prevReplicas && prevReplicas != -1 {
break
}
prevReplicas = replicas
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return int(replicas)
}
// Waits some time to ensure that the replica count doesn't change.
func AssertReplicaCountNotChangeDuringTimePeriod(t *testing.T, kc *kubernetes.Clientset, name, namespace string, target, intervalSeconds int) {
t.Logf("Waiting for some time to ensure deployment replica count doesn't change from %d", target)
var replicas int32
for i := 0; i < intervalSeconds; i++ {
deployment, _ := kc.AppsV1().Deployments(namespace).Get(context.Background(), name, metav1.GetOptions{})
replicas = deployment.Status.Replicas
t.Logf("Deployment - %s, Current - %d", name, replicas)
if replicas != int32(target) {
assert.Fail(t, fmt.Sprintf("%s replica count has changed from %d to %d", name, target, replicas))
return
}
time.Sleep(time.Second)
}
}
func WaitForHpaCreation(t *testing.T, kc *kubernetes.Clientset, name, namespace string,
iterations, intervalSeconds int) (*autoscalingv2.HorizontalPodAutoscaler, error) {
hpa := &autoscalingv2.HorizontalPodAutoscaler{}
var err error
for i := 0; i < iterations; i++ {
hpa, err = kc.AutoscalingV2().HorizontalPodAutoscalers(namespace).Get(context.Background(), name, metav1.GetOptions{})
t.Log("Waiting for hpa creation")
if err == nil {
return hpa, err
}
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return hpa, err
}
func KubernetesScaleDeployment(t *testing.T, kc *kubernetes.Clientset, name string, desiredReplica int64, namespace string) {
scaleObject, _ := kc.AppsV1().Deployments(namespace).GetScale(context.TODO(), name, metav1.GetOptions{})
sc := *scaleObject
sc.Spec.Replicas = int32(desiredReplica)
us, err := kc.AppsV1().Deployments(namespace).UpdateScale(context.TODO(), name, &sc, metav1.UpdateOptions{})
if err != nil {
assert.NoErrorf(t, err, "couldn't scale the deployment: %v: %v", us.Name, err.Error())
}
}
type Template struct {
Name, Config string
}
func KubectlApplyWithTemplate(t *testing.T, data interface{}, templateName string, config string) {
t.Logf("Applying template: %s", templateName)
tmpl, err := template.New("kubernetes resource template").Parse(config)
assert.NoErrorf(t, err, "cannot parse template - %s", err)
tempFile, err := os.CreateTemp("", templateName)
assert.NoErrorf(t, err, "cannot create temp file - %s", err)
defer os.Remove(tempFile.Name())
err = tmpl.Execute(tempFile, data)
assert.NoErrorf(t, err, "cannot insert data into template - %s", err)
_, err = ExecuteCommand(fmt.Sprintf("kubectl apply -f %s", tempFile.Name()))
assert.NoErrorf(t, err, "cannot apply file - %s", err)
err = tempFile.Close()
assert.NoErrorf(t, err, "cannot close temp file - %s", err)
}
func KubectlApplyWithErrors(t *testing.T, data interface{}, templateName string, config string) error {
t.Logf("Applying template: %s", templateName)
tmpl, err := template.New("kubernetes resource template").Parse(config)
assert.NoErrorf(t, err, "cannot parse template - %s", err)
tempFile, err := os.CreateTemp("", templateName)
assert.NoErrorf(t, err, "cannot create temp file - %s", err)
if err != nil {
defer tempFile.Close()
defer os.Remove(tempFile.Name())
}
err = tmpl.Execute(tempFile, data)
assert.NoErrorf(t, err, "cannot insert data into template - %s", err)
_, err = ExecuteCommand(fmt.Sprintf("kubectl apply -f %s", tempFile.Name()))
return err
}
// Apply templates in order of slice
func KubectlApplyMultipleWithTemplate(t *testing.T, data interface{}, templates []Template) {
for _, tmpl := range templates {
KubectlApplyWithTemplate(t, data, tmpl.Name, tmpl.Config)
}
}
func KubectlDeleteWithTemplate(t *testing.T, data interface{}, templateName, config string) {
t.Logf("Deleting template: %s", templateName)
tmpl, err := template.New("kubernetes resource template").Parse(config)
assert.NoErrorf(t, err, "cannot parse template - %s", err)
tempFile, err := os.CreateTemp("", templateName)
assert.NoErrorf(t, err, "cannot delete temp file - %s", err)
defer os.Remove(tempFile.Name())
err = tmpl.Execute(tempFile, data)
assert.NoErrorf(t, err, "cannot insert data into template - %s", err)
_, err = ExecuteCommand(fmt.Sprintf("kubectl delete -f %s", tempFile.Name()))
assert.NoErrorf(t, err, "cannot apply file - %s", err)
err = tempFile.Close()
assert.NoErrorf(t, err, "cannot close temp file - %s", err)
}
// Delete templates in reverse order of slice
func KubectlDeleteMultipleWithTemplate(t *testing.T, data interface{}, templates []Template) {
for idx := len(templates) - 1; idx >= 0; idx-- {
tmpl := templates[idx]
KubectlDeleteWithTemplate(t, data, tmpl.Name, tmpl.Config)
}
}
func CreateKubernetesResources(t *testing.T, kc *kubernetes.Clientset, nsName string, data interface{}, templates []Template) {
CreateNamespace(t, kc, nsName)
KubectlApplyMultipleWithTemplate(t, data, templates)
}
func DeleteKubernetesResources(t *testing.T, nsName string, data interface{}, templates []Template) {
KubectlDeleteMultipleWithTemplate(t, data, templates)
DeleteNamespace(t, nsName)
deleted := WaitForNamespaceDeletion(t, nsName)
assert.Truef(t, deleted, "%s namespace not deleted", nsName)
}
func GetRandomNumber() int {
return random.Intn(10000)
}
func RemoveANSI(input string) string {
reg := regexp.MustCompile(`(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]`)
return reg.ReplaceAllString(input, "")
}
func FindPodLogs(kc *kubernetes.Clientset, namespace, label string) ([]string, error) {
var podLogs []string
pods, err := kc.CoreV1().Pods(namespace).List(context.TODO(),
metav1.ListOptions{LabelSelector: label})
if err != nil {
return []string{}, err
}
var podLogRequest *rest.Request
for _, v := range pods.Items {
podLogRequest = kc.CoreV1().Pods(namespace).GetLogs(v.Name, &corev1.PodLogOptions{})
stream, err := podLogRequest.Stream(context.TODO())
if err != nil {
return []string{}, err
}
defer stream.Close()
for {
buf := make([]byte, 2000)
numBytes, err := stream.Read(buf)
if err == io.EOF {
break
}
if numBytes == 0 {
continue
}
if err != nil {
return []string{}, err
}
podLogs = append(podLogs, string(buf[:numBytes]))
}
}
return podLogs, nil
}
// Delete all pods in namespace by selector
func DeletePodsInNamespaceBySelector(t *testing.T, kc *kubernetes.Clientset, selector, namespace string) {
t.Logf("killing all pods in %s namespace with selector %s", namespace, selector)
err := kc.CoreV1().Pods(namespace).DeleteCollection(context.Background(), metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: selector,
})
assert.NoErrorf(t, err, "cannot delete pods - %s", err)
}
// Wait for Pods identified by selector to complete termination
func WaitForPodsTerminated(t *testing.T, kc *kubernetes.Clientset, selector, namespace string,
iterations, intervalSeconds int) bool {
for i := 0; i < iterations; i++ {
pods, err := kc.CoreV1().Pods(namespace).List(context.Background(), metav1.ListOptions{LabelSelector: selector})
if (err != nil && errors.IsNotFound(err)) || len(pods.Items) == 0 {
t.Logf("No pods with label %s", selector)
return true
}
t.Logf("Waiting for pods with label %s to terminate", selector)
time.Sleep(time.Duration(intervalSeconds) * time.Second)
}
return false
}
// KubectlGetResult runs `kubectl get` with parameters
func KubectlGetResult(t *testing.T, kind string, name string, namespace string, otherparameter string) string {
time.Sleep(1 * time.Second) // wait a second for recource deployment finished
kctlGetCmd := fmt.Sprintf(`kubectl get %s/%s -n %s %s"`, kind, name, namespace, otherparameter)
t.Log("Running kubectl cmd:", kctlGetCmd)
output, err := ExecuteCommand(kctlGetCmd)
assert.NoErrorf(t, err, "cannot get rollout info - %s", err)
return strings.ReplaceAll(string(output), "\"", "")
}