diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..8e079f42e20db66cf04e209b488c0b64d27e279b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +terraform +providers +master +worker \ No newline at end of file diff --git a/app/apis/nkd/const.go b/app/apis/nkd/const.go index 0433ffb035545df89bff6914dbf51bdfdd51b4dd..fff1e50a6e98d2ae3552b16e1457a8776e4c1ef1 100644 --- a/app/apis/nkd/const.go +++ b/app/apis/nkd/const.go @@ -22,10 +22,10 @@ var ( NkdClusterName = "example nkd cluster" // system - MasterHostName = "master" - WorkerHostName = "worker" + MasterHostName = "k8s-master" + WorkerHostName = "k8s-worker" Username = "root" - Password = "" + Password = "$1$yoursalt$UGhjCXAJKpWWpeN8xsF.c/" // repo Secret = []map[string]string{{"repousre": ""}} @@ -111,5 +111,5 @@ var ( PauseImageTag = "3.6" CorednsImageTag = "v1.8.6" ReleaseImageURl = "" - CertificateKey = "" + CertificateKey = "40652d74835eceaa99be3a7c177c9f47b71a601924100dd67567fed9fa0bb6a4" ) diff --git a/app/cmd/cmd.go b/app/cmd/cmd.go index ccd1af3ad13c36ece286f067f54c433716f6ace6..36e84b7846c71c853bc94e9a4c8667d429b6d425 100755 --- a/app/cmd/cmd.go +++ b/app/cmd/cmd.go @@ -28,11 +28,11 @@ func NewNkdCommand(in io.Reader, out, err io.Writer) *cobra.Command { } cmds.ResetFlags() - // TODO: 修改名称 cmds.AddCommand(NewConfigCommand()) cmds.AddCommand(NewDeployCommand()) cmds.AddCommand(NewDestroyCommand()) cmds.AddCommand(NewUpgradeCommand()) + // TODO: 当前extend是指扩展到的worker节点个数,后续应改成想扩展的worker节点个数。 cmds.AddCommand(NewExtendCommand()) return cmds diff --git a/app/cmd/config.go b/app/cmd/config.go index cd68e5a8f36f83982709ca09b069a105cf1f4dbc..88073ad626ed9e03d3ae1bc44f583e85bb4056ca 100755 --- a/app/cmd/config.go +++ b/app/cmd/config.go @@ -17,18 +17,16 @@ package cmd import ( "nestos-kubernetes-deployer/app/cmd/phases/config" - "nestos-kubernetes-deployer/app/cmd/phases/workflow" "github.com/spf13/cobra" ) func NewConfigCommand() *cobra.Command { - configRunner := workflow.NewRunner() cmd := &cobra.Command{ Use: "config", Short: "Manage a k8s cluster", RunE: func(cmd *cobra.Command, args []string) error { - return configRunner.Run() + return cmd.Help() }, } diff --git a/app/cmd/extend.go b/app/cmd/extend.go index 941d374a1ba414009c10e1dbf33a928bfb4041a4..f8f503c094ec590b9b62056c2fa439473c366c71 100755 --- a/app/cmd/extend.go +++ b/app/cmd/extend.go @@ -24,19 +24,22 @@ import ( func NewExtendCommand() *cobra.Command { var num int cmd := &cobra.Command{ - Use: "extended", - Short: "Extended worker nodes of kubernetes cluster", + Use: "extend", + Short: "Extend worker nodes of kubernetes cluster", RunE: func(cmd *cobra.Command, args []string) error { + if num == 0 { + return cmd.Help() + } + cluster := &infra.Cluster{ Node: "worker", Num: num, } - return cluster.Extend() }, } - cmd.PersistentFlags().IntVarP(&num, "num", "n", 3, "number of the extended nodes") + cmd.PersistentFlags().IntVarP(&num, "num", "n", 0, "extend to the number of the nodes") return cmd } diff --git a/app/cmd/init.go b/app/cmd/init.go index 1517bf32ce72c895f0d1d7ee54c911669c08c62a..c0e6a999a3e83ecb021dcd8e9fe9bbbb8b492348 100755 --- a/app/cmd/init.go +++ b/app/cmd/init.go @@ -17,6 +17,7 @@ limitations under the License. package cmd import ( + "fmt" "nestos-kubernetes-deployer/app/apis/nkd" "nestos-kubernetes-deployer/app/cmd/phases/initconfig" "nestos-kubernetes-deployer/app/cmd/phases/workflow" @@ -33,12 +34,13 @@ type initData struct { func NewInitDefaultNkdConfigCommand() *cobra.Command { initRunner := workflow.NewRunner() var config string + cmd := &cobra.Command{ Use: "init", - Short: "Use this command to init ign, cert config", + Short: "Use this command to init cert, ign and tf config", RunE: func(cmd *cobra.Command, args []string) error { initRunner.SetDataInitializer(func(cmd *cobra.Command, args []string) (workflow.RunData, string, error) { - data, nodetype, err := newInitData(cmd, args, config) + data, nodetype, err := newInitData(config) if err != nil { return nil, "", err } @@ -54,6 +56,7 @@ func NewInitDefaultNkdConfigCommand() *cobra.Command { // initRunner.AppendPhase(initconfig.NewGenerateCertsCmd()) initRunner.AppendPhase(initconfig.NewGenerateIgnCmd()) initRunner.AppendPhase(initconfig.NewGenerateTFCmd()) + cmd.PersistentFlags().StringVarP(&config, "config", "c", "", "config for init") return cmd } @@ -65,19 +68,22 @@ func (i *initData) MasterCfg() *nkd.Master { func (i *initData) WorkerCfg() *nkd.Worker { return i.workercfg } -func newInitData(cmd *cobra.Command, args []string, cfgPath string) (*initData, string, error) { + +func newInitData(cfgPath string) (*initData, string, error) { cfg, nodetype, err := config.LoadOrDefaultInitConfiguration(cfgPath) if err != nil { return nil, "", err } - _, ok := cfg.(*nkd.Master) - if ok { - return &initData{ - mastercfg: cfg.(*nkd.Master), - }, nodetype, nil - } else { - return &initData{ - workercfg: cfg.(*nkd.Worker), - }, nodetype, nil + + initData := &initData{} + switch cfg := cfg.(type) { + case *nkd.Master: + initData.mastercfg = cfg + case *nkd.Worker: + initData.workercfg = cfg + default: + return nil, "", fmt.Errorf("please provide the path of the cluster node config, master or worker") } + + return initData, nodetype, nil } diff --git a/app/cmd/phases/config/print.go b/app/cmd/phases/config/print.go index 01675ec48dd37fc32dca1e7bcb756877c080c47e..7d7125cbaabc100e5e64fa6a5e4ef75c904ab79e 100644 --- a/app/cmd/phases/config/print.go +++ b/app/cmd/phases/config/print.go @@ -32,8 +32,7 @@ func NewPrintDefaultNkdConfigCommand() *cobra.Command { Use: "print", Short: "use this command to print nkd config", RunE: func(cmd *cobra.Command, args []string) error { - return nil - // return runPrintDefaultConfig() + return cmd.Help() }, } cmd.AddCommand(newPrintMasterDefaultConfigCommand()) @@ -51,7 +50,7 @@ func newPrintWorkerDefaultConfigCommand() *cobra.Command { func newCommandPrintDefaultNodeConfig(node string) *cobra.Command { cmd := &cobra.Command{ - Use: fmt.Sprintf("%s", node), + Use: node, Short: fmt.Sprintf("use this command to init %s default config", node), RunE: func(cmd *cobra.Command, args []string) error { return runPrintDefaultConfig(node) @@ -60,35 +59,34 @@ func newCommandPrintDefaultNodeConfig(node string) *cobra.Command { return cmd } -func runPrintDefaultConfig(node string) error { - if node == "master" { - internalconfig := &nkd.Master{} - DefaultedStaticMasterConfiguration(internalconfig) - conf, err := yaml.Marshal(&internalconfig) - if err != nil { - return err - } +func createConfigFile(nodeType string, config interface{}, fileName string) error { + conf, err := yaml.Marshal(config) + if err != nil { + return err + } - if err = os.MkdirAll(node, os.ModePerm); err != nil { - return err - } + if err := os.MkdirAll(nodeType, os.ModePerm); err != nil { + return err + } - if err = os.WriteFile(filepath.Join(node, "master.yaml"), conf, 0644); err != nil { - return err - } - } else if node == "worker" { - internalconfig := &nkd.Worker{} - DefaultedStaticWorkerConfiguration(internalconfig) - conf, err := yaml.Marshal(&internalconfig) - if err != nil { - return err - } + if err := os.WriteFile(filepath.Join(nodeType, fileName), conf, 0644); err != nil { + return err + } + + return nil +} - if err = os.MkdirAll(node, os.ModePerm); err != nil { +func runPrintDefaultConfig(node string) error { + if node == "master" { + internalConfig := &nkd.Master{} + DefaultedStaticMasterConfiguration(internalConfig) + if err := createConfigFile(node, internalConfig, "master.yaml"); err != nil { return err } - - if err = os.WriteFile(filepath.Join(node, "worker.yaml"), conf, 0644); err != nil { + } else if node == "worker" { + internalConfig := &nkd.Worker{} + DefaultedStaticWorkerConfiguration(internalConfig) + if err := createConfigFile(node, internalConfig, "worker.yaml"); err != nil { return err } } diff --git a/app/phases/infra/terraform/terraform.go b/app/phases/infra/terraform/terraform.go index 0656ab3e14102c7e803ce65f41940be785b683ed..e46b7046ec75948e54b4607f7fa12b8bdfa7f6f4 100644 --- a/app/phases/infra/terraform/terraform.go +++ b/app/phases/infra/terraform/terraform.go @@ -71,8 +71,16 @@ func TFInit(tfDir string, terraformDir string) (err error) { return errors.Wrap(err, "failed to create a new tfexec") } - // 使用本地terraform插件 - err = tf.Init(context.Background(), tfexec.PluginDir(filepath.Join(terraformDir, ".terraform/providers"))) + // 尝试使用本地插件目录执行初始化 + err = tf.Init(context.Background(), tfexec.PluginDir(filepath.Join(terraformDir, "providers"))) + if err == nil { + return nil + } + + fmt.Print("Failed to initialize Terraform with existed plugin directory\nStart downloading plugins...\n") + // 设置插件下载的路径 + os.Setenv("TF_DATA_DIR", terraformDir) + err = tf.Init(context.Background(), tfexec.Upgrade(false)) if err != nil { return errors.Wrap(err, "failed to init terraform") } diff --git a/app/util/config/initconfiguration.go b/app/util/config/initconfiguration.go index 67397348b7a4fed16793a0a8d3b5078f35874f44..4bcbf57b145d8a1bf2f3b09ce7d789021f6a8f5f 100644 --- a/app/util/config/initconfiguration.go +++ b/app/util/config/initconfiguration.go @@ -17,9 +17,9 @@ limitations under the License. package config import ( - "os" - + "fmt" "nestos-kubernetes-deployer/app/apis/nkd" + "os" "gopkg.in/yaml.v2" ) @@ -33,49 +33,39 @@ func LoadOrDefaultInitConfiguration(cfgPath string) (interface{}, string, error) return cfg, nodetype, nil } - cfg, err := DefaultinitConfiguration() - if err != nil { - return nil, "", err - } - return cfg, "", nil + return DefaultinitConfiguration() } func LoadInitConfigurationFromFile(cfg string) (interface{}, string, error) { node := new(nkd.Node) yamlFile, err := os.ReadFile(cfg) - if err != nil { return nil, "", err } - err = yaml.Unmarshal(yamlFile, node) if err != nil { return nil, "", err } - nodetype := node.Node - - if nodetype == "master" { + switch node.Node { + case "master": master := new(nkd.Master) - masterinfo, err := Unmarshal(master, yamlFile) + masterInfo, err := Unmarshal(master, yamlFile) if err != nil { return nil, "", err } - return masterinfo, nodetype, nil - - } else if nodetype == "worker" { + return masterInfo, "master", nil + case "worker": worker := new(nkd.Worker) - workerinfo, err := Unmarshal(worker, yamlFile) + workerInfo, err := Unmarshal(worker, yamlFile) if err != nil { return nil, "", err } - return workerinfo, nodetype, nil - - } else { - return nil, "", err + return workerInfo, "worker", nil + default: + return nil, "", fmt.Errorf("unsupported node type: %s", node.Node) } - } func Unmarshal(nodeinfo interface{}, cfg []byte) (interface{}, error) { @@ -86,6 +76,6 @@ func Unmarshal(nodeinfo interface{}, cfg []byte) (interface{}, error) { return nodeinfo, nil } -func DefaultinitConfiguration() (*interface{}, error) { - return nil, nil +func DefaultinitConfiguration() (*interface{}, string, error) { + return nil, "", nil }