-
Notifications
You must be signed in to change notification settings - Fork 221
/
autoscaling.go
196 lines (151 loc) · 5 KB
/
autoscaling.go
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package cmd
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/cmdctx"
"github.com/superfly/flyctl/internal/appconfig"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/docstrings"
"github.com/spf13/cobra"
)
func newAutoscaleCommand(client *client.Client) *Command {
autoscaleStrings := docstrings.Get("autoscale")
cmd := BuildCommandKS(nil, nil, autoscaleStrings, client, requireSession, requireAppName)
// cmd.Deprecated = "use `flyctl scale` instead"
disableCmdStrings := docstrings.Get("autoscale.disable")
disableCmd := BuildCommand(cmd, runDisableAutoscaling, disableCmdStrings.Usage, disableCmdStrings.Short, disableCmdStrings.Long, client, requireSession, requireAppName)
disableCmd.Args = cobra.RangeArgs(0, 2)
disableCmd.AddBoolFlag(BoolFlagOpts{Name: "json", Shorthand: "j", Description: "JSON output"})
setCmdStrings := docstrings.Get("autoscale.set")
setCmd := BuildCommand(cmd, runSetParams, setCmdStrings.Usage, setCmdStrings.Short, setCmdStrings.Long, client, requireSession, requireAppName)
setCmd.Args = cobra.RangeArgs(0, 2)
setCmd.AddBoolFlag(BoolFlagOpts{Name: "json", Shorthand: "j", Description: "JSON output"})
showCmdStrings := docstrings.Get("autoscale.show")
showCmd := BuildCommand(cmd, runAutoscalingShow, showCmdStrings.Usage, showCmdStrings.Short, showCmdStrings.Long, client, requireSession, requireAppName)
showCmd.AddBoolFlag(BoolFlagOpts{Name: "json", Shorthand: "j", Description: "JSON output"})
return cmd
}
func runSetParams(commandContext *cmdctx.CmdContext) error {
return actualScale(commandContext, false)
}
func runDisableAutoscaling(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
app, err := cmdCtx.Client.API().GetAppCompact(ctx, cmdCtx.AppName)
if err != nil {
return err
}
if app.PlatformVersion == appconfig.MachinesPlatform {
printMachinesAutoscalingBanner()
return nil
}
newcfg := api.UpdateAutoscaleConfigInput{AppID: cmdCtx.AppName, Enabled: api.BoolPointer(false)}
cfg, err := cmdCtx.Client.API().UpdateAutoscaleConfig(ctx, newcfg)
if err != nil {
return err
}
printScaleConfig(cmdCtx, cfg)
return nil
}
func actualScale(cmdCtx *cmdctx.CmdContext, balanceRegions bool) error {
ctx := cmdCtx.Command.Context()
app, err := cmdCtx.Client.API().GetAppCompact(ctx, cmdCtx.AppName)
if err != nil {
return err
}
if app.PlatformVersion == appconfig.MachinesPlatform {
printMachinesAutoscalingBanner()
return nil
}
currentcfg, err := cmdCtx.Client.API().AppAutoscalingConfig(ctx, cmdCtx.AppName)
if err != nil {
return err
}
newcfg := api.UpdateAutoscaleConfigInput{AppID: cmdCtx.AppName}
newcfg.BalanceRegions = &balanceRegions
newcfg.MinCount = ¤tcfg.MinCount
newcfg.MaxCount = ¤tcfg.MaxCount
kvargs := make(map[string]string)
for _, pair := range cmdCtx.Args {
parts := strings.SplitN(pair, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("Scale parameters must be provided as NAME=VALUE pairs (%s is invalid)", pair)
}
key := parts[0]
value := parts[1]
kvargs[strings.ToLower(key)] = value
}
minval, found := kvargs["min"]
if found {
minint64val, err := strconv.ParseInt(minval, 10, 64)
if err != nil {
return errors.New("could not parse min count value")
}
minintval := int(minint64val)
newcfg.MinCount = &minintval
delete(kvargs, "min")
}
maxval, found := kvargs["max"]
if found {
maxint64val, err := strconv.ParseInt(maxval, 10, 64)
if err != nil {
return errors.New("could not parse max count value")
}
maxintval := int(maxint64val)
newcfg.MaxCount = &maxintval
delete(kvargs, "max")
}
if len(kvargs) != 0 {
unusedkeys := ""
for k := range kvargs {
if unusedkeys == "" {
unusedkeys = k
} else {
unusedkeys = unusedkeys + ", " + k
}
}
return errors.New("unrecognised parameters in command:" + unusedkeys)
}
cfg, err := cmdCtx.Client.API().UpdateAutoscaleConfig(ctx, newcfg)
if err != nil {
return err
}
printScaleConfig(cmdCtx, cfg)
return nil
}
func runAutoscalingShow(cmdCtx *cmdctx.CmdContext) error {
ctx := cmdCtx.Command.Context()
cfg, err := cmdCtx.Client.API().AppAutoscalingConfig(ctx, cmdCtx.AppName)
if err != nil {
return err
}
printScaleConfig(cmdCtx, cfg)
return nil
}
func printScaleConfig(cmdCtx *cmdctx.CmdContext, cfg *api.AutoscalingConfig) {
asJSON := cmdCtx.OutputJSON()
if asJSON {
cmdCtx.WriteJSON(cfg)
} else {
var mode string
if !cfg.Enabled {
mode = "Disabled"
} else {
mode = "Enabled"
}
fmt.Fprintf(cmdCtx.Out, "%15s: %s\n", "Autoscaling", mode)
if cfg.Enabled {
fmt.Fprintf(cmdCtx.Out, "%15s: %d\n", "Min Count", cfg.MinCount)
fmt.Fprintf(cmdCtx.Out, "%15s: %d\n", "Max Count", cfg.MaxCount)
}
}
}
func printMachinesAutoscalingBanner() {
fmt.Printf(`
Configuring autoscaling via 'flyctl autoscale' is supported only for apps running on Nomad platform.
Refer to this post for details on how to enable autoscaling for Apps V2:
https://community.fly.io/t/increasing-apps-v2-availability/12357
`)
}