/
resource.go
120 lines (109 loc) · 3.44 KB
/
resource.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
package liveupdate
import (
"time"
"github.com/tilt-dev/tilt/internal/controllers/apis/liveupdate"
"github.com/tilt-dev/tilt/internal/dockercompose"
"github.com/tilt-dev/tilt/internal/store/k8sconv"
"github.com/tilt-dev/tilt/pkg/apis"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
)
// Helper interface for live-updating different kinds of resources.
//
// This interface uses the language "pods", even though only Kubernetes
// has pods. For other kinds of orchestrator, just use whatever kind
// of workload primitive fits best.
type luResource interface {
// The time to start syncing changes from.
bestStartTime() time.Time
// An iterator for visiting each container.
visitSelectedContainers(visit func(pod v1alpha1.Pod, c v1alpha1.Container) bool)
}
type luK8sResource struct {
selector *v1alpha1.LiveUpdateKubernetesSelector
res *k8sconv.KubernetesResource
im *v1alpha1.ImageMap
}
func (r *luK8sResource) bestStartTime() time.Time {
if r.res.ApplyStatus != nil {
return r.res.ApplyStatus.LastApplyStartTime.Time
}
startTime := time.Time{}
for _, pod := range r.res.FilteredPods {
if startTime.IsZero() || (!pod.CreatedAt.IsZero() && pod.CreatedAt.Time.Before(startTime)) {
startTime = pod.CreatedAt.Time
}
}
return startTime
}
// Visit all selected containers.
func (r *luK8sResource) visitSelectedContainers(
visit func(pod v1alpha1.Pod, c v1alpha1.Container) bool) {
for _, pod := range r.res.FilteredPods {
for _, c := range pod.Containers {
if c.Name == "" {
// ignore any blatantly invalid containers
continue
}
if !liveupdate.KubernetesSelectorMatchesContainer(c, r.selector, r.im) {
continue
}
stop := visit(pod, c)
if stop {
return
}
}
}
}
// We model the DockerCompose resource as a single-container pod with a
// name equal to the container id.
type luDCResource struct {
selector *v1alpha1.LiveUpdateDockerComposeSelector
res *v1alpha1.DockerComposeService
}
func (r *luDCResource) bestStartTime() time.Time {
return r.res.Status.LastApplyStartTime.Time
}
// Visit all selected containers.
func (r *luDCResource) visitSelectedContainers(
visit func(pod v1alpha1.Pod, c v1alpha1.Container) bool) {
cID := r.res.Status.ContainerID
state := r.res.Status.ContainerState
if cID != "" && state != nil {
// In DockerCompose, we leave the pod empty.
pod := v1alpha1.Pod{}
var waiting *v1alpha1.ContainerStateWaiting
var running *v1alpha1.ContainerStateRunning
var terminated *v1alpha1.ContainerStateTerminated
switch state.Status {
case dockercompose.ContainerStatusCreated,
dockercompose.ContainerStatusPaused,
dockercompose.ContainerStatusRestarting:
waiting = &v1alpha1.ContainerStateWaiting{Reason: state.Status}
case dockercompose.ContainerStatusRunning:
running = &v1alpha1.ContainerStateRunning{
StartedAt: apis.NewTime(state.StartedAt.Time),
}
case dockercompose.ContainerStatusRemoving,
dockercompose.ContainerStatusExited,
dockercompose.ContainerStatusDead:
terminated = &v1alpha1.ContainerStateTerminated{
ExitCode: state.ExitCode,
Reason: state.Status,
StartedAt: apis.NewTime(state.StartedAt.Time),
FinishedAt: apis.NewTime(state.FinishedAt.Time),
}
}
cName := r.res.Status.ContainerName
c := v1alpha1.Container{
Name: cName,
ID: cID,
State: v1alpha1.ContainerState{
Waiting: waiting,
Running: running,
Terminated: terminated,
},
Ready: running != nil,
}
visit(pod, c)
}
}