2016-09-08 13:11:39 -04:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli"
|
|
|
|
"github.com/docker/cli/cli/command"
|
2022-03-30 09:27:25 -04:00
|
|
|
"github.com/docker/cli/cli/command/completion"
|
2017-04-17 18:07:56 -04:00
|
|
|
"github.com/docker/cli/cli/command/formatter"
|
2021-03-09 18:45:56 -05:00
|
|
|
flagsHelper "github.com/docker/cli/cli/flags"
|
2017-05-15 08:45:19 -04:00
|
|
|
"github.com/docker/cli/opts"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
2019-10-23 18:16:35 -04:00
|
|
|
"github.com/docker/docker/client"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type listOptions struct {
|
|
|
|
quiet bool
|
2017-01-26 16:08:07 -05:00
|
|
|
format string
|
2016-09-08 13:11:39 -04:00
|
|
|
filter opts.FilterOpt
|
|
|
|
}
|
|
|
|
|
2017-07-19 03:34:03 -04:00
|
|
|
func newListCommand(dockerCli command.Cli) *cobra.Command {
|
2017-05-15 08:45:19 -04:00
|
|
|
options := listOptions{filter: opts.NewFilterOpt()}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
cmd := &cobra.Command{
|
|
|
|
Use: "ls [OPTIONS]",
|
|
|
|
Aliases: []string{"list"},
|
|
|
|
Short: "List services",
|
|
|
|
Args: cli.NoArgs,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2017-05-15 08:45:19 -04:00
|
|
|
return runList(dockerCli, options)
|
2016-09-08 13:11:39 -04:00
|
|
|
},
|
2022-03-30 09:27:25 -04:00
|
|
|
ValidArgsFunction: completion.NoComplete,
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only display IDs")
|
2021-03-09 18:45:56 -05:00
|
|
|
flags.StringVar(&options.format, "format", "", flagsHelper.FormatHelp)
|
2017-05-15 08:45:19 -04:00
|
|
|
flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:16:35 -04:00
|
|
|
func runList(dockerCli command.Cli, opts listOptions) error {
|
|
|
|
var (
|
|
|
|
apiClient = dockerCli.Client()
|
|
|
|
ctx = context.Background()
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
listOpts := types.ServiceListOptions{
|
|
|
|
Filters: opts.filter.Value(),
|
|
|
|
// When not running "quiet", also get service status (number of running
|
|
|
|
// and desired tasks). Note that this is only supported on API v1.41 and
|
|
|
|
// up; older API versions ignore this option, and we will have to collect
|
|
|
|
// the information manually below.
|
|
|
|
Status: !opts.quiet,
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2019-10-23 18:16:35 -04:00
|
|
|
services, err := apiClient.ServiceList(ctx, listOpts)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-23 18:16:35 -04:00
|
|
|
if listOpts.Status {
|
|
|
|
// Now that a request was made, we know what API version was used (either
|
|
|
|
// through configuration, or after client and daemon negotiated a version).
|
|
|
|
// If API version v1.41 or up was used; the daemon should already have done
|
|
|
|
// the legwork for us, and we don't have to calculate the number of desired
|
|
|
|
// and running tasks. On older API versions, we need to do some extra requests
|
|
|
|
// to get that information.
|
|
|
|
//
|
|
|
|
// So theoretically, this step can be skipped based on API version, however,
|
|
|
|
// some of our unit tests don't set the API version, and there may be other
|
|
|
|
// situations where the client uses the "default" version. To account for
|
|
|
|
// these situations, we do a quick check for services that do not have
|
|
|
|
// a ServiceStatus set, and perform a lookup for those.
|
|
|
|
services, err = AppendServiceStatus(ctx, apiClient, services)
|
2016-09-08 13:11:39 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-26 16:08:07 -05:00
|
|
|
}
|
|
|
|
|
2019-10-23 18:16:35 -04:00
|
|
|
format := opts.format
|
2017-01-26 16:08:07 -05:00
|
|
|
if len(format) == 0 {
|
2019-10-23 18:16:35 -04:00
|
|
|
if len(dockerCli.ConfigFile().ServicesFormat) > 0 && !opts.quiet {
|
2017-01-26 16:08:07 -05:00
|
|
|
format = dockerCli.ConfigFile().ServicesFormat
|
|
|
|
} else {
|
|
|
|
format = formatter.TableFormatKey
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-10-08 22:29:58 -04:00
|
|
|
|
2017-01-26 16:08:07 -05:00
|
|
|
servicesCtx := formatter.Context{
|
|
|
|
Output: dockerCli.Out(),
|
2019-10-23 18:16:35 -04:00
|
|
|
Format: NewListFormat(format, opts.quiet),
|
2017-01-26 16:08:07 -05:00
|
|
|
}
|
2019-10-23 18:16:35 -04:00
|
|
|
return ListFormatWrite(servicesCtx, services)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 18:16:35 -04:00
|
|
|
// AppendServiceStatus propagates the ServiceStatus field for "services".
|
|
|
|
//
|
|
|
|
// If API version v1.41 or up is used, this information is already set by the
|
|
|
|
// daemon. On older API versions, we need to do some extra requests to get
|
|
|
|
// that information. Theoretically, this function can be skipped based on API
|
|
|
|
// version, however, some of our unit tests don't set the API version, and
|
|
|
|
// there may be other situations where the client uses the "default" version.
|
|
|
|
// To take these situations into account, we do a quick check for services
|
|
|
|
// that don't have ServiceStatus set, and perform a lookup for those.
|
2022-07-13 06:29:49 -04:00
|
|
|
//
|
|
|
|
//nolint:gocyclo
|
2019-10-23 18:16:35 -04:00
|
|
|
func AppendServiceStatus(ctx context.Context, c client.APIClient, services []swarm.Service) ([]swarm.Service, error) {
|
|
|
|
status := map[string]*swarm.ServiceStatus{}
|
|
|
|
taskFilter := filters.NewArgs()
|
|
|
|
for i, s := range services {
|
2020-01-09 13:17:43 -05:00
|
|
|
// there is no need in this switch to check for job modes. jobs are not
|
|
|
|
// supported until after ServiceStatus was introduced.
|
2019-10-23 18:16:35 -04:00
|
|
|
switch {
|
|
|
|
case s.ServiceStatus != nil:
|
|
|
|
// Server already returned service-status, so we don't
|
|
|
|
// have to look-up tasks for this service.
|
|
|
|
continue
|
|
|
|
case s.Spec.Mode.Replicated != nil:
|
|
|
|
// For replicated services, set the desired number of tasks;
|
|
|
|
// that way we can present this information in case we're unable
|
|
|
|
// to get a list of tasks from the server.
|
|
|
|
services[i].ServiceStatus = &swarm.ServiceStatus{DesiredTasks: *s.Spec.Mode.Replicated.Replicas}
|
|
|
|
status[s.ID] = &swarm.ServiceStatus{}
|
|
|
|
taskFilter.Add("service", s.ID)
|
|
|
|
case s.Spec.Mode.Global != nil:
|
|
|
|
// No such thing as number of desired tasks for global services
|
|
|
|
services[i].ServiceStatus = &swarm.ServiceStatus{}
|
|
|
|
status[s.ID] = &swarm.ServiceStatus{}
|
|
|
|
taskFilter.Add("service", s.ID)
|
|
|
|
default:
|
|
|
|
// Unknown task type
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-23 18:16:35 -04:00
|
|
|
if len(status) == 0 {
|
|
|
|
// All services have their ServiceStatus set, so we're done
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks, err := c.TaskList(ctx, types.TaskListOptions{Filters: taskFilter})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(tasks) == 0 {
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
activeNodes, err := getActiveNodes(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
for _, task := range tasks {
|
2019-10-23 18:16:35 -04:00
|
|
|
if status[task.ServiceID] == nil {
|
|
|
|
// This should not happen in practice; either all services have
|
|
|
|
// a ServiceStatus set, or none of them.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// TODO: this should only be needed for "global" services. Replicated
|
|
|
|
// services have `Spec.Mode.Replicated.Replicas`, which should give this value.
|
2016-10-24 23:39:53 -04:00
|
|
|
if task.DesiredState != swarm.TaskStateShutdown {
|
2019-10-23 18:16:35 -04:00
|
|
|
status[task.ServiceID].DesiredTasks++
|
2016-10-24 23:39:53 -04:00
|
|
|
}
|
|
|
|
if _, nodeActive := activeNodes[task.NodeID]; nodeActive && task.Status.State == swarm.TaskStateRunning {
|
2019-10-23 18:16:35 -04:00
|
|
|
status[task.ServiceID].RunningTasks++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, service := range services {
|
|
|
|
if s := status[service.ID]; s != nil {
|
|
|
|
services[i].ServiceStatus = s
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-23 18:16:35 -04:00
|
|
|
return services, nil
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2019-10-23 18:16:35 -04:00
|
|
|
func getActiveNodes(ctx context.Context, c client.NodeAPIClient) (map[string]struct{}, error) {
|
|
|
|
nodes, err := c.NodeList(ctx, types.NodeListOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
activeNodes := make(map[string]struct{})
|
|
|
|
for _, n := range nodes {
|
|
|
|
if n.Status.State != swarm.NodeStateDown {
|
|
|
|
activeNodes[n.ID] = struct{}{}
|
2016-11-17 01:21:18 -05:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2019-10-23 18:16:35 -04:00
|
|
|
return activeNodes, nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|