From d27f4fbc84181622bb48cb569fbdfdb31f1ae0a4 Mon Sep 17 00:00:00 2001 From: sealos-ci-robot <109538726+sealos-ci-robot@users.noreply.github.com> Date: Wed, 30 Aug 2023 19:04:51 +0800 Subject: [PATCH] feature(main): sealos run uing defualt ssh port (#3791) (#3801) * feature(main): add logger for ssh * fix(main): sealos run uing defualt ssh port --------- Signed-off-by: cuisongliu Co-authored-by: cuisongliu --- pkg/apply/run.go | 8 ++++---- pkg/apply/scale.go | 5 ++--- pkg/apply/utils.go | 5 ++--- pkg/registry/helpers/helpers.go | 11 +++++++---- pkg/ssh/sshcmd.go | 1 + pkg/types/v1beta1/cluster.go | 7 +++++++ 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/pkg/apply/run.go b/pkg/apply/run.go index 60de5804d36..204d9270ad1 100644 --- a/pkg/apply/run.go +++ b/pkg/apply/run.go @@ -117,7 +117,7 @@ func (r *ClusterArgs) runArgs(cmd *cobra.Command, args *RunArgs, imageList []str r.cluster.SetNewImages(imageList) - defaultPort := strconv.Itoa(int(defaultSSHPort(r.cluster.Spec.SSH.Port))) + defaultPort := defaultSSHPort(r.cluster.Spec.SSH.Port) masters := stringsutil.SplitRemoveEmpty(args.Cluster.Masters, ",") nodes := stringsutil.SplitRemoveEmpty(args.Cluster.Nodes, ",") r.hosts = []v2.Host{} @@ -139,7 +139,7 @@ func (r *ClusterArgs) runArgs(cmd *cobra.Command, args *RunArgs, imageList []str } func (r *ClusterArgs) setHostWithIpsPort(ips []string, roles []string) { - defaultPort := strconv.Itoa(int(defaultSSHPort(r.cluster.Spec.SSH.Port))) + defaultPort := defaultSSHPort(r.cluster.Spec.SSH.Port) hostMap := map[string]*v2.Host{} for i := range ips { ip, port := iputils.GetHostIPAndPortOrDefault(ips[i], defaultPort) @@ -165,9 +165,9 @@ func (r *ClusterArgs) setHostWithIpsPort(ips []string, roles []string) { } } -func defaultSSHPort(port uint16) uint16 { +func defaultSSHPort(port uint16) string { if port == 0 { port = v2.DefaultSSHPort } - return port + return strconv.Itoa(int(port)) } diff --git a/pkg/apply/scale.go b/pkg/apply/scale.go index 06e3b5a9a8e..69d520bb5b4 100644 --- a/pkg/apply/scale.go +++ b/pkg/apply/scale.go @@ -17,7 +17,6 @@ package apply import ( "fmt" "net" - "strconv" "strings" "github.com/spf13/cobra" @@ -126,7 +125,7 @@ func verifyAndSetNodes(cmd *cobra.Command, cluster *v2.Cluster, scaleArgs *Scale } } - defaultPort := strconv.Itoa(int(cluster.Spec.SSH.Port)) + defaultPort := defaultSSHPort(cluster.Spec.SSH.Port) var hosts []v2.Host var hasMaster bool @@ -226,7 +225,7 @@ func deleteNodes(cluster *v2.Cluster, scaleArgs *ScaleArgs) error { return fmt.Errorf("master0 machine cannot be deleted") } - defaultPort := strconv.Itoa(int(cluster.Spec.SSH.Port)) + defaultPort := defaultSSHPort(cluster.Spec.SSH.Port) hostsSet := sets.NewString() diff --git a/pkg/apply/utils.go b/pkg/apply/utils.go index d2efb6ad444..8aec20d7d38 100644 --- a/pkg/apply/utils.go +++ b/pkg/apply/utils.go @@ -20,7 +20,6 @@ import ( "fmt" "net" "path/filepath" - "strconv" "strings" "github.com/labring/sealos/pkg/constants" @@ -141,7 +140,7 @@ func GetNewImages(currentCluster, desiredCluster *v2.Cluster) []string { } func CheckAndInitialize(cluster *v2.Cluster) { - cluster.Spec.SSH.Port = defaultSSHPort(cluster.Spec.SSH.Port) + cluster.Spec.SSH.Port = cluster.Spec.SSH.DefaultPort() if cluster.Spec.SSH.Pk == "" { cluster.Spec.SSH.Pk = filepath.Join(constants.GetHomeDir(), ".ssh", "id_rsa") @@ -152,7 +151,7 @@ func CheckAndInitialize(cluster *v2.Cluster) { sshClient := ssh.NewSSHClient(&clusterSSH, true) localIpv4 := iputils.GetLocalIpv4() - defaultPort := strconv.Itoa(int(cluster.Spec.SSH.Port)) + defaultPort := defaultSSHPort(cluster.Spec.SSH.Port) addr := net.JoinHostPort(localIpv4, defaultPort) cluster.Spec.Hosts = append(cluster.Spec.Hosts, v2.Host{ diff --git a/pkg/registry/helpers/helpers.go b/pkg/registry/helpers/helpers.go index c4d35d2de44..fad08545e77 100644 --- a/pkg/registry/helpers/helpers.go +++ b/pkg/registry/helpers/helpers.go @@ -41,13 +41,16 @@ func GetRegistryInfo(sshInterface ssh.Interface, rootfs, defaultRegistry string) Data: constants.DefaultRegistryData, } etcPath := path.Join(rootfs, constants.EtcDirName, RegistryCustomConfig) - out, _ := sshInterface.Cmd(defaultRegistry, fmt.Sprintf("cat %s", etcPath)) + out, err := sshInterface.Cmd(defaultRegistry, fmt.Sprintf("cat %s", etcPath)) + if err != nil { + logger.Warn("load registry config error: %+v, using default registry config", err) + return DefaultConfig + } logger.Debug("registry config data info: %s", string(out)) readConfig := &v1beta1.RegistryConfig{} - err := yaml.Unmarshal(out, &readConfig) + err = yaml.Unmarshal(out, &readConfig) if err != nil { - logger.Warn("read registry config path error: %+v", err) - logger.Info("use default registry config") + logger.Warn("read registry config path error: %+v, using default registry config", err) return DefaultConfig } if readConfig.IP == "" { diff --git a/pkg/ssh/sshcmd.go b/pkg/ssh/sshcmd.go index cf4853185eb..2cabc3468b4 100644 --- a/pkg/ssh/sshcmd.go +++ b/pkg/ssh/sshcmd.go @@ -118,6 +118,7 @@ func (c *Client) Cmd(host, cmd string) ([]byte, error) { d, err := exec.RunBashCmd(cmd) return []byte(d), err } + logger.Debug("start to exec `%s` on %s", cmd, host) client, session, err := c.Connect(host) if err != nil { return nil, fmt.Errorf("failed to create ssh session for %s: %v", host, err) diff --git a/pkg/types/v1beta1/cluster.go b/pkg/types/v1beta1/cluster.go index 57fafa9873a..955a1a576ef 100644 --- a/pkg/types/v1beta1/cluster.go +++ b/pkg/types/v1beta1/cluster.go @@ -219,6 +219,13 @@ type SSH struct { Port uint16 `json:"port,omitempty"` } +func (s *SSH) DefaultPort() uint16 { + if s.Port != 0 { + return s.Port + } + return 22 +} + type Host struct { IPS []string `json:"ips,omitempty"` Roles []string `json:"roles,omitempty"`