/
state.go
97 lines (79 loc) · 2.35 KB
/
state.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
package dockercompose
import (
"fmt"
"time"
"github.com/docker/docker/api/types"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
"github.com/tilt-dev/tilt/internal/container"
)
func (evt Event) IsStartupEvent() bool {
if evt.Type != TypeContainer {
return false
}
return evt.Action == ActionStart || evt.Action == ActionRestart || evt.Action == ActionUpdate
}
type State struct {
ContainerState types.ContainerState
ContainerID container.ID
// TODO(nick): It might make since to get rid of StartTime
// and parse it out of the ContainerState.StartedAt string,
// though we would have to decide how to treat containers
// started before Tilt start.
StartTime time.Time
LastReadyTime time.Time
SpanID model.LogSpanID
}
func (State) RuntimeState() {}
func (s State) RuntimeStatus() v1alpha1.RuntimeStatus {
if s.ContainerState.Error != "" || s.ContainerState.ExitCode != 0 {
return v1alpha1.RuntimeStatusError
}
if s.ContainerState.Running ||
// Status strings taken from comments on:
// https://godoc.org/github.com/docker/docker/api/types#ContainerState
s.ContainerState.Status == "running" ||
s.ContainerState.Status == "exited" {
return v1alpha1.RuntimeStatusOK
}
if s.ContainerState.Status == "" {
return v1alpha1.RuntimeStatusUnknown
}
return v1alpha1.RuntimeStatusPending
}
func (s State) RuntimeStatusError() error {
status := s.RuntimeStatus()
if status != v1alpha1.RuntimeStatusError {
return nil
}
if s.ContainerState.Error != "" {
return fmt.Errorf("Container %s: %s", s.ContainerID, s.ContainerState.Error)
}
if s.ContainerState.ExitCode != 0 {
return fmt.Errorf("Container %s exited with %d", s.ContainerID, s.ContainerState.ExitCode)
}
return fmt.Errorf("Container %s error status: %s", s.ContainerID, s.ContainerState.Status)
}
func (s State) WithContainerState(state types.ContainerState) State {
s.ContainerState = state
return s
}
func (s State) WithSpanID(spanID model.LogSpanID) State {
s.SpanID = spanID
return s
}
func (s State) WithContainerID(cID container.ID) State {
s.ContainerID = cID
return s
}
func (s State) WithStartTime(time time.Time) State {
s.StartTime = time
return s
}
func (s State) WithLastReadyTime(time time.Time) State {
s.LastReadyTime = time
return s
}
func (s State) HasEverBeenReadyOrSucceeded() bool {
return !s.LastReadyTime.IsZero()
}