From b8b810a06fa2ec321b8b84b1794c64f9c48c357a Mon Sep 17 00:00:00 2001
From: kakaZhou719 <83740799+kakaZhou719@users.noreply.github.com>
Date: Tue, 26 Apr 2022 10:18:30 +0800
Subject: [PATCH] bugfix: dump config if not exist; not to send registry if not
 exist (#1337)

* bugfix:dump config if not exist; not to send registry if not exist
---
 pkg/config/config.go                       | 13 ++++++++-----
 pkg/filesystem/cloudfilesystem/overlay2.go |  2 +-
 pkg/filesystem/cloudfilesystem/utils.go    | 11 +++++++----
 utils/iputils.go                           | 11 +++++------
 utils/ssh/connect.go                       |  5 -----
 utils/ssh/scp.go                           |  8 ++++----
 utils/ssh/ssh.go                           | 19 ++++++++++---------
 utils/ssh/sshcmd_test.go                   | 16 ++++++++--------
 8 files changed, 43 insertions(+), 42 deletions(-)

diff --git a/pkg/config/config.go b/pkg/config/config.go
index d3d319151c2..7ad74d1778b 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -102,13 +102,16 @@ func (c *Dumper) WriteFiles() (err error) {
 				continue
 			}
 			configPath := filepath.Join(mountRoot, f.Name(), config.Spec.Path)
-			//only the YAML format is supported
-			if config.Spec.Strategy == Merge {
-				configData, err = getMergeConfigData(configPath, configData)
-				if err != nil {
-					return err
+			if utils.IsExist(configPath) {
+				//only the YAML format is supported
+				if config.Spec.Strategy == Merge {
+					configData, err = getMergeConfigData(configPath, configData)
+					if err != nil {
+						return err
+					}
 				}
 			}
+
 			err = utils.WriteFile(configPath, configData)
 			if err != nil {
 				return fmt.Errorf("write config file failed %v", err)
diff --git a/pkg/filesystem/cloudfilesystem/overlay2.go b/pkg/filesystem/cloudfilesystem/overlay2.go
index 327c9b3af15..9695bc75e3d 100644
--- a/pkg/filesystem/cloudfilesystem/overlay2.go
+++ b/pkg/filesystem/cloudfilesystem/overlay2.go
@@ -101,7 +101,7 @@ func mountRootfs(ipList []string, target string, cluster *v2.Cluster, initFlag b
 	if !utils.InList(config.IP, ipList) {
 		return nil
 	}
-	return copyRegistry(config, cluster, mountEntry.mountDirs, target)
+	return copyRegistry(config.IP, cluster, mountEntry.mountDirs, target)
 }
 
 func unmountRootfs(ipList []string, cluster *v2.Cluster) error {
diff --git a/pkg/filesystem/cloudfilesystem/utils.go b/pkg/filesystem/cloudfilesystem/utils.go
index 645d2fd9db5..8895833607e 100644
--- a/pkg/filesystem/cloudfilesystem/utils.go
+++ b/pkg/filesystem/cloudfilesystem/utils.go
@@ -19,7 +19,6 @@ import (
 	"io/ioutil"
 	"path/filepath"
 
-	"github.com/alibaba/sealer/pkg/runtime"
 	v2 "github.com/alibaba/sealer/types/api/v2"
 
 	"github.com/alibaba/sealer/common"
@@ -45,13 +44,17 @@ func copyFiles(sshEntry ssh.Interface, ip, src, target string) error {
 	return nil
 }
 
-func copyRegistry(regConfig *runtime.RegistryConfig, cluster *v2.Cluster, mountDir map[string]bool, target string) error {
-	sshClient, err := ssh.GetHostSSHClient(regConfig.IP, cluster)
+func copyRegistry(regIP string, cluster *v2.Cluster, mountDir map[string]bool, target string) error {
+	sshClient, err := ssh.GetHostSSHClient(regIP, cluster)
 	if err != nil {
 		return err
 	}
 	for dir := range mountDir {
-		err = sshClient.Copy(regConfig.IP, filepath.Join(dir, common.RegistryDirName), filepath.Join(target, common.RegistryDirName))
+		dir = filepath.Join(dir, common.RegistryDirName)
+		if !utils.IsExist(dir) {
+			return nil
+		}
+		err = sshClient.Copy(regIP, dir, filepath.Join(target, common.RegistryDirName))
 		if err != nil {
 			return err
 		}
diff --git a/utils/iputils.go b/utils/iputils.go
index f5a4f708999..2ad2fc69d77 100644
--- a/utils/iputils.go
+++ b/utils/iputils.go
@@ -21,7 +21,6 @@ import (
 	"strings"
 )
 
-//use only one
 func GetHostIP(host string) string {
 	if !strings.ContainsRune(host, ':') {
 		return host
@@ -61,14 +60,14 @@ func GetHostNetInterface(host string) (string, error) {
 		if err != nil {
 			return "", fmt.Errorf("failed to get Addrs, %v", err)
 		}
-		if IsLocalIP(host, &addrs) {
+		if IsLocalIP(host, addrs) {
 			return netInterfaces[i].Name, nil
 		}
 	}
 	return "", nil
 }
 
-func GetLocalHostAddresses() (*[]net.Addr, error) {
+func GetLocalHostAddresses() ([]net.Addr, error) {
 	netInterfaces, err := net.Interfaces()
 	if err != nil {
 		fmt.Println("net.Interfaces failed, err:", err.Error())
@@ -87,11 +86,11 @@ func GetLocalHostAddresses() (*[]net.Addr, error) {
 			allAddrs = append(allAddrs, addrs[j])
 		}
 	}
-	return &allAddrs, nil
+	return allAddrs, nil
 }
 
-func IsLocalIP(ip string, addrs *[]net.Addr) bool {
-	for _, address := range *addrs {
+func IsLocalIP(ip string, addrs []net.Addr) bool {
+	for _, address := range addrs {
 		if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil && ipnet.IP.String() == ip {
 			return true
 		}
diff --git a/utils/ssh/connect.go b/utils/ssh/connect.go
index dc672f43dca..25a028d9902 100644
--- a/utils/ssh/connect.go
+++ b/utils/ssh/connect.go
@@ -26,10 +26,6 @@ import (
 	"golang.org/x/crypto/ssh"
 )
 
-/**
-  SSH connection operation
-*/
-
 const DefaultSSHPort = "22"
 
 func (s *SSH) connect(host string) (*ssh.Client, error) {
@@ -135,7 +131,6 @@ func (s *SSH) sshPasswordMethod(password string) ssh.AuthMethod {
 	return ssh.Password(password)
 }
 
-//RemoteFileExist is
 func (s *SSH) IsFileExist(host, remoteFilePath string) (bool, error) {
 	sshClient, sftpClient, err := s.sftpConnect(host)
 	if err != nil {
diff --git a/utils/ssh/scp.go b/utils/ssh/scp.go
index 1f2629594bb..d2178fec83f 100644
--- a/utils/ssh/scp.go
+++ b/utils/ssh/scp.go
@@ -141,7 +141,7 @@ func (s *SSH) sftpConnect(host string) (*ssh.Client, *sftp.Client, error) {
 	return sshClient, sftpClient, err
 }
 
-// CopyRemoteFileToLocal is scp remote file to local
+// Fetch scp remote file to local
 func (s *SSH) Fetch(host, localFilePath, remoteFilePath string) error {
 	if utils.IsLocalIP(host, s.LocalAddress) {
 		if remoteFilePath != localFilePath {
@@ -187,7 +187,7 @@ func (s *SSH) Fetch(host, localFilePath, remoteFilePath string) error {
 	return err
 }
 
-// CopyLocalToRemote is copy file or dir to remotePath, add md5 validate
+// Copy file or dir to remotePath, add md5 validate
 func (s *SSH) Copy(host, localPath, remotePath string) error {
 	go displayInitOnce.Do(displayInit)
 	if utils.IsLocalIP(host, s.LocalAddress) {
@@ -281,7 +281,7 @@ func (s *SSH) copyLocalDirToRemote(host string, sftpClient *sftp.Client, localPa
 }
 
 // check the remote file existence before copying
-// solve the sesion
+// solve the session
 func (s *SSH) copyLocalFileToRemote(host string, sftpClient *sftp.Client, localPath, remotePath string) error {
 	var (
 		srcMd5, dstMd5 string
@@ -343,7 +343,7 @@ func LocalMd5Sum(localPath string) string {
 	return md5
 }
 
-//if remote file not exist return false and nil
+// RemoteDirExist if remote file not exist return false and nil
 func (s *SSH) RemoteDirExist(host, remoteDirpath string) (bool, error) {
 	sshClient, sftpClient, err := s.sftpConnect(host)
 	if err != nil {
diff --git a/utils/ssh/ssh.go b/utils/ssh/ssh.go
index 69a429c7ae3..a6becc7ddd7 100644
--- a/utils/ssh/ssh.go
+++ b/utils/ssh/ssh.go
@@ -32,24 +32,25 @@ import (
 )
 
 type Interface interface {
-	// copy local files to remote host
+	// Copy local files to remote host
 	// scp -r /tmp root@192.168.0.2:/root/tmp => Copy("192.168.0.2","tmp","/root/tmp")
 	// need check md5sum
 	Copy(host, srcFilePath, dstFilePath string) error
-	// copy remote host files to localhost
+	// Fetch copy remote host files to localhost
 	Fetch(host, srcFilePath, dstFilePath string) error
-	// exec command on remote host, and asynchronous return logs
+	// CmdAsync exec command on remote host, and asynchronous return logs
 	CmdAsync(host string, cmd ...string) error
-	// exec command on remote host, and return combined standard output and standard error
+	// Cmd exec command on remote host, and return combined standard output and standard error
 	Cmd(host, cmd string) ([]byte, error)
-	// check remote file exist or not
+	// IsFileExist check remote file exist or not
 	IsFileExist(host, remoteFilePath string) (bool, error)
-	//Remote file existence returns true, nil
+	// RemoteDirExist Remote file existence returns true, nil
 	RemoteDirExist(host, remoteDirpath string) (bool, error)
-	// exec command on remote host, and return spilt standard output and standard error
+	// CmdToString exec command on remote host, and return spilt standard output and standard error
 	CmdToString(host, cmd, spilt string) (string, error)
-	// Get remote platform
+	// Platform Get remote platform
 	Platform(host string) (v1.Platform, error)
+
 	Ping(host string) error
 }
 
@@ -62,7 +63,7 @@ type SSH struct {
 	PkFile       string
 	PkPassword   string
 	Timeout      *time.Duration
-	LocalAddress *[]net.Addr
+	LocalAddress []net.Addr
 }
 
 func NewSSHByCluster(cluster *v1.Cluster) Interface {
diff --git a/utils/ssh/sshcmd_test.go b/utils/ssh/sshcmd_test.go
index d2dbcfe77f6..6bf27bf1ae3 100644
--- a/utils/ssh/sshcmd_test.go
+++ b/utils/ssh/sshcmd_test.go
@@ -41,7 +41,7 @@ func TestSSH_Cmd(t *testing.T) {
 					"",
 					"",
 					nil,
-					&[]net.Addr{},
+					[]net.Addr{},
 				},
 				host: "192.168.56.103",
 				cmd:  "bash /opt/touchTxt.sh",
@@ -60,7 +60,7 @@ func TestSSH_Cmd(t *testing.T) {
 					"",
 					"",
 					nil,
-					&[]net.Addr{},
+					[]net.Addr{},
 				},
 				host: "192.168.56.103",
 				cmd:  "ls /opt/test",
@@ -79,7 +79,7 @@ func TestSSH_Cmd(t *testing.T) {
 					"",
 					"",
 					nil,
-					&[]net.Addr{},
+					[]net.Addr{},
 				},
 				host: "192.168.56.103",
 				cmd:  "bash /opt/removeTxt.sh",
@@ -98,7 +98,7 @@ func TestSSH_Cmd(t *testing.T) {
 					"",
 					"",
 					nil,
-					&[]net.Addr{},
+					[]net.Addr{},
 				},
 				host: "192.168.56.103",
 				cmd:  "bash /opt/exit1.sh",
@@ -144,7 +144,7 @@ func TestSSH_CmdAsync(t *testing.T) {
 					"",
 					"",
 					nil,
-					&[]net.Addr{},
+					[]net.Addr{},
 				},
 				host: "192.168.56.103",
 				cmd:  "bash /opt/touchTxt.sh",
@@ -162,7 +162,7 @@ func TestSSH_CmdAsync(t *testing.T) {
 					"",
 					"",
 					nil,
-					&[]net.Addr{},
+					[]net.Addr{},
 				},
 				host: "192.168.56.103",
 				cmd:  "ls /opt/test",
@@ -180,7 +180,7 @@ func TestSSH_CmdAsync(t *testing.T) {
 					"",
 					"",
 					nil,
-					&[]net.Addr{},
+					[]net.Addr{},
 				},
 				host: "192.168.56.103",
 				cmd:  "bash /opt/removeTxt.sh",
@@ -198,7 +198,7 @@ func TestSSH_CmdAsync(t *testing.T) {
 					"",
 					"",
 					nil,
-					&[]net.Addr{},
+					[]net.Addr{},
 				},
 				host: "192.168.56.103",
 				cmd:  "bash /opt/exit1.sh",