-
Notifications
You must be signed in to change notification settings - Fork 15
/
constraints_set.go
78 lines (66 loc) · 1.55 KB
/
constraints_set.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
package cmd
import (
"context"
"errors"
"strconv"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
)
var cstrSet func(*cke.Constraints)
// constraintsSetCmd represents the "constraints set" command
var constraintsSetCmd = &cobra.Command{
Use: "set NAME VALUE",
Short: "set a constraint for cluster configuration",
Long: `Set a constraint for cluster configuration.
NAME is one of:
control-plane-count
minimum-workers
maximum-workers
VALUE is an integer.`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return errors.New("wrong number of arguments")
}
val, err := strconv.Atoi(args[1])
if err != nil {
return err
}
switch args[0] {
case "control-plane-count":
cstrSet = func(cstr *cke.Constraints) {
cstr.ControlPlaneCount = val
}
case "minimum-workers":
cstrSet = func(cstr *cke.Constraints) {
cstr.MinimumWorkers = val
}
case "maximum-workers":
cstrSet = func(cstr *cke.Constraints) {
cstr.MaximumWorkers = val
}
default:
return errors.New("no such constraint: " + args[0])
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
well.Go(func(ctx context.Context) error {
cstr, err := storage.GetConstraints(ctx)
switch err {
case cke.ErrNotFound:
cstr = cke.DefaultConstraints()
case nil:
default:
return err
}
cstrSet(cstr)
return storage.PutConstraints(ctx, cstr)
})
well.Stop()
return well.Wait()
},
}
func init() {
constraintsCmd.AddCommand(constraintsSetCmd)
}