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"
|
2018-10-23 11:05:44 -04:00
|
|
|
"github.com/docker/cli/cli/command/stack/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.
|
2023-09-09 18:27:44 -04:00
|
|
|
func GetStacks(ctx context.Context, dockerCli command.Cli) ([]*formatter.Stack, error) {
|
2018-04-25 08:24:46 -04:00
|
|
|
services, err := dockerCli.Client().ServiceList(
|
2023-09-09 18:27:44 -04:00
|
|
|
ctx,
|
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{
|
2022-02-22 07:46:35 -05:00
|
|
|
Name: name,
|
|
|
|
Services: 1,
|
2016-06-23 01:00:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ztack.Services++
|
|
|
|
}
|
|
|
|
}
|
2022-09-03 14:07:29 -04:00
|
|
|
stacks := make([]*formatter.Stack, 0, len(m))
|
2016-06-23 01:00:21 -04:00
|
|
|
for _, stack := range m {
|
|
|
|
stacks = append(stacks, stack)
|
|
|
|
}
|
|
|
|
return stacks, nil
|
|
|
|
}
|