/
waiters.go
314 lines (294 loc) · 10.6 KB
/
waiters.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
// Copyright 2020 The Kube-burner 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 burner
import (
"context"
"encoding/json"
"time"
log "github.com/sirupsen/logrus"
"golang.org/x/time/rate"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"github.com/cloud-bulldozer/kube-burner/pkg/burner/types"
)
func (ex *Executor) waitForObjects(ns string, limiter *rate.Limiter) {
for _, obj := range ex.objects {
if !obj.Wait {
continue
}
if obj.WaitOptions.ForCondition != "" {
if !obj.Namespaced {
ns = ""
}
waitForCondition(obj.gvr, ns, obj.WaitOptions.ForCondition, ex.MaxWaitTimeout, limiter)
} else {
switch obj.kind {
case "Deployment":
waitForDeployments(ns, ex.MaxWaitTimeout, limiter)
case "ReplicaSet":
waitForRS(ns, ex.MaxWaitTimeout, limiter)
case "ReplicationController":
waitForRC(ns, ex.MaxWaitTimeout, limiter)
case "StatefulSet":
waitForStatefulSet(ns, ex.MaxWaitTimeout, limiter)
case "DaemonSet":
waitForDS(ns, ex.MaxWaitTimeout, limiter)
case "Pod":
waitForPod(ns, ex.MaxWaitTimeout, limiter)
case "Build", "BuildConfig":
waitForBuild(ns, ex.MaxWaitTimeout, obj.Replicas, limiter)
case "VirtualMachine":
waitForVM(ns, ex.MaxWaitTimeout, limiter)
case "VirtualMachineInstance":
waitForVMI(ns, ex.MaxWaitTimeout, limiter)
case "VirtualMachineInstanceReplicaSet":
waitForVMIRS(ns, ex.MaxWaitTimeout, limiter)
case "Job":
waitForJob(ns, ex.MaxWaitTimeout, limiter)
case "PersistentVolumeClaim":
waitForPVC(ns, ex.MaxWaitTimeout, limiter)
}
}
}
log.Infof("Actions in namespace %v completed", ns)
}
func waitForDeployments(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
// TODO handle errors such as timeouts
wait.PollUntilContextTimeout(context.TODO(), time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
limiter.Wait(context.TODO())
deps, err := ClientSet.AppsV1().Deployments(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
for _, dep := range deps.Items {
if *dep.Spec.Replicas != dep.Status.ReadyReplicas {
log.Debugf("Waiting for replicas from deployments in ns %s to be ready", ns)
return false, nil
}
}
return true, nil
})
}
func waitForRS(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
wait.PollUntilContextTimeout(context.TODO(), time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
limiter.Wait(context.TODO())
rss, err := ClientSet.AppsV1().ReplicaSets(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
for _, rs := range rss.Items {
if *rs.Spec.Replicas != rs.Status.ReadyReplicas {
log.Debugf("Waiting for replicas from replicaSets in ns %s to be ready", ns)
return false, nil
}
}
return true, nil
})
}
func waitForStatefulSet(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
wait.PollUntilContextTimeout(context.TODO(), time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
limiter.Wait(context.TODO())
stss, err := ClientSet.AppsV1().StatefulSets(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
for _, sts := range stss.Items {
if *sts.Spec.Replicas != sts.Status.ReadyReplicas {
log.Debugf("Waiting for replicas from statefulSets in ns %s to be ready", ns)
return false, nil
}
}
return true, nil
})
}
func waitForPVC(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
wait.PollUntilContextTimeout(context.TODO(), time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
limiter.Wait(context.TODO())
pvc, err := ClientSet.CoreV1().PersistentVolumeClaims(ns).List(context.TODO(), metav1.ListOptions{FieldSelector: "status.phase!=Bound"})
if err != nil {
return false, err
}
return len(pvc.Items) == 0, nil
})
}
func waitForRC(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
wait.PollUntilContextTimeout(context.TODO(), time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
limiter.Wait(context.TODO())
rcs, err := ClientSet.CoreV1().ReplicationControllers(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
for _, rc := range rcs.Items {
if *rc.Spec.Replicas != rc.Status.ReadyReplicas {
log.Debugf("Waiting for replicas from replicationControllers in ns %s to be ready", ns)
return false, nil
}
}
return true, nil
})
}
func waitForDS(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
wait.PollUntilContextTimeout(context.TODO(), time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
limiter.Wait(context.TODO())
dss, err := ClientSet.AppsV1().DaemonSets(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
for _, ds := range dss.Items {
if ds.Status.DesiredNumberScheduled != ds.Status.NumberReady {
log.Debugf("Waiting for replicas from daemonsets in ns %s to be ready", ns)
return false, nil
}
}
return true, nil
})
}
func waitForPod(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
wait.PollUntilContextTimeout(context.TODO(), time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
limiter.Wait(context.TODO())
pods, err := ClientSet.CoreV1().Pods(ns).List(context.TODO(), metav1.ListOptions{FieldSelector: "status.phase!=Running"})
if err != nil {
return false, err
}
return len(pods.Items) == 0, nil
})
}
func waitForBuild(ns string, maxWaitTimeout time.Duration, expected int, limiter *rate.Limiter) {
buildStatus := []string{"New", "Pending", "Running"}
var build types.UnstructuredContent
gvr := schema.GroupVersionResource{
Group: types.OpenShiftBuildGroup,
Version: types.OpenShiftBuildAPIVersion,
Resource: types.OpenShiftBuildResource,
}
wait.PollUntilContextTimeout(context.TODO(), time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
limiter.Wait(context.TODO())
builds, err := DynamicClient.Resource(gvr).Namespace(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return false, err
}
if len(builds.Items) < expected {
log.Debugf("Waiting for Builds in ns %s to be completed", ns)
return false, err
}
for _, b := range builds.Items {
jsonBuild, err := b.MarshalJSON()
if err != nil {
log.Errorf("Error decoding Build object: %s", err)
}
_ = json.Unmarshal(jsonBuild, &build)
for _, bs := range buildStatus {
if build.Status.Phase == "" || build.Status.Phase == bs {
log.Debugf("Waiting for Builds in ns %s to be completed", ns)
return false, err
}
}
}
return true, nil
})
}
func waitForJob(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
gvr := schema.GroupVersionResource{
Group: "batch",
Version: "v1",
Resource: "jobs",
}
verifyCondition(gvr, ns, "Complete", maxWaitTimeout, limiter)
}
func waitForCondition(gvr schema.GroupVersionResource, ns, condition string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
verifyCondition(gvr, ns, condition, maxWaitTimeout, limiter)
}
func verifyCondition(gvr schema.GroupVersionResource, ns, condition string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
var uObj types.UnstructuredContent
wait.PollUntilContextTimeout(context.TODO(), 10*time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
var objs *unstructured.UnstructuredList
limiter.Wait(context.TODO())
if ns != "" {
objs, err = DynamicClient.Resource(gvr).Namespace(ns).List(context.TODO(), metav1.ListOptions{})
} else {
objs, err = DynamicClient.Resource(gvr).List(context.TODO(), metav1.ListOptions{})
}
if err != nil {
return false, err
}
VERIFY:
for _, obj := range objs.Items {
jsonBuild, err := obj.MarshalJSON()
if err != nil {
log.Errorf("Error decoding object: %s", err)
return false, err
}
_ = json.Unmarshal(jsonBuild, &uObj)
for _, c := range uObj.Status.Conditions {
if c.Status == "True" && c.Type == condition {
continue VERIFY
}
}
if ns != "" {
log.Debugf("Waiting for %s in ns %s to be ready", gvr.Resource, ns)
} else {
log.Debugf("Waiting for %s to be ready", gvr.Resource)
}
return false, err
}
return true, nil
})
}
func waitForVM(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
vmGVR := schema.GroupVersionResource{
Group: types.KubevirtGroup,
Version: types.KubevirtAPIVersion,
Resource: types.VirtualMachineResource,
}
verifyCondition(vmGVR, ns, "Ready", maxWaitTimeout, limiter)
}
func waitForVMI(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
vmiGVR := schema.GroupVersionResource{
Group: types.KubevirtGroup,
Version: types.KubevirtAPIVersion,
Resource: types.VirtualMachineInstanceResource,
}
verifyCondition(vmiGVR, ns, "Ready", maxWaitTimeout, limiter)
}
func waitForVMIRS(ns string, maxWaitTimeout time.Duration, limiter *rate.Limiter) {
var rs types.UnstructuredContent
vmiGVRRS := schema.GroupVersionResource{
Group: types.KubevirtGroup,
Version: types.KubevirtAPIVersion,
Resource: types.VirtualMachineInstanceReplicaSetResource,
}
wait.PollUntilContextTimeout(context.TODO(), 10*time.Second, maxWaitTimeout, true, func(ctx context.Context) (done bool, err error) {
limiter.Wait(context.TODO())
objs, err := DynamicClient.Resource(vmiGVRRS).Namespace(ns).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Debugf("VMIRS error %v", err)
return false, err
}
for _, obj := range objs.Items {
jsonBuild, err := obj.MarshalJSON()
if err != nil {
log.Errorf("Error decoding VMIRS object: %s", err)
return false, err
}
_ = json.Unmarshal(jsonBuild, &rs)
if rs.Spec.Replicas != rs.Status.ReadyReplicas {
log.Debugf("Waiting for replicas from VMIRS in ns %s to be running", ns)
return false, nil
}
}
return true, nil
})
}