@@ -21,6 +21,7 @@ import (
21
21
22
22
const (
23
23
defaultGetContainersCacheDuration = 10 * time .Second
24
+ dockerCreatedAtTimeFormat = "2006-01-02 15:04:05 -0700 MST"
24
25
getContainersTimeout = 5 * time .Second
25
26
)
26
27
@@ -56,6 +57,8 @@ func (ch *containersHandler) getContainers(ctx context.Context) ([]codersdk.Work
56
57
ch .cacheDuration = defaultGetContainersCacheDuration
57
58
}
58
59
if ch .cl == nil {
60
+ // TODO(cian): we may need some way to select the desired
61
+ // implementation, but for now there is only one.
59
62
ch .cl = & dockerCLIContainerLister {}
60
63
}
61
64
if ch .containers == nil {
@@ -94,6 +97,8 @@ type ContainerLister interface {
94
97
// dockerCLIContainerLister is a ContainerLister that lists containers using the docker CLI
95
98
type dockerCLIContainerLister struct {}
96
99
100
+ var _ ContainerLister = & dockerCLIContainerLister {}
101
+
97
102
type dockerCLIList struct {
98
103
CreatedAt string `json:"CreatedAt"`
99
104
ID string `json:"ID"`
@@ -107,7 +112,7 @@ func (*dockerCLIContainerLister) List(ctx context.Context) ([]codersdk.Workspace
107
112
cmd := exec .CommandContext (ctx , "docker" , "ps" , "--all" , "--no-trunc" , "--format=json" )
108
113
cmd .Stdout = & buf
109
114
if err := cmd .Run (); err != nil {
110
- return nil , xerrors .Errorf ("list containers : %w" , err )
115
+ return nil , xerrors .Errorf ("run docker ps : %w" , err )
111
116
}
112
117
113
118
// the output is returned with a single item per line, so we have to decode it
@@ -119,7 +124,7 @@ func (*dockerCLIContainerLister) List(ctx context.Context) ([]codersdk.Workspace
119
124
continue
120
125
}
121
126
if err := json .NewDecoder (strings .NewReader (line )).Decode (& tmp ); err != nil {
122
- return nil , xerrors .Errorf ("list containers : %w" , err )
127
+ return nil , xerrors .Errorf ("decode docker ps output : %w" , err )
123
128
}
124
129
out = append (out , convertDockerCLIList (tmp ))
125
130
}
@@ -134,17 +139,17 @@ func convertDockerCLIList(in dockerCLIList) codersdk.WorkspaceAgentContainer {
134
139
Labels : map [string ]string {},
135
140
}
136
141
137
- createdAt , err := time .Parse ("2006-01-02 15:04:05 -0700 MST" , in .CreatedAt )
142
+ createdAt , err := time .Parse (dockerCreatedAtTimeFormat , in .CreatedAt )
138
143
if err != nil {
139
- createdAt = time.Time {} // TODO: how to handle invalid createdAt?
144
+ createdAt = time .Unix ( 0 , 0 )
140
145
}
141
146
out .CreatedAt = createdAt
142
147
143
148
labels := strings .Split (in .Labels , "," )
144
149
for _ , label := range labels {
145
150
kvs := strings .Split (label , "=" )
146
151
if len (kvs ) != 2 {
147
- continue // TODO: how should we handle this weirdness?
152
+ continue // skip invalid labels
148
153
}
149
154
out .Labels [kvs [0 ]] = kvs [1 ]
150
155
}
0 commit comments