-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathcreate.go
75 lines (63 loc) · 1.94 KB
/
create.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
package cmd
import (
"fmt"
"github.com/cnrancher/autok3s/cmd/common"
"github.com/cnrancher/autok3s/pkg/providers"
"github.com/cnrancher/autok3s/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var (
createCmd = &cobra.Command{
Use: "create",
Short: "Create a K3s cluster",
}
cProvider = ""
cp providers.Provider
)
func init() {
createCmd.Flags().StringVarP(&cProvider, "provider", "p", cProvider, "Provider is a module which provides an interface for managing cloud resources")
}
// CreateCommand create command.
func CreateCommand() *cobra.Command {
// load dynamic provider flags.
pStr := common.FlagHackLookup("--provider")
if pStr != "" {
if reg, err := providers.GetProvider(pStr); err != nil {
logrus.Fatalln(err)
} else {
cp = reg
}
createCmd.Flags().AddFlagSet(utils.ConvertFlags(createCmd, cp.GetCredentialFlags()))
createCmd.Flags().AddFlagSet(utils.ConvertFlags(createCmd, cp.GetOptionFlags()))
createCmd.Flags().AddFlagSet(utils.ConvertFlags(createCmd, cp.GetCreateFlags()))
createCmd.Example = cp.GetUsageExample("create")
createCmd.Use = fmt.Sprintf("create -p %s", pStr)
}
createCmd.PreRunE = func(cmd *cobra.Command, _ []string) error {
if cProvider == "" {
logrus.Fatalln("required flag(s) \"--provider\" not set")
}
common.BindEnvFlags(cmd)
if err := common.MakeSureCredentialFlag(cmd.Flags(), cp); err != nil {
return err
}
utils.ValidateRequiredFlags(cmd.Flags())
return nil
}
createCmd.Run = func(_ *cobra.Command, _ []string) {
// generate cluster name. i.e. input: "--name k3s1 --region cn-hangzhou" output: "k3s1.cn-hangzhou.<provider>".
cp.GenerateClusterName()
if err := cp.BindCredential(); err != nil {
logrus.Fatalln(err)
}
if err := cp.CreateCheck(); err != nil {
logrus.Fatalln(err)
}
// create k3s cluster with generated cluster name.
if err := cp.CreateK3sCluster(); err != nil {
logrus.Fatalln(err)
}
}
return createCmd
}