/
containers_get.go
337 lines (276 loc) · 7.54 KB
/
containers_get.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"fmt"
"net/http"
"sort"
"strconv"
"sync"
"time"
"github.com/lxc/lxd/lxd/cluster"
"github.com/lxc/lxd/lxd/db"
"github.com/lxc/lxd/lxd/db/query"
"github.com/lxc/lxd/shared"
"github.com/lxc/lxd/shared/api"
"github.com/lxc/lxd/shared/logger"
"github.com/lxc/lxd/shared/version"
"github.com/pkg/errors"
)
func containersGet(d *Daemon, r *http.Request) Response {
for i := 0; i < 100; i++ {
result, err := doContainersGet(d, r)
if err == nil {
return SyncResponse(true, result)
}
if !query.IsRetriableError(err) {
logger.Debugf("DBERR: containersGet: error %q", err)
return SmartError(err)
}
// 1 s may seem drastic, but we really don't want to thrash
// perhaps we should use a random amount
time.Sleep(100 * time.Millisecond)
}
logger.Debugf("DBERR: containersGet, db is locked")
logger.Debugf(logger.GetStack())
return InternalError(fmt.Errorf("DB is locked"))
}
func doContainersGet(d *Daemon, r *http.Request) (interface{}, error) {
resultString := []string{}
resultList := []*api.Container{}
resultFullList := []*api.ContainerFull{}
resultMu := sync.Mutex{}
// Parse the recursion field
recursionStr := r.FormValue("recursion")
recursion, err := strconv.Atoi(recursionStr)
if err != nil {
recursion = 0
}
// Parse the project field
project := projectParam(r)
// Get the list and location of all containers
var result map[string][]string // Containers by node address
var nodes map[string]string // Node names by container
err = d.cluster.Transaction(func(tx *db.ClusterTx) error {
var err error
result, err = tx.ContainersListByNodeAddress(project)
if err != nil {
return err
}
nodes, err = tx.ContainersByNodeName(project)
if err != nil {
return err
}
return nil
})
if err != nil {
return []string{}, err
}
// Get the local containers
nodeCts := map[string]container{}
if recursion > 0 {
cts, err := containerLoadNodeProjectAll(d.State(), project)
if err != nil {
return nil, err
}
for _, ct := range cts {
nodeCts[ct.Name()] = ct
}
}
// Append containers to list and handle errors
resultListAppend := func(name string, c api.Container, err error) {
if err != nil {
c = api.Container{
Name: name,
Status: api.Error.String(),
StatusCode: api.Error,
Location: nodes[name],
}
}
resultMu.Lock()
resultList = append(resultList, &c)
resultMu.Unlock()
}
resultFullListAppend := func(name string, c api.ContainerFull, err error) {
if err != nil {
c = api.ContainerFull{Container: api.Container{
Name: name,
Status: api.Error.String(),
StatusCode: api.Error,
Location: nodes[name],
}}
}
resultMu.Lock()
resultFullList = append(resultFullList, &c)
resultMu.Unlock()
}
// Get the data
wg := sync.WaitGroup{}
for address, containers := range result {
// If this is an internal request from another cluster node,
// ignore containers from other nodes, and return only the ones
// on this node
if isClusterNotification(r) && address != "" {
continue
}
// Mark containers on unavailable nodes as down
if recursion > 0 && address == "0.0.0.0" {
for _, container := range containers {
if recursion == 1 {
resultListAppend(container, api.Container{}, fmt.Errorf("unavailable"))
} else {
resultFullListAppend(container, api.ContainerFull{}, fmt.Errorf("unavailable"))
}
}
continue
}
// For recursion requests we need to fetch the state of remote
// containers from their respective nodes.
if recursion > 0 && address != "" && !isClusterNotification(r) {
wg.Add(1)
go func(address string, containers []string) {
defer wg.Done()
cert := d.endpoints.NetworkCert()
if recursion == 1 {
cs, err := doContainersGetFromNode(project, address, cert)
if err != nil {
for _, name := range containers {
resultListAppend(name, api.Container{}, err)
}
return
}
for _, c := range cs {
resultListAppend(c.Name, c, nil)
}
return
}
cs, err := doContainersFullGetFromNode(project, address, cert)
if err != nil {
for _, name := range containers {
resultFullListAppend(name, api.ContainerFull{}, err)
}
return
}
for _, c := range cs {
resultFullListAppend(c.Name, c, nil)
}
}(address, containers)
continue
}
if recursion == 0 {
for _, container := range containers {
url := fmt.Sprintf("/%s/containers/%s", version.APIVersion, container)
resultString = append(resultString, url)
}
} else {
threads := 4
if len(containers) < threads {
threads = len(containers)
}
queue := make(chan string, threads)
for i := 0; i < threads; i++ {
wg.Add(1)
go func() {
for {
container, more := <-queue
if !more {
break
}
if recursion == 1 {
c, _, err := nodeCts[container].Render()
if err != nil {
resultListAppend(container, api.Container{}, err)
} else {
resultListAppend(container, *c.(*api.Container), err)
}
continue
}
c, _, err := nodeCts[container].RenderFull()
if err != nil {
resultFullListAppend(container, api.ContainerFull{}, err)
} else {
resultFullListAppend(container, *c, err)
}
}
wg.Done()
}()
}
for _, container := range containers {
queue <- container
}
close(queue)
}
}
wg.Wait()
if recursion == 0 {
return resultString, nil
}
if recursion == 1 {
// Sort the result list by name.
sort.Slice(resultList, func(i, j int) bool {
return resultList[i].Name < resultList[j].Name
})
return resultList, nil
}
// Sort the result list by name.
sort.Slice(resultFullList, func(i, j int) bool {
return resultFullList[i].Name < resultFullList[j].Name
})
return resultFullList, nil
}
// Fetch information about the containers on the given remote node, using the
// rest API and with a timeout of 30 seconds.
func doContainersGetFromNode(project, node string, cert *shared.CertInfo) ([]api.Container, error) {
f := func() ([]api.Container, error) {
client, err := cluster.Connect(node, cert, true)
if err != nil {
return nil, errors.Wrapf(err, "Failed to connect to node %s", node)
}
client = client.UseProject(project)
containers, err := client.GetContainers()
if err != nil {
return nil, errors.Wrapf(err, "Failed to get containers from node %s", node)
}
return containers, nil
}
timeout := time.After(30 * time.Second)
done := make(chan struct{})
var containers []api.Container
var err error
go func() {
containers, err = f()
done <- struct{}{}
}()
select {
case <-timeout:
err = fmt.Errorf("Timeout getting containers from node %s", node)
case <-done:
}
return containers, err
}
func doContainersFullGetFromNode(project, node string, cert *shared.CertInfo) ([]api.ContainerFull, error) {
f := func() ([]api.ContainerFull, error) {
client, err := cluster.Connect(node, cert, true)
if err != nil {
return nil, errors.Wrapf(err, "Failed to connect to node %s", node)
}
client = client.UseProject(project)
containers, err := client.GetContainersFull()
if err != nil {
return nil, errors.Wrapf(err, "Failed to get containers from node %s", node)
}
return containers, nil
}
timeout := time.After(30 * time.Second)
done := make(chan struct{})
var containers []api.ContainerFull
var err error
go func() {
containers, err = f()
done <- struct{}{}
}()
select {
case <-timeout:
err = fmt.Errorf("Timeout getting containers from node %s", node)
case <-done:
}
return containers, err
}