-
Notifications
You must be signed in to change notification settings - Fork 288
/
container.go
175 lines (148 loc) · 4.46 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
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
package k8s
import (
"context"
"fmt"
"strings"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
"github.com/windmilleng/tilt/internal/container"
"github.com/windmilleng/tilt/internal/logger"
)
const ContainerIDPrefix = "docker://"
func WaitForContainerReady(ctx context.Context, client Client, pod *v1.Pod, ref container.RefSelector) (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 {
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
}
FixContainerStatusImages(pod)
cStatus, err := waitForContainerReadyHelper(pod, ref)
if err != nil {
return v1.ContainerStatus{}, err
} else if cStatus != (v1.ContainerStatus{}) {
return cStatus, nil
}
}
}
}
func waitForContainerReadyHelper(pod *v1.Pod, ref container.RefSelector) (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, ""
}
// Kubernetes has a bug where the image ref in the container status
// can be wrong (though this does not mean the container is running
// unexpected code)
//
// Repro steps:
// 1) Create an image and give it two different tags (A and B)
// 2) Deploy Pods with both A and B in the pod spec
// 3) The PodStatus will choose A or B for both pods.
//
// More details here:
// https://github.com/kubernetes/kubernetes/issues/51017
//
// For Tilt, it's pretty important that the image tag is correct (for matching
// purposes). To work around this bug, we change the image reference in
// ContainerStatus to match the ContainerSpec.
func FixContainerStatusImages(pod *v1.Pod) {
refsByContainerName := make(map[string]string)
for _, c := range pod.Spec.Containers {
if c.Name != "" {
refsByContainerName[c.Name] = c.Image
}
}
for i, cs := range pod.Status.ContainerStatuses {
image, ok := refsByContainerName[cs.Name]
if !ok {
continue
}
cs.Image = image
pod.Status.ContainerStatuses[i] = cs
}
}
func ContainerMatching(pod *v1.Pod, ref container.RefSelector) (v1.ContainerStatus, error) {
for _, c := range pod.Status.ContainerStatuses {
cRef, err := container.ParseNamed(c.Image)
if err != nil {
return v1.ContainerStatus{}, errors.Wrap(err, "ContainerMatching")
}
if ref.Matches(cRef) {
return c, nil
}
}
return v1.ContainerStatus{}, nil
}
func ContainerIDFromContainerStatus(status v1.ContainerStatus) (container.ID, error) {
id := status.ContainerID
if id == "" {
return "", nil
}
components := strings.SplitN(id, "://", 2)
if len(components) != 2 {
return "", fmt.Errorf("Malformed container ID: %s", id)
}
return container.ID(components[1]), 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{}
}