-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathlist_utils.go
More file actions
36 lines (31 loc) · 909 Bytes
/
list_utils.go
File metadata and controls
36 lines (31 loc) · 909 Bytes
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
package stack
import (
"context"
"errors"
"github.com/docker/cli/cli/compose/convert"
"github.com/moby/moby/client"
)
// getStacks lists the swarm stacks with the number of services they contain.
func getStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]stackSummary, error) {
res, err := apiClient.ServiceList(ctx, client.ServiceListOptions{
Filters: getAllStacksFilter(),
})
if err != nil {
return nil, err
}
idx := make(map[string]int, len(res.Items))
out := make([]stackSummary, 0, len(res.Items))
for _, svc := range res.Items {
name, ok := svc.Spec.Labels[convert.LabelNamespace]
if !ok {
return nil, errors.New("cannot get label " + convert.LabelNamespace + " for service " + svc.ID)
}
if i, ok := idx[name]; ok {
out[i].Services++
continue
}
idx[name] = len(out)
out = append(out, stackSummary{Name: name, Services: 1})
}
return out, nil
}