From 2a05951680fc4108211d4c748b40a8e6e020abb0 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 28 Aug 2025 14:23:41 +0200 Subject: [PATCH] cli/command/stack: remove deprecated RunServices and swarm.GetServices These were deprecated in f0e5a0d6545399477660087e2db69ebbf831666d, 036d3a6bab54fdffd9804ab2367fb9a14e62893b, and d16c56066427be5e988c197d40c93475446bcdda and were only used internally. Signed-off-by: Sebastiaan van Stijn --- cli/command/stack/common.go | 10 ++++ cli/command/stack/options/opts.go | 10 ---- cli/command/stack/services.go | 48 +++++++++---------- .../{swarm/services.go => services_utils.go} | 19 ++------ 4 files changed, 37 insertions(+), 50 deletions(-) rename cli/command/stack/{swarm/services.go => services_utils.go} (74%) diff --git a/cli/command/stack/common.go b/cli/command/stack/common.go index 6de410da84..40b1db2cfc 100644 --- a/cli/command/stack/common.go +++ b/cli/command/stack/common.go @@ -4,6 +4,10 @@ import ( "fmt" "strings" "unicode" + + "github.com/docker/cli/cli/compose/convert" + "github.com/docker/cli/opts" + "github.com/moby/moby/api/types/filters" ) // validateStackName checks if the provided string is a valid stack name (namespace). @@ -29,3 +33,9 @@ func validateStackNames(namespaces []string) error { func quotesOrWhitespace(r rune) bool { return unicode.IsSpace(r) || r == '"' || r == '\'' } + +func getStackFilterFromOpt(namespace string, opt opts.FilterOpt) filters.Args { + filter := opt.Value() + filter.Add("label", convert.LabelNamespace+"="+namespace) + return filter +} diff --git a/cli/command/stack/options/opts.go b/cli/command/stack/options/opts.go index 36a01f4e02..58848883fa 100644 --- a/cli/command/stack/options/opts.go +++ b/cli/command/stack/options/opts.go @@ -42,13 +42,3 @@ type Remove struct { Namespaces []string Detach bool } - -// Services holds docker stack services options -// -// Deprecated: this type was for internal use and will be removed in the next release. -type Services struct { - Quiet bool - Format string - Filter opts.FilterOpt - Namespace string -} diff --git a/cli/command/stack/services.go b/cli/command/stack/services.go index f10cfa3105..13823ae645 100644 --- a/cli/command/stack/services.go +++ b/cli/command/stack/services.go @@ -7,30 +7,33 @@ import ( "github.com/docker/cli/cli" "github.com/docker/cli/cli/command" + "github.com/docker/cli/cli/command/formatter" "github.com/docker/cli/cli/command/service" - "github.com/docker/cli/cli/command/stack/formatter" - "github.com/docker/cli/cli/command/stack/options" - "github.com/docker/cli/cli/command/stack/swarm" flagsHelper "github.com/docker/cli/cli/flags" cliopts "github.com/docker/cli/opts" "github.com/fvbommel/sortorder" - swarmtypes "github.com/moby/moby/api/types/swarm" + "github.com/moby/moby/api/types/swarm" "github.com/spf13/cobra" ) -// servicesOptions holds docker stack services options -type servicesOptions = options.Services +// serviceListOptions holds docker stack services options +type serviceListOptions = struct { + quiet bool + format string + filter cliopts.FilterOpt + namespace string +} func newServicesCommand(dockerCLI command.Cli) *cobra.Command { - opts := servicesOptions{Filter: cliopts.NewFilterOpt()} + opts := serviceListOptions{filter: cliopts.NewFilterOpt()} cmd := &cobra.Command{ Use: "services [OPTIONS] STACK", Short: "List the services in the stack", Args: cli.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - opts.Namespace = args[0] - if err := validateStackName(opts.Namespace); err != nil { + opts.namespace = args[0] + if err := validateStackName(opts.namespace); err != nil { return err } return runServices(cmd.Context(), dockerCLI, opts) @@ -40,41 +43,34 @@ func newServicesCommand(dockerCLI command.Cli) *cobra.Command { }, } flags := cmd.Flags() - flags.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs") - flags.StringVar(&opts.Format, "format", "", flagsHelper.FormatHelp) - flags.VarP(&opts.Filter, "filter", "f", "Filter output based on conditions provided") + flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only display IDs") + flags.StringVar(&opts.format, "format", "", flagsHelper.FormatHelp) + flags.VarP(&opts.filter, "filter", "f", "Filter output based on conditions provided") return cmd } -// RunServices performs a stack services against the specified swarm cluster -// -// Deprecated: this function was for internal use and will be removed in the next release. -func RunServices(ctx context.Context, dockerCLI command.Cli, opts options.Services) error { - return runServices(ctx, dockerCLI, opts) -} - // runServices performs a stack services against the specified swarm cluster -func runServices(ctx context.Context, dockerCLI command.Cli, opts servicesOptions) error { - services, err := swarm.GetServices(ctx, dockerCLI, opts) +func runServices(ctx context.Context, dockerCLI command.Cli, opts serviceListOptions) error { + services, err := getServices(ctx, dockerCLI.Client(), opts) if err != nil { return err } return formatWrite(dockerCLI, services, opts) } -func formatWrite(dockerCLI command.Cli, services []swarmtypes.Service, opts servicesOptions) error { +func formatWrite(dockerCLI command.Cli, services []swarm.Service, opts serviceListOptions) error { // if no services in the stack, print message and exit 0 if len(services) == 0 { - _, _ = fmt.Fprintln(dockerCLI.Err(), "Nothing found in stack:", opts.Namespace) + _, _ = fmt.Fprintln(dockerCLI.Err(), "Nothing found in stack:", opts.namespace) return nil } sort.Slice(services, func(i, j int) bool { return sortorder.NaturalLess(services[i].Spec.Name, services[j].Spec.Name) }) - f := opts.Format + f := opts.format if len(f) == 0 { - if len(dockerCLI.ConfigFile().ServicesFormat) > 0 && !opts.Quiet { + if len(dockerCLI.ConfigFile().ServicesFormat) > 0 && !opts.quiet { f = dockerCLI.ConfigFile().ServicesFormat } else { f = formatter.TableFormatKey @@ -83,7 +79,7 @@ func formatWrite(dockerCLI command.Cli, services []swarmtypes.Service, opts serv servicesCtx := formatter.Context{ Output: dockerCLI.Out(), - Format: service.NewListFormat(f, opts.Quiet), + Format: service.NewListFormat(f, opts.quiet), } return service.ListFormatWrite(servicesCtx, services) } diff --git a/cli/command/stack/swarm/services.go b/cli/command/stack/services_utils.go similarity index 74% rename from cli/command/stack/swarm/services.go rename to cli/command/stack/services_utils.go index 0cfa062a49..4fd10f2d30 100644 --- a/cli/command/stack/swarm/services.go +++ b/cli/command/stack/services_utils.go @@ -1,31 +1,22 @@ -package swarm +package stack import ( "context" - "github.com/docker/cli/cli/command" "github.com/docker/cli/cli/command/service" - "github.com/docker/cli/cli/command/stack/options" "github.com/moby/moby/api/types/swarm" "github.com/moby/moby/client" ) -// GetServices is the swarm implementation of listing stack services -// -// Deprecated: this function was for internal use and will be removed in the next release. -func GetServices(ctx context.Context, dockerCLI command.Cli, opts options.Services) ([]swarm.Service, error) { - var ( - err error - apiClient = dockerCLI.Client() - ) - +// getServices is the swarm implementation of listing stack services +func getServices(ctx context.Context, apiClient client.APIClient, opts serviceListOptions) ([]swarm.Service, error) { listOpts := client.ServiceListOptions{ - Filters: getStackFilterFromOpt(opts.Namespace, opts.Filter), + Filters: getStackFilterFromOpt(opts.namespace, opts.filter), // 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, + Status: !opts.quiet, } services, err := apiClient.ServiceList(ctx, listOpts)