-
Notifications
You must be signed in to change notification settings - Fork 15
/
cluster_set.go
62 lines (53 loc) · 1.15 KB
/
cluster_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
package cmd
import (
"context"
"io/ioutil"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
)
// clusterSetCmd represents the "cluster set" command
var clusterSetCmd = &cobra.Command{
Use: "set FILE",
Short: "load cluster configuration",
Long: `Load cluster configuration from FILE and store it in etcd.
The file must be either YAML or JSON.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
b, err := ioutil.ReadFile(args[0])
if err != nil {
return err
}
cfg := cke.NewCluster()
err = yaml.Unmarshal(b, cfg)
if err != nil {
return err
}
err = cfg.Validate(false)
if err != nil {
return err
}
well.Go(func(ctx context.Context) error {
constraints, err := storage.GetConstraints(ctx)
switch err {
case cke.ErrNotFound:
constraints = cke.DefaultConstraints()
fallthrough
case nil:
err = constraints.Check(cfg)
if err != nil {
return err
}
default:
return err
}
return storage.PutCluster(ctx, cfg)
})
well.Stop()
return well.Wait()
},
}
func init() {
clusterCmd.AddCommand(clusterSetCmd)
}