-
Notifications
You must be signed in to change notification settings - Fork 14
/
sabakan_set_template.go
67 lines (57 loc) · 1.55 KB
/
sabakan_set_template.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
package cmd
import (
"context"
"errors"
"os"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)
func validateTemplate(tmpl *cke.Cluster) error {
switch {
case len(tmpl.Nodes) != 2:
fallthrough
case tmpl.Nodes[0].ControlPlane && tmpl.Nodes[1].ControlPlane:
fallthrough
case !tmpl.Nodes[0].ControlPlane && !tmpl.Nodes[1].ControlPlane:
return errors.New("template must contain one control-plane and one non-control-plane nodes")
}
tmpl.Nodes[0].Address = "10.0.0.1"
tmpl.Nodes[1].Address = "10.0.0.2"
return tmpl.Validate()
}
// sabakanSetTemplateCmd represents the "sabakan set-template" command
var sabakanSetTemplateCmd = &cobra.Command{
Use: "set-template FILE",
Short: "set the cluster configuration template",
Long: `Set the cluster configuration template.
FILE should contain a YAML/JSON template of the cluster configuration.
The format is the same as the cluster configuration, but must contain
just one control-plane node and one non contorl-plane node.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
r, err := os.Open(args[0])
if err != nil {
return err
}
defer r.Close()
tmpl := cke.NewCluster()
err = yaml.NewDecoder(r).Decode(tmpl)
if err != nil {
return err
}
err = validateTemplate(tmpl)
if err != nil {
return err
}
well.Go(func(ctx context.Context) error {
return storage.SetSabakanTemplate(ctx, tmpl)
})
well.Stop()
return well.Wait()
},
}
func init() {
sabakanCmd.AddCommand(sabakanSetTemplateCmd)
}