Files
docker-cli/cli/command/stack/list_utils.go
Sebastiaan van Stijn 6647e229be cli/command/stack: move GetStacks and StackWrite internal
These were deprecated in 036d3a6bab and
30774ed1f2, and were originally in the
cli/command/stack package, but moved for the (now deprecated) Compose
on Kubernetes feature in 4d947de292.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-09-01 09:40:44 +02:00

37 lines
926 B
Go

package stack
import (
"context"
"github.com/docker/cli/cli/compose/convert"
"github.com/moby/moby/client"
"github.com/pkg/errors"
)
// getStacks lists the swarm stacks with the number of services they contain.
func getStacks(ctx context.Context, apiClient client.ServiceAPIClient) ([]stackSummary, error) {
services, err := apiClient.ServiceList(ctx, client.ServiceListOptions{
Filters: getAllStacksFilter(),
})
if err != nil {
return nil, err
}
idx := make(map[string]int, len(services))
out := make([]stackSummary, 0, len(services))
for _, svc := range services {
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
}