-
Notifications
You must be signed in to change notification settings - Fork 19
/
wait.go
164 lines (151 loc) · 4.54 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
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
package kube
import (
"log"
"strings"
"time"
"github.com/gogo/protobuf/proto"
. "github.com/onsi/gomega"
"github.com/rotisserie/eris"
"github.com/solo-io/go-utils/kubeutils"
corev1 "k8s.io/api/core/v1"
apiexts "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func WaitForNamespaceTeardown(ns string) {
WaitForNamespaceTeardownWithClient(ns, MustKubeClient())
}
func WaitForNamespaceTeardownWithClient(ns string, client kubernetes.Interface) {
EventuallyWithOffset(1, func() (bool, error) {
namespaces, err := client.CoreV1().Namespaces().List(v1.ListOptions{})
if err != nil {
// namespace is gone
return false, err
}
for _, n := range namespaces.Items {
if n.Name == ns {
return false, nil
}
}
return true, nil
}, time.Second*180).Should(BeTrue())
}
func TeardownSuperGloo(kube kubernetes.Interface) {
kube.CoreV1().Namespaces().Delete("supergloo-system", nil)
clusterroles, err := kube.RbacV1beta1().ClusterRoles().List(metav1.ListOptions{})
if err == nil {
for _, cr := range clusterroles.Items {
if strings.Contains(cr.Name, "supergloo") {
kube.RbacV1beta1().ClusterRoles().Delete(cr.Name, nil)
}
}
}
clusterrolebindings, err := kube.RbacV1beta1().ClusterRoleBindings().List(metav1.ListOptions{})
if err == nil {
for _, cr := range clusterrolebindings.Items {
if strings.Contains(cr.Name, "supergloo") {
kube.RbacV1beta1().ClusterRoleBindings().Delete(cr.Name, nil)
}
}
}
webhooks, err := kube.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().List(metav1.ListOptions{})
if err == nil {
for _, wh := range webhooks.Items {
if strings.Contains(wh.Name, "supergloo") {
kube.AdmissionregistrationV1beta1().MutatingWebhookConfigurations().Delete(wh.Name, nil)
}
}
}
cfg, err := kubeutils.GetConfig("", "")
Expect(err).NotTo(HaveOccurred())
exts, err := apiexts.NewForConfig(cfg)
Expect(err).NotTo(HaveOccurred())
crds, err := exts.ApiextensionsV1beta1().CustomResourceDefinitions().List(metav1.ListOptions{})
if err == nil {
for _, cr := range crds.Items {
if strings.Contains(cr.Name, "supergloo") {
exts.ApiextensionsV1beta1().CustomResourceDefinitions().Delete(cr.Name, nil)
}
}
}
}
// remove supergloo controller pod(s)
func DeleteSuperglooPods(kube kubernetes.Interface, superglooNamespace string) {
// wait until pod is gone
Eventually(func() error {
dep, err := kube.ExtensionsV1beta1().Deployments(superglooNamespace).Get("supergloo", metav1.GetOptions{})
if err != nil {
return err
}
dep.Spec.Replicas = proto.Int(0)
_, err = kube.ExtensionsV1beta1().Deployments(superglooNamespace).Update(dep)
if err != nil {
return err
}
pods, err := kube.CoreV1().Pods(superglooNamespace).List(metav1.ListOptions{})
if err != nil {
return err
}
for _, p := range pods.Items {
if strings.HasPrefix(p.Name, "supergloo") {
return eris.Errorf("supergloo pods still exist")
}
}
return nil
}, time.Second*60).ShouldNot(HaveOccurred())
}
func WaitUntilPodsRunning(timeout time.Duration, namespace string, podPrefixes ...string) error {
pods := MustKubeClient().CoreV1().Pods(namespace)
podsWithPrefixReady := func(prefix string) (bool, error) {
list, err := pods.List(metav1.ListOptions{})
if err != nil {
return false, err
}
var podsWithPrefix []corev1.Pod
for _, pod := range list.Items {
if strings.HasPrefix(pod.Name, prefix) {
podsWithPrefix = append(podsWithPrefix, pod)
}
}
if len(podsWithPrefix) == 0 {
return false, eris.Errorf("no pods found with prefix %v", prefix)
}
for _, pod := range podsWithPrefix {
var podReady bool
for _, cond := range pod.Status.Conditions {
if cond.Type == corev1.ContainersReady && cond.Status == corev1.ConditionTrue {
podReady = true
break
}
}
if !podReady {
return false, nil
}
}
return true, nil
}
failed := time.After(timeout)
notYetRunning := make(map[string]struct{})
for {
select {
case <-failed:
return eris.Errorf("timed out waiting for pods to come online: %v", notYetRunning)
case <-time.After(time.Second / 2):
notYetRunning = make(map[string]struct{})
for _, prefix := range podPrefixes {
ready, err := podsWithPrefixReady(prefix)
if err != nil {
log.Printf("failed to get pod status: %v", err)
notYetRunning[prefix] = struct{}{}
}
if !ready {
notYetRunning[prefix] = struct{}{}
}
}
if len(notYetRunning) == 0 {
return nil
}
}
}
}