github.com/ali-iotechsys/cli@v20.10.0+incompatible/cli/command/stack/swarm/services.go (about)

     1  package swarm
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli/command"
     7  	"github.com/docker/cli/cli/command/service"
     8  	"github.com/docker/cli/cli/command/stack/options"
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/docker/docker/api/types/swarm"
    11  )
    12  
    13  // GetServices is the swarm implementation of listing stack services
    14  func GetServices(dockerCli command.Cli, opts options.Services) ([]swarm.Service, error) {
    15  	var (
    16  		err    error
    17  		ctx    = context.Background()
    18  		client = dockerCli.Client()
    19  	)
    20  
    21  	listOpts := types.ServiceListOptions{
    22  		Filters: getStackFilterFromOpt(opts.Namespace, opts.Filter),
    23  		// When not running "quiet", also get service status (number of running
    24  		// and desired tasks). Note that this is only supported on API v1.41 and
    25  		// up; older API versions ignore this option, and we will have to collect
    26  		// the information manually below.
    27  		Status: !opts.Quiet,
    28  	}
    29  
    30  	services, err := client.ServiceList(ctx, listOpts)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	if listOpts.Status {
    36  		// Now that a request was made, we know what API version was used (either
    37  		// through configuration, or after client and daemon negotiated a version).
    38  		// If API version v1.41 or up was used; the daemon should already have done
    39  		// the legwork for us, and we don't have to calculate the number of desired
    40  		// and running tasks. On older API versions, we need to do some extra requests
    41  		// to get that information.
    42  		//
    43  		// So theoretically, this step can be skipped based on API version, however,
    44  		// some of our unit tests don't set the API version, and there may be other
    45  		// situations where the client uses the "default" version. To account for
    46  		// these situations, we do a quick check for services that do not have
    47  		// a ServiceStatus set, and perform a lookup for those.
    48  		services, err = service.AppendServiceStatus(ctx, client, services)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  	}
    53  	return services, nil
    54  }