From 79e9bd5b97e74c8a7181d06393e5156c2500aba4 Mon Sep 17 00:00:00 2001 From: jianli-97 Date: Thu, 14 Sep 2023 09:31:04 +0800 Subject: [PATCH] bugfix and cleanup redundant code --- .gitignore | 4 ++ app/apis/nkd/const.go | 8 ++-- app/cmd/cmd.go | 2 +- app/cmd/config.go | 4 +- app/cmd/extend.go | 11 ++++-- app/cmd/init.go | 30 ++++++++------ app/cmd/phases/config/print.go | 52 ++++++++++++------------- app/phases/infra/terraform/terraform.go | 12 +++++- app/util/config/initconfiguration.go | 38 +++++++----------- 9 files changed, 84 insertions(+), 77 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..8e079f42 --- /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 0433ffb0..fff1e50a 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 ccd1af3a..36e84b78 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 cd68e5a8..88073ad6 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 941d374a..f8f503c0 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 1517bf32..c0e6a999 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 01675ec4..7d7125cb 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 0656ab3e..e46b7046 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 67397348..4bcbf57b 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 } -- Gitee