-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathautoscale.go
More file actions
109 lines (97 loc) · 3.73 KB
/
autoscale.go
File metadata and controls
109 lines (97 loc) · 3.73 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
package parser
import (
"github.com/drycc/workflow-cli/internal/commands"
"github.com/drycc/workflow-cli/internal/completion"
"github.com/drycc/workflow-cli/internal/template"
"github.com/drycc/workflow-cli/pkg/i18n"
"github.com/spf13/cobra"
)
// NewAutoscaleCommand creates the autoscale command
func NewAutoscaleCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "autoscale",
Short: i18n.T("Manage autoscale for applications"),
RunE: func(cmd *cobra.Command, args []string) error {
return cmdr.AutoscaleList(app)
},
}
cmd.PersistentFlags().StringVarP(&app, "app", "a", "", i18n.T("The uniquely identifiable name for the application"))
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
cmd.AddCommand(autoscaleListCommand(cmdr))
cmd.AddCommand(autoscaleSetCommand(cmdr))
cmd.AddCommand(autoscaleUnsetCommand(cmdr))
return cmd
}
func autoscaleListCommand(cmdr *commands.DryccCmd) *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: i18n.T("List autoscale options for an application"),
Long: i18n.T("Prints a list of autoscale options for the application"),
RunE: func(cmd *cobra.Command, args []string) error {
return cmdr.AutoscaleList(app)
},
}
return cmd
}
func autoscaleSetCommand(cmdr *commands.DryccCmd) *cobra.Command {
var flags struct {
min int
max int
cpuPercent int
}
ptsArgsCompletion := completion.PtsArgsCompletion{
PtsCompletion: &completion.PtsCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile},
}
cmd := &cobra.Command{
Use: "set <ptype> --min=<min> --max=<max> --cpu-percent=<percent>",
Example: template.CustomExample(
"drycc autoscale set web --min=1 --max=10 --cpu-percent=50",
map[string]string{
"<ptype>": i18n.T("The process type to add to the application's autoscale settings"),
},
),
Args: cobra.ExactArgs(1),
Short: i18n.T("Turn on autoscale for an app"),
Long: i18n.T("Set autoscale option per process type for an app"),
ValidArgsFunction: ptsArgsCompletion.CompletionFunc,
RunE: func(cmd *cobra.Command, args []string) error {
ptype := args[0]
return cmdr.AutoscaleSet(app, ptype, flags.min, flags.max, flags.cpuPercent)
},
}
cmd.Flags().IntVarP(&flags.min, "min", "", 0, i18n.T("minimum replicas to keep around"))
cmd.Flags().IntVarP(&flags.max, "max", "", 0, i18n.T("Max replicas to scale up to"))
cmd.Flags().IntVarP(&flags.cpuPercent, "cpu-percent", "", 0, i18n.T("Target CPU utilization percentage"))
cmd.Flags().SortFlags = false
mustFlags := []string{"min", "max", "cpu-percent"}
for _, mustFlag := range mustFlags {
cmd.MarkFlagRequired(mustFlag)
}
appCompletion := completion.AppCompletion{ArgsLen: -1, ConfigFile: &cmdr.ConfigFile}
cmd.RegisterFlagCompletionFunc("app", appCompletion.CompletionFunc)
return cmd
}
func autoscaleUnsetCommand(cmdr *commands.DryccCmd) *cobra.Command {
ptsArgsCompletion := completion.PtsArgsCompletion{
PtsCompletion: &completion.PtsCompletion{AppID: &app, ArgsLen: 0, ConfigFile: &cmdr.ConfigFile},
}
cmd := &cobra.Command{
Use: "unset <ptype>",
Example: template.CustomExample(
"drycc autoscale unset web",
map[string]string{
"<ptype>": i18n.T("The process type to remove from the application's autoscale settings"),
},
),
Args: cobra.ExactArgs(1),
Short: i18n.T("Turn off autoscale for an app"),
Long: i18n.T("Unset autoscale per process type for an app"),
ValidArgsFunction: ptsArgsCompletion.CompletionFunc,
RunE: func(cmd *cobra.Command, args []string) error {
ptype := args[0]
return cmdr.AutoscaleUnset(app, ptype)
},
}
return cmd
}