2017-11-20 09:30:52 -05:00
|
|
|
package swarm
|
2016-06-23 01:00:21 -04:00
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-06-23 01:00:21 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command"
|
2017-03-05 13:02:03 -05:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/compose/convert"
|
2016-06-23 01:00:21 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2017-03-09 13:23:45 -05:00
|
|
|
"github.com/pkg/errors"
|
2016-06-23 01:00:21 -04:00
|
|
|
)
|
|
|
|
|
2018-04-25 08:24:46 -04:00
|
|
|
// GetStacks lists the swarm stacks.
|
|
|
|
func GetStacks(dockerCli command.Cli) ([]*formatter.Stack, error) {
|
|
|
|
services, err := dockerCli.Client().ServiceList(
|
|
|
|
context.Background(),
|
2016-11-30 17:38:40 -05:00
|
|
|
types.ServiceListOptions{Filters: getAllStacksFilter()})
|
2016-06-23 01:00:21 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-09 17:42:16 -04:00
|
|
|
m := make(map[string]*formatter.Stack)
|
2016-06-23 01:00:21 -04:00
|
|
|
for _, service := range services {
|
|
|
|
labels := service.Spec.Labels
|
2016-12-05 16:14:08 -05:00
|
|
|
name, ok := labels[convert.LabelNamespace]
|
2016-06-23 01:00:21 -04:00
|
|
|
if !ok {
|
2017-03-09 13:23:45 -05:00
|
|
|
return nil, errors.Errorf("cannot get label %s for service %s",
|
2016-12-05 16:14:08 -05:00
|
|
|
convert.LabelNamespace, service.ID)
|
2016-06-23 01:00:21 -04:00
|
|
|
}
|
|
|
|
ztack, ok := m[name]
|
|
|
|
if !ok {
|
2017-03-05 13:02:03 -05:00
|
|
|
m[name] = &formatter.Stack{
|
2018-03-27 10:38:47 -04:00
|
|
|
Name: name,
|
|
|
|
Services: 1,
|
|
|
|
Orchestrator: "Swarm",
|
2016-06-23 01:00:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ztack.Services++
|
|
|
|
}
|
|
|
|
}
|
2017-03-05 13:02:03 -05:00
|
|
|
var stacks []*formatter.Stack
|
2016-06-23 01:00:21 -04:00
|
|
|
for _, stack := range m {
|
|
|
|
stacks = append(stacks, stack)
|
|
|
|
}
|
|
|
|
return stacks, nil
|
|
|
|
}
|