forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
314 lines (285 loc) · 9.6 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
package deployments
import (
"fmt"
"sort"
"strings"
"time"
kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/util/sets"
"k8s.io/kubernetes/pkg/util/wait"
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 []kapi.Pod) (map[string][]*kapi.Pod, error) {
deployers := make(map[string][]*kapi.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 []*kapi.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 kapi.PodSucceeded:
succeeded = true
completed = true
case kapi.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 kapi.PodSucceeded:
case kapi.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 []kapi.ReplicationController, pods []kapi.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 []kapi.ReplicationController, pods []kapi.Pod) (bool, error) {
if len(rcs) == 0 {
return false, nil
}
rc := rcs[len(rcs)-1]
version := deployutil.DeploymentVersionFor(&rc)
if version != dc.Status.LatestVersion {
return false, nil
}
status := rc.Annotations[deployapi.DeploymentStatusAnnotation]
if deployapi.DeploymentStatus(status) != deployapi.DeploymentStatusComplete {
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
}
return true, nil
}
func deploymentRunning(dc *deployapi.DeploymentConfig, rcs []kapi.ReplicationController, pods []kapi.Pod) (bool, error) {
if len(rcs) == 0 {
return false, nil
}
rc := rcs[len(rcs)-1]
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 []kapi.ReplicationController, pods []kapi.Pod) (bool, error) {
var preHook *kapi.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 deploymentInfo(oc *exutil.CLI, name string) (*deployapi.DeploymentConfig, []kapi.ReplicationController, []kapi.Pod, error) {
dc, err := oc.REST().DeploymentConfigs(oc.Namespace()).Get(name)
if err != nil {
return nil, nil, nil, err
}
// get pods before RCs, so we see more RCs than pods.
pods, err := oc.KubeREST().Pods(oc.Namespace()).List(kapi.ListOptions{})
if err != nil {
return nil, nil, nil, err
}
rcs, err := oc.KubeREST().ReplicationControllers(oc.Namespace()).List(kapi.ListOptions{
LabelSelector: deployutil.ConfigSelector(name),
})
if err != nil {
return nil, nil, nil, err
}
sort.Sort(deployutil.ByLatestVersionAsc(rcs.Items))
return dc, rcs.Items, pods.Items, nil
}
type deploymentConditionFunc func(dc *deployapi.DeploymentConfig, rcs []kapi.ReplicationController, pods []kapi.Pod) (bool, error)
func waitForLatestCondition(oc *exutil.CLI, name string, timeout time.Duration, fn deploymentConditionFunc) error {
return wait.Poll(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)
})
}
// 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
}
dc, rcs, pods, err := deploymentInfo(oc, name)
if err != nil {
return
}
e2e.Logf("DC: %#v", dc)
e2e.Logf(" RCs: %#v", rcs)
p, _ := deploymentPods(pods)
for k, v := range p {
for _, pod := range v {
e2e.Logf(" Deployer: %s %#v", k, pod)
}
}
}
func checkDeploymentConfigHasSynced(dc *deployapi.DeploymentConfig, _ []kapi.ReplicationController, _ []kapi.Pod) (bool, error) {
return deployutil.HasSynced(dc), nil
}