Skip to content

Commit

Permalink
feature(main): sealos run uing defualt ssh port (labring#3791)
Browse files Browse the repository at this point in the history
* feature(main): add logger for ssh

Signed-off-by: cuisongliu <[email protected]>

* fix(main): sealos run uing defualt ssh port

Signed-off-by: cuisongliu <[email protected]>

---------

Signed-off-by: cuisongliu <[email protected]>
  • Loading branch information
cuisongliu committed Aug 30, 2023
1 parent 9904f62 commit 7336729
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
8 changes: 4 additions & 4 deletions pkg/apply/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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)
Expand All @@ -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))
}
5 changes: 2 additions & 3 deletions pkg/apply/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package apply
import (
"fmt"
"net"
"strconv"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

Expand Down
5 changes: 2 additions & 3 deletions pkg/apply/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"net"
"path/filepath"
"strconv"
"strings"

"github.com/labring/sealos/pkg/constants"
Expand Down Expand Up @@ -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")
Expand All @@ -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{
Expand Down
11 changes: 7 additions & 4 deletions pkg/registry/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
1 change: 1 addition & 0 deletions pkg/ssh/sshcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions pkg/types/v1beta1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit 7336729

Please sign in to comment.