From 9992ccc894e2b5289a13d3bb664a96244a9fe660 Mon Sep 17 00:00:00 2001 From: oldthreefeng Date: Fri, 28 Aug 2020 18:09:31 +0800 Subject: [PATCH] fix ETCD CaCert or key file is not exist occurs panic, #427 --- cmd/etcd.go | 14 +++++++------- install/etcd_restore.go | 4 ++++ install/etcd_save.go | 9 +++++++++ install/init.go | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cmd/etcd.go b/cmd/etcd.go index ac714daf45b..4a7de97bd0c 100644 --- a/cmd/etcd.go +++ b/cmd/etcd.go @@ -51,12 +51,12 @@ func NewEtcdCommand() *cobra.Command { cmd := &cobra.Command{ Use: "etcd ", Short: "Simplest way to snapshot/restore your kubernets etcd", - Long: `etcd save --name snapshot`, + Long: `sealos etcd save --name snapshot`, Example: exampleCmd, } - cmd.AddCommand(NewEtcdSaveComand()) - cmd.AddCommand(NewEtcdRestoreComand()) - cmd.AddCommand(NewEtcdHealthComand()) + cmd.AddCommand(NewEtcdSaveCommand()) + cmd.AddCommand(NewEtcdRestoreCommand()) + cmd.AddCommand(NewEtcdHealthCommand()) //cmd.Flags().BoolVar(&install.EtcdSnapshotSave, "snap-save", false, "snapshot your kubernets etcd ") //cmd.Flags().BoolVar(&install.EtcdRestore, "snap-restore", false, "restore your kubernets etcd") //cmd.Flags().BoolVar(&install.EtcdHealthCheck, "health", false, "check your kubernets etcd") @@ -67,7 +67,7 @@ func NewEtcdCommand() *cobra.Command { return cmd } -func NewEtcdSaveComand() *cobra.Command { +func NewEtcdSaveCommand() *cobra.Command { cmd := &cobra.Command{ Use: "save", Short: "Stores an etcd node backend snapshot to a given file", @@ -85,7 +85,7 @@ func NewEtcdSaveComand() *cobra.Command { return cmd } -func NewEtcdRestoreComand() *cobra.Command { +func NewEtcdRestoreCommand() *cobra.Command { cmd := &cobra.Command{ Use: "restore", Short: "Restores an etcd member snapshot to an etcd directory", @@ -97,7 +97,7 @@ func NewEtcdRestoreComand() *cobra.Command { return cmd } -func NewEtcdHealthComand() *cobra.Command { +func NewEtcdHealthCommand() *cobra.Command { return &cobra.Command{ Use: "health", Short: "test your etcd status", diff --git a/install/etcd_restore.go b/install/etcd_restore.go index 58dcbfd7760..900aca32451 100644 --- a/install/etcd_restore.go +++ b/install/etcd_restore.go @@ -28,6 +28,10 @@ func RandStringRunes(n int) string { func GetRestoreFlags() *EtcdFlags { e := &EtcdFlags{} + if !e.CertFileExist() { + logger.Error("ETCD CaCert or key file is not exist.") + os.Exit(1) + } err := e.Load("") if err != nil { logger.Error(err) diff --git a/install/etcd_save.go b/install/etcd_save.go index e6f403350cd..7ccceb9aa0d 100644 --- a/install/etcd_save.go +++ b/install/etcd_save.go @@ -32,6 +32,10 @@ func GetEtcdBackFlags() *EtcdFlags { ip, endpoint string ) e := &EtcdFlags{} + if !e.CertFileExist() { + logger.Error("ETCD CaCert or key file is not exist.") + os.Exit(1) + } err := e.Load("") if err != nil { logger.Error(err) @@ -257,3 +261,8 @@ func (e *EtcdFlags) HealthCheck() { logger.Error("unhealthy cluster") } } + +// CertFileExist if cert file is exist return true +func (e *EtcdFlags) CertFileExist() bool { + return FileExist(EtcdCacart) && FileExist(EtcdCert) && FileExist(EtcdKey) +} \ No newline at end of file diff --git a/install/init.go b/install/init.go index 57cef8858f4..e807f156627 100644 --- a/install/init.go +++ b/install/init.go @@ -124,7 +124,7 @@ func (s *SealosInstaller) InstallMaster0() { s.SendKubeConfigs(s.Masters, true) //master0 do sth - cmd := fmt.Sprintf("grep -qF '%s %s' /etc/hosts || echo %s %s >> /etc/hosts", IpFormat(s.Masters[0]), ApiServer) + cmd := fmt.Sprintf("grep -qF '%s %s' /etc/hosts || echo %s %s >> /etc/hosts", IpFormat(s.Masters[0]), ApiServer, IpFormat(s.Masters[0]), ApiServer) _ = SSHConfig.CmdAsync(s.Masters[0], cmd) cmd = s.Command(Version, InitMaster)