-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathscale.go
More file actions
135 lines (120 loc) · 3.89 KB
/
scale.go
File metadata and controls
135 lines (120 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package service
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/moby/moby/client"
"github.com/spf13/cobra"
)
type scaleOptions struct {
detach bool
}
func newScaleCommand(dockerCLI command.Cli) *cobra.Command {
options := &scaleOptions{}
cmd := &cobra.Command{
Use: "scale SERVICE=REPLICAS [SERVICE=REPLICAS...]",
Short: "Scale one or multiple replicated services",
Args: scaleArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runScale(cmd.Context(), dockerCLI, options, args)
},
ValidArgsFunction: completeScaleArgs(dockerCLI),
DisableFlagsInUseLine: true,
}
flags := cmd.Flags()
addDetachFlag(flags, &options.detach)
return cmd
}
func scaleArgs(cmd *cobra.Command, args []string) error {
if err := cli.RequiresMinArgs(1)(cmd, args); err != nil {
return err
}
for _, arg := range args {
if k, v, ok := strings.Cut(arg, "="); !ok || k == "" || v == "" {
return fmt.Errorf(
"invalid scale specifier '%s'.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
arg,
cmd.CommandPath(),
cmd.UseLine(),
cmd.Short,
)
}
}
return nil
}
func runScale(ctx context.Context, dockerCLI command.Cli, options *scaleOptions, args []string) error {
apiClient := dockerCLI.Client()
var (
errs []error
serviceIDs = make([]string, 0, len(args))
)
for _, arg := range args {
serviceID, scaleStr, _ := strings.Cut(arg, "=")
// validate input arg scale number
scale, err := strconv.ParseUint(scaleStr, 10, 64)
if err != nil {
errs = append(errs, fmt.Errorf("%s: invalid replicas value %s: %v", serviceID, scaleStr, err))
continue
}
warnings, err := runServiceScale(ctx, apiClient, serviceID, scale)
if err != nil {
errs = append(errs, fmt.Errorf("%s: %v", serviceID, err))
continue
}
for _, warning := range warnings {
_, _ = fmt.Fprintln(dockerCLI.Err(), warning)
}
_, _ = fmt.Fprintf(dockerCLI.Out(), "%s scaled to %d\n", serviceID, scale)
serviceIDs = append(serviceIDs, serviceID)
}
if len(serviceIDs) > 0 && !options.detach {
for _, serviceID := range serviceIDs {
if err := WaitOnService(ctx, dockerCLI, serviceID, false); err != nil {
errs = append(errs, fmt.Errorf("%s: %v", serviceID, err))
}
}
}
return errors.Join(errs...)
}
func runServiceScale(ctx context.Context, apiClient client.ServiceAPIClient, serviceID string, scale uint64) (warnings []string, _ error) {
res, err := apiClient.ServiceInspect(ctx, serviceID, client.ServiceInspectOptions{})
if err != nil {
return nil, err
}
serviceMode := &res.Service.Spec.Mode
switch {
case serviceMode.Replicated != nil:
serviceMode.Replicated.Replicas = &scale
case serviceMode.ReplicatedJob != nil:
serviceMode.ReplicatedJob.TotalCompletions = &scale
default:
return nil, errors.New("scale can only be used with replicated or replicated-job mode")
}
response, err := apiClient.ServiceUpdate(ctx, res.Service.ID, client.ServiceUpdateOptions{
Version: res.Service.Version,
Spec: res.Service.Spec,
})
if err != nil {
return nil, err
}
return response.Warnings, nil
}
// completeScaleArgs returns a completion function for the args of the scale command.
// It completes service names followed by "=", suppressing the trailing space.
func completeScaleArgs(dockerCli command.Cli) func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
// reuse the existing logic for configurable completion of service names and IDs.
completions, directive := completeServiceNames(dockerCli)(cmd, args, toComplete)
if directive == cobra.ShellCompDirectiveError {
return completions, directive
}
for i, v := range completions {
completions[i] = v + "="
}
return completions, directive | cobra.ShellCompDirectiveNoSpace
}
}