-
Notifications
You must be signed in to change notification settings - Fork 28
/
wait.go
59 lines (46 loc) · 1.43 KB
/
wait.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
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0
package e2e
import (
"testing"
"time"
"github.com/ghodss/yaml"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
)
func waitForSecret(t *testing.T, kubectl Kubectl, name string) string {
return waitForSecretInNs(t, kubectl, "", name)
}
func waitForSecretInNs(t *testing.T, kubectl Kubectl, nsName, name string) string {
var lastErr error
args := []string{"get", "secret", name, "-o", "yaml"}
noNs := false
if len(nsName) > 0 {
args = append(args, []string{"-n", nsName}...)
noNs = true
}
for i := 0; i < 30; i++ {
var out string
out, lastErr = kubectl.RunWithOpts(args, RunOpts{AllowError: true, NoNamespace: noNs})
if lastErr == nil {
return out
}
time.Sleep(time.Second)
}
t.Fatalf("Expected to find secret '%s' but did not: %s", name, lastErr)
panic("Unreachable")
}
func waitUntilSecretInNsPopulated(t *testing.T, kubectl Kubectl, nsName, name string, checkFun func(*corev1.Secret) bool) string {
var secret corev1.Secret
for i := 0; i < 10; i++ {
out := waitForSecretInNs(t, kubectl, nsName, name)
err := yaml.Unmarshal([]byte(out), &secret)
require.NoError(t, err)
if checkFun(&secret) {
return out
}
time.Sleep(time.Second)
}
t.Fatalf("Timed out before Secret '%s' satisfied condition in Namespace '%s'; current data: %s", name, nsName, string(secret.Data[".dockerconfigjson"]))
panic("Unreachable")
}