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