forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
439 lines (407 loc) · 14.1 KB
/
util.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
package deployments
import (
"fmt"
"sort"
"strings"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apimachinery/pkg/watch"
kapi "k8s.io/kubernetes/pkg/api"
kapiv1 "k8s.io/kubernetes/pkg/api/v1"
e2e "k8s.io/kubernetes/test/e2e/framework"
deployapi "github.com/openshift/origin/pkg/deploy/api"
deployutil "github.com/openshift/origin/pkg/deploy/util"
exutil "github.com/openshift/origin/test/extended/util"
)
func deploymentStatuses(rcs []kapi.ReplicationController) []string {
statuses := []string{}
for _, rc := range rcs {
statuses = append(statuses, string(deployutil.DeploymentStatusFor(&rc)))
}
return statuses
}
func deploymentPods(pods []kapiv1.Pod) (map[string][]*kapiv1.Pod, error) {
deployers := make(map[string][]*kapiv1.Pod)
for i := range pods {
name, ok := pods[i].Labels[deployapi.DeployerPodForDeploymentLabel]
if !ok {
continue
}
deployers[name] = append(deployers[name], &pods[i])
}
return deployers, nil
}
var completedStatuses = sets.NewString(string(deployapi.DeploymentStatusComplete), string(deployapi.DeploymentStatusFailed))
func checkDeployerPodInvariants(deploymentName string, pods []*kapiv1.Pod) (isRunning, isCompleted bool, err error) {
running := false
completed := false
succeeded := false
hasDeployer := false
// find deployment state
for _, pod := range pods {
switch {
case strings.HasSuffix(pod.Name, "-deploy"):
if hasDeployer {
return false, false, fmt.Errorf("multiple deployer pods for %q", deploymentName)
}
hasDeployer = true
switch pod.Status.Phase {
case kapiv1.PodSucceeded:
succeeded = true
completed = true
case kapiv1.PodFailed:
completed = true
default:
running = true
}
case strings.HasSuffix(pod.Name, "-pre"), strings.HasSuffix(pod.Name, "-mid"), strings.HasSuffix(pod.Name, "-post"):
default:
return false, false, fmt.Errorf("deployer pod %q not recognized as being a valid deployment pod", pod.Name)
}
}
// check hook pods
for _, pod := range pods {
switch {
case strings.HasSuffix(pod.Name, "-pre"), strings.HasSuffix(pod.Name, "-mid"), strings.HasSuffix(pod.Name, "-post"):
switch pod.Status.Phase {
case kapiv1.PodSucceeded:
case kapiv1.PodFailed:
if succeeded {
return false, false, fmt.Errorf("deployer hook pod %q failed but the deployment %q pod succeeded", pod.Name, deploymentName)
}
default:
if completed {
// TODO: we need to tighten guarantees around hook pods: https://github.com/openshift/origin/issues/8500
//for i := range pods {
// e2e.Logf("deployment %q pod[%d]: %#v", deploymentName, i, pods[i])
//}
//return false, false, fmt.Errorf("deployer hook pod %q is still running but the deployment %q is complete", pod.Name, deploymentName)
//e2e.Logf("deployer hook pod %q is still running but the deployment %q is complete", pod.Name, deploymentName)
}
}
}
}
return running, completed, nil
}
func checkDeploymentInvariants(dc *deployapi.DeploymentConfig, rcs []*kapiv1.ReplicationController, pods []kapiv1.Pod) error {
deployers, err := deploymentPods(pods)
if err != nil {
return err
}
if len(deployers) > len(rcs) {
existing := sets.NewString()
for k := range deployers {
existing.Insert(k)
}
for _, rc := range rcs {
if existing.Has(rc.Name) {
existing.Delete(rc.Name)
} else {
e2e.Logf("ANOMALY: No deployer pod found for deployment %q", rc.Name)
}
}
for k := range existing {
// TODO: we are missing RCs? https://github.com/openshift/origin/pull/8483#issuecomment-209150611
e2e.Logf("ANOMALY: Deployer pod found for %q but no RC exists", k)
//return fmt.Errorf("more deployer pods found than deployments: %#v %#v", deployers, rcs)
}
}
running := sets.NewString()
completed := 0
for k, v := range deployers {
isRunning, isCompleted, err := checkDeployerPodInvariants(k, v)
if err != nil {
return err
}
if isCompleted {
completed++
}
if isRunning {
running.Insert(k)
}
}
if running.Len() > 1 {
return fmt.Errorf("found multiple running deployments: %v", running.List())
}
sawStatus := sets.NewString()
statuses := []string{}
for _, rc := range rcs {
status := deployutil.DeploymentStatusFor(rc)
if sawStatus.Len() != 0 {
switch status {
case deployapi.DeploymentStatusComplete, deployapi.DeploymentStatusFailed:
if sawStatus.Difference(completedStatuses).Len() != 0 {
return fmt.Errorf("rc %s was %s, but earlier RCs were not completed: %v", rc.Name, status, statuses)
}
case deployapi.DeploymentStatusRunning, deployapi.DeploymentStatusPending:
if sawStatus.Has(string(status)) {
return fmt.Errorf("rc %s was %s, but so was an earlier RC: %v", rc.Name, status, statuses)
}
if sawStatus.Difference(completedStatuses).Len() != 0 {
return fmt.Errorf("rc %s was %s, but earlier RCs were not completed: %v", rc.Name, status, statuses)
}
case deployapi.DeploymentStatusNew:
default:
return fmt.Errorf("rc %s has unexpected status %s: %v", rc.Name, status, statuses)
}
}
sawStatus.Insert(string(status))
statuses = append(statuses, string(status))
}
return nil
}
func deploymentReachedCompletion(dc *deployapi.DeploymentConfig, rcs []*kapiv1.ReplicationController, pods []kapiv1.Pod) (bool, error) {
if len(rcs) == 0 {
return false, nil
}
rcv1 := rcs[len(rcs)-1]
rc := &kapi.ReplicationController{}
kapiv1.Convert_v1_ReplicationController_To_api_ReplicationController(rcv1, rc, nil)
version := deployutil.DeploymentVersionFor(rc)
if version != dc.Status.LatestVersion {
return false, nil
}
if !deployutil.IsCompleteDeployment(rc) {
return false, nil
}
cond := deployutil.GetDeploymentCondition(dc.Status, deployapi.DeploymentProgressing)
if cond == nil || cond.Reason != deployapi.NewRcAvailableReason {
return false, nil
}
expectedReplicas := dc.Spec.Replicas
if dc.Spec.Test {
expectedReplicas = 0
}
if rc.Spec.Replicas != int32(expectedReplicas) {
return false, fmt.Errorf("deployment is complete but doesn't have expected spec replicas: %d %d", rc.Spec.Replicas, expectedReplicas)
}
if rc.Status.Replicas != int32(expectedReplicas) {
e2e.Logf("POSSIBLE_ANOMALY: deployment is complete but doesn't have expected status replicas: %d %d", rc.Status.Replicas, expectedReplicas)
return false, nil
}
e2e.Logf("Latest rollout of dc/%s (rc/%s) is complete.", dc.Name, rc.Name)
return true, nil
}
func deploymentFailed(dc *deployapi.DeploymentConfig, rcs []*kapiv1.ReplicationController, _ []kapiv1.Pod) (bool, error) {
if len(rcs) == 0 {
return false, nil
}
rcv1 := rcs[len(rcs)-1]
rc := &kapi.ReplicationController{}
kapiv1.Convert_v1_ReplicationController_To_api_ReplicationController(rcv1, rc, nil)
version := deployutil.DeploymentVersionFor(rc)
if version != dc.Status.LatestVersion {
return false, nil
}
if !deployutil.IsFailedDeployment(rc) {
return false, nil
}
cond := deployutil.GetDeploymentCondition(dc.Status, deployapi.DeploymentProgressing)
return cond != nil && cond.Reason == deployapi.TimedOutReason, nil
}
func deploymentRunning(dc *deployapi.DeploymentConfig, rcs []*kapiv1.ReplicationController, pods []kapiv1.Pod) (bool, error) {
if len(rcs) == 0 {
return false, nil
}
rcv1 := rcs[len(rcs)-1]
rc := &kapi.ReplicationController{}
kapiv1.Convert_v1_ReplicationController_To_api_ReplicationController(rcv1, rc, nil)
version := deployutil.DeploymentVersionFor(rc)
if version != dc.Status.LatestVersion {
//e2e.Logf("deployment %s is not the latest version on DC: %d", rc.Name, version)
return false, nil
}
status := rc.Annotations[deployapi.DeploymentStatusAnnotation]
switch deployapi.DeploymentStatus(status) {
case deployapi.DeploymentStatusFailed:
if deployutil.IsDeploymentCancelled(rc) {
return true, nil
}
reason := deployutil.DeploymentStatusReasonFor(rc)
if reason == "deployer pod no longer exists" {
return true, nil
}
return false, fmt.Errorf("deployment failed: %v", deployutil.DeploymentStatusReasonFor(rc))
case deployapi.DeploymentStatusRunning, deployapi.DeploymentStatusComplete:
return true, nil
default:
return false, nil
}
}
func deploymentPreHookRetried(dc *deployapi.DeploymentConfig, rcs []*kapiv1.ReplicationController, pods []kapiv1.Pod) (bool, error) {
var preHook *kapiv1.Pod
for i := range pods {
pod := pods[i]
if !strings.HasSuffix(pod.Name, "-pre") {
continue
}
preHook = &pod
break
}
if preHook == nil || len(preHook.Status.ContainerStatuses) == 0 {
return false, nil
}
return preHook.Status.ContainerStatuses[0].RestartCount > 0, nil
}
func deploymentImageTriggersResolved(expectTriggers int) func(dc *deployapi.DeploymentConfig, rcs []*kapiv1.ReplicationController, pods []kapiv1.Pod) (bool, error) {
return func(dc *deployapi.DeploymentConfig, rcs []*kapiv1.ReplicationController, pods []kapiv1.Pod) (bool, error) {
expect := 0
for _, t := range dc.Spec.Triggers {
if t.Type != deployapi.DeploymentTriggerOnImageChange {
continue
}
if expect >= expectTriggers {
return false, fmt.Errorf("dc %s had too many image change triggers: %#v", dc.Name, dc.Spec.Triggers)
}
if t.ImageChangeParams == nil {
return false, nil
}
if len(t.ImageChangeParams.LastTriggeredImage) == 0 {
return false, nil
}
expect++
}
return expect == expectTriggers, nil
}
}
func deploymentInfo(oc *exutil.CLI, name string) (*deployapi.DeploymentConfig, []*kapiv1.ReplicationController, []kapiv1.Pod, error) {
dc, err := oc.Client().DeploymentConfigs(oc.Namespace()).Get(name, metav1.GetOptions{})
if err != nil {
return nil, nil, nil, err
}
// get pods before RCs, so we see more RCs than pods.
pods, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).List(metav1.ListOptions{})
if err != nil {
return nil, nil, nil, err
}
rcs, err := oc.KubeClient().CoreV1().ReplicationControllers(oc.Namespace()).List(metav1.ListOptions{
LabelSelector: deployutil.ConfigSelector(name).String(),
})
if err != nil {
return nil, nil, nil, err
}
deployments := make([]*kapiv1.ReplicationController, 0, len(rcs.Items))
for i := range rcs.Items {
deployments = append(deployments, &rcs.Items[i])
}
sort.Sort(deployutil.ByLatestVersionAscV1(deployments))
return dc, deployments, pods.Items, nil
}
type deploymentConditionFunc func(dc *deployapi.DeploymentConfig, rcs []*kapiv1.ReplicationController, pods []kapiv1.Pod) (bool, error)
func waitForLatestCondition(oc *exutil.CLI, name string, timeout time.Duration, fn deploymentConditionFunc) error {
return wait.PollImmediate(200*time.Millisecond, timeout, func() (bool, error) {
dc, rcs, pods, err := deploymentInfo(oc, name)
if err != nil {
return false, err
}
if err := checkDeploymentInvariants(dc, rcs, pods); err != nil {
return false, err
}
return fn(dc, rcs, pods)
})
}
func waitForSyncedConfig(oc *exutil.CLI, name string, timeout time.Duration) error {
dc, rcs, pods, err := deploymentInfo(oc, name)
if err != nil {
return err
}
if err := checkDeploymentInvariants(dc, rcs, pods); err != nil {
return err
}
generation := dc.Generation
return wait.PollImmediate(200*time.Millisecond, timeout, func() (bool, error) {
config, err := oc.Client().DeploymentConfigs(oc.Namespace()).Get(name, metav1.GetOptions{})
if err != nil {
return false, err
}
return deployutil.HasSynced(config, generation), nil
})
}
// waitForDeployerToComplete waits till the replication controller is created for a given
// rollout and then wait till the deployer pod finish. Then scrubs the deployer logs and
// return it.
func waitForDeployerToComplete(oc *exutil.CLI, name string, timeout time.Duration) (string, error) {
watcher, err := oc.InternalKubeClient().Core().ReplicationControllers(oc.Namespace()).Watch(metav1.ListOptions{FieldSelector: fields.Everything().String()})
if err != nil {
return "", err
}
defer watcher.Stop()
var rc *kapi.ReplicationController
if _, err := watch.Until(timeout, watcher, func(e watch.Event) (bool, error) {
if e.Type == watch.Error {
return false, fmt.Errorf("error while waiting for replication controller: %v", e.Object)
}
if e.Type == watch.Added || e.Type == watch.Modified {
if newRC, ok := e.Object.(*kapi.ReplicationController); ok && newRC.Name == name {
rc = newRC
return true, nil
}
}
return false, nil
}); err != nil {
return "", err
}
podName := deployutil.DeployerPodNameForDeployment(rc.Name)
if err := deployutil.WaitForRunningDeployerPod(oc.InternalKubeClient().Core(), rc, timeout); err != nil {
return "", err
}
output, err := oc.Run("logs").Args("-f", "pods/"+podName).Output()
if err != nil {
return "", err
}
return output, nil
}
// createFixture will create the provided fixture and return the resource and the
// name separately.
// TODO: Probably move to a more general location like test/extended/util/cli.go
func createFixture(oc *exutil.CLI, fixture string) (string, string, error) {
resource, err := oc.Run("create").Args("-f", fixture, "-o", "name").Output()
if err != nil {
return "", "", err
}
parts := strings.Split(resource, "/")
if len(parts) != 2 {
return "", "", fmt.Errorf("expected type/name syntax, got: %s", resource)
}
return resource, parts[1], nil
}
func failureTrap(oc *exutil.CLI, name string, failed bool) {
if !failed {
return
}
out, err := oc.Run("get").Args("dc/"+name, "-o", "yaml").Output()
if err != nil {
e2e.Logf("Error getting Deployment Config %s: %v", name, err)
return
}
e2e.Logf("\n%s\n", out)
_, rcs, pods, err := deploymentInfo(oc, name)
if err != nil {
e2e.Logf("Error getting deployment %s info: %v", name, err)
return
}
for _, r := range rcs {
out, err := oc.Run("get").Args("rc/"+r.Name, "-o", "yaml").Output()
if err != nil {
e2e.Logf("Error getting replication controller %s info: %v", r.Name, err)
return
}
e2e.Logf("\n%s\n", out)
}
p, _ := deploymentPods(pods)
for _, v := range p {
for _, pod := range v {
out, err := oc.Run("get").Args("pod/"+pod.Name, "-o", "yaml").Output()
if err != nil {
e2e.Logf("Error getting pod %s: %v", pod.Name, err)
return
}
e2e.Logf("\n%s\n", out)
out, _ = oc.Run("logs").Args("pod/"+pod.Name, "--timestamps=true").Output()
e2e.Logf("--- pod %s logs\n%s---\n", pod.Name, out)
}
}
}