-
Notifications
You must be signed in to change notification settings - Fork 288
/
container.go
140 lines (117 loc) · 3.53 KB
/
container.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
package k8s
import (
"context"
"fmt"
"strings"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/logger"
"k8s.io/api/core/v1"
)
const ContainerIDPrefix = "docker://"
func WaitForContainerReady(ctx context.Context, client Client, pod *v1.Pod, ref reference.Named) (v1.ContainerStatus, error) {
cStatus, err := waitForContainerReadyHelper(pod, ref)
if err != nil {
return v1.ContainerStatus{}, err
} else if cStatus != (v1.ContainerStatus{}) {
return cStatus, nil
}
watch, err := client.WatchPod(ctx, pod)
if err != nil {
return v1.ContainerStatus{}, errors.Wrap(err, "WaitForContainerReady")
}
defer watch.Stop()
for true {
select {
case <-ctx.Done():
return v1.ContainerStatus{}, errors.Wrap(ctx.Err(), "WaitForContainerReady")
case event, ok := <-watch.ResultChan():
if !ok {
return v1.ContainerStatus{}, fmt.Errorf("Container watch closed: %s", ref)
}
obj := event.Object
pod, ok := obj.(*v1.Pod)
if !ok {
logger.Get(ctx).Debugf("Unexpected watch notification: %T", obj)
continue
}
cStatus, err := waitForContainerReadyHelper(pod, ref)
if err != nil {
return v1.ContainerStatus{}, err
} else if cStatus != (v1.ContainerStatus{}) {
return cStatus, nil
}
}
}
panic("WaitForContainerReady") // should never reach this state
}
func waitForContainerReadyHelper(pod *v1.Pod, ref reference.Named) (v1.ContainerStatus, error) {
cStatus, err := ContainerMatching(pod, ref)
if err != nil {
return v1.ContainerStatus{}, errors.Wrap(err, "WaitForContainerReadyHelper")
}
unschedulable, msg := IsUnschedulable(pod.Status)
if unschedulable {
return v1.ContainerStatus{}, fmt.Errorf("Container will never be ready: %s", msg)
}
if IsContainerExited(pod.Status, cStatus) {
return v1.ContainerStatus{}, fmt.Errorf("Container will never be ready: %s", ref)
}
if !cStatus.Ready {
return v1.ContainerStatus{}, nil
}
return cStatus, nil
}
// If true, this means the container is gone and will never recover.
func IsContainerExited(pod v1.PodStatus, container v1.ContainerStatus) bool {
if pod.Phase == v1.PodSucceeded || pod.Phase == v1.PodFailed {
return true
}
if container.State.Terminated != nil {
return true
}
return false
}
// Returns the error message if the pod is unschedulable
func IsUnschedulable(pod v1.PodStatus) (bool, string) {
for _, cond := range pod.Conditions {
if cond.Reason == v1.PodReasonUnschedulable {
return true, cond.Message
}
}
return false, ""
}
func ContainerMatching(pod *v1.Pod, ref reference.Named) (v1.ContainerStatus, error) {
for _, c := range pod.Status.ContainerStatuses {
cRef, err := reference.ParseNormalizedNamed(c.Image)
if err != nil {
return v1.ContainerStatus{}, errors.Wrap(err, "ContainerMatching")
}
if cRef.Name() == ref.Name() {
return c, nil
}
}
return v1.ContainerStatus{}, nil
}
func ContainerIDFromContainerStatus(status v1.ContainerStatus) (container.ID, error) {
id := status.ContainerID
if id == "" {
return "", nil
}
if !strings.HasPrefix(id, ContainerIDPrefix) {
return "", fmt.Errorf("Malformed container ID: %s", id)
}
return container.ID(id[len(ContainerIDPrefix):]), nil
}
func ContainerNameFromContainerStatus(status v1.ContainerStatus) container.Name {
return container.Name(status.Name)
}
func ContainerSpecOf(pod *v1.Pod, status v1.ContainerStatus) v1.Container {
for _, spec := range pod.Spec.Containers {
if spec.Name == status.Name {
return spec
}
}
return v1.Container{}
}