Skip to content

Commit

Permalink
修复echo yaml str的时候未知命令
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangguanzhang committed Apr 4, 2020
1 parent 2475f0b commit cb4e615
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cert/cert_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// return sealos cert command
func CertCMD(altNames []string, hostIP, hostName, serviceCIRD , DNSDomain string) string {
func CertCMD(altNames []string, hostIP, hostName, serviceCIRD, DNSDomain string) string {
cmd := fmt.Sprintf("sealos cert --node-ip %s --node-name %s --service-cird %s --dns-domain %s", hostIP, hostName, serviceCIRD, DNSDomain)
for _, name := range altNames {
cmd += fmt.Sprintf(" --alt-names %s", name)
Expand Down
4 changes: 2 additions & 2 deletions cert/kube_certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const (
)

// apiServerIPAndDomains = MasterIP + VIP + CertSANS 暂时只有apiserver, 记得把cluster.local后缀加到apiServerIPAndDOmas里先
func NewSealosCertMetaData(certPATH, certEtcdPATH string, apiServerIPAndDomains []string, SvcCIDR, nodeName, nodeIP,DNSDomain string) (*SealosCertMetaData, error) {
func NewSealosCertMetaData(certPATH, certEtcdPATH string, apiServerIPAndDomains []string, SvcCIDR, nodeName, nodeIP, DNSDomain string) (*SealosCertMetaData, error) {
data := &SealosCertMetaData{}
data.CertPath = certPATH
data.CertEtcdPath = certEtcdPATH
Expand Down Expand Up @@ -194,7 +194,7 @@ func (meta *SealosCertMetaData) apiServerAltName(certList *[]Config) {
(*certList)[APIserverCert].AltNames.DNSNames = append((*certList)[APIserverCert].AltNames.DNSNames,
meta.APIServer.DNSNames...)
(*certList)[APIserverCert].AltNames.DNSNames = append((*certList)[APIserverCert].AltNames.DNSNames,
meta.NodeName, fmt.Sprintf("kubernetes.default.svc.%s",meta.DNSDomain))
meta.NodeName, fmt.Sprintf("kubernetes.default.svc.%s", meta.DNSDomain))
(*certList)[APIserverCert].AltNames.IPs = append((*certList)[APIserverCert].AltNames.IPs,
meta.APIServer.IPs...)

Expand Down
2 changes: 1 addition & 1 deletion cmd/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var certCmd = &cobra.Command{
Short: "generate certs",
Long: `you can specify expire time`,
Run: func(cmd *cobra.Command, args []string) {
cert.GenerateCert(config.CertPath, config.CertEtcdPath, config.AltNames, config.NodeIP, config.NodeName, config.ServiceCIRD,config.DNSDomain)
cert.GenerateCert(config.CertPath, config.CertEtcdPath, config.AltNames, config.NodeIP, config.NodeName, config.ServiceCIRD, config.DNSDomain)
},
}

Expand Down
4 changes: 2 additions & 2 deletions install/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ func (s *SealosInstaller) KubeadmConfigInstall() {
}

func getDefaultSANs() []string {
var sans=[]string{"127.0.0.1","apiserver.cluster.local",VIP}
for _,master := range MasterIPs {
var sans = []string{"127.0.0.1", "apiserver.cluster.local", VIP}
for _, master := range MasterIPs {
sans = append(sans, IpFormat(master))
}
return sans
Expand Down
18 changes: 10 additions & 8 deletions install/join.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (s *SealosInstaller) JoinMasters(masters []string) {
_ = SSHConfig.CmdAsync(master, cmdHosts)
copyk8sConf := `mkdir -p /root/.kube && cp -i /etc/kubernetes/admin.conf /root/.kube/config`
_ = SSHConfig.CmdAsync(master, copyk8sConf)
cleaninstall := `rm -rf /root/kube`
cleaninstall := `rm -rf /root/kube || :`
_ = SSHConfig.CmdAsync(master, cleaninstall)
}(master)
}
Expand All @@ -125,7 +125,8 @@ func (s *SealosInstaller) JoinNodes() {
//create lvscare static pod
yaml := ipvs.LvsStaticPodYaml(VIP, MasterIPs, "")
_ = SSHConfig.CmdAsync(node, cmd)
_ = SSHConfig.CmdAsync(node, fmt.Sprintf("mkdir -p /etc/kubernetes/manifests && echo '%s' > /etc/kubernetes/manifests/kube-sealyun-lvscare.yaml", yaml))
_ = SSHConfig.Cmd(node, "mkdir -p /etc/kubernetes/manifests")
SSHConfig.Copy(node, "/etc/kubernetes/manifests/kube-sealyun-lvscare.yaml", []byte(yaml))

cleaninstall := `rm -rf /root/kube`
_ = SSHConfig.CmdAsync(node, cleaninstall)
Expand All @@ -142,8 +143,8 @@ func (s *SealosInstaller) lvscare() {
go func(node string) {
defer wg.Done()
yaml := ipvs.LvsStaticPodYaml(VIP, MasterIPs, "")
_ = SSHConfig.CmdAsync(node, "rm -rf /etc/kubernetes/manifests/kube-sealyun-lvscare*")
_ = SSHConfig.CmdAsync(node, fmt.Sprintf("mkdir -p /etc/kubernetes/manifests && echo '%s' > /etc/kubernetes/manifests/kube-sealyun-lvscare.yaml", yaml))
_ = SSHConfig.Cmd(node, "rm -rf /etc/kubernetes/manifests/kube-sealyun-lvscare* || :")
SSHConfig.Copy(node, "/etc/kubernetes/manifests/kube-sealyun-lvscare.yaml", []byte(yaml))
}(node)
}

Expand All @@ -156,14 +157,15 @@ func (s *SealosInstaller) sendCaAndKey(hosts []string) {
SendPackage(CertPath+"/sa.key", hosts, cert.KubeDefaultCertPath, nil, nil)
SendPackage(CertPath+"/sa.pub", hosts, cert.KubeDefaultCertPath, nil, nil)
for _, ca := range caConfigs {
SendPackage(path.Join(ca.Path,ca.BaseName+".key"), hosts, ca.DefaultPath, nil, nil)
SendPackage(path.Join(ca.Path,ca.BaseName+".crt"), hosts, ca.DefaultPath, nil, nil)
SendPackage(path.Join(ca.Path, ca.BaseName+".key"), hosts, ca.DefaultPath, nil, nil)
SendPackage(path.Join(ca.Path, ca.BaseName+".crt"), hosts, ca.DefaultPath, nil, nil)
}
}

func (s *SealosInstaller) sendCerts(hosts []string) {
certConfigs := cert.CertList(CertPath, CertEtcdPath)
for _, cert := range certConfigs {
SendPackage(path.Join(cert.Path,cert.BaseName+".key"), hosts, cert.DefaultPath, nil, nil)
SendPackage(path.Join(cert.Path,cert.BaseName+".crt"), hosts, cert.DefaultPath, nil, nil)
SendPackage(path.Join(cert.Path, cert.BaseName+".key"), hosts, cert.DefaultPath, nil, nil)
SendPackage(path.Join(cert.Path, cert.BaseName+".crt"), hosts, cert.DefaultPath, nil, nil)
}
}
20 changes: 10 additions & 10 deletions install/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,27 @@ func TestDecodeIPs(t *testing.T) {
{
"test decode ips",
args{[]string{"192.168.0.1-192.168.0.3"}},
[]string{"192.168.0.1:22","192.168.0.2:22","192.168.0.3:22"},
[]string{"192.168.0.1:22", "192.168.0.2:22", "192.168.0.3:22"},
},
{
"test decode ips",
args{[]string{"192.168.0.1","192.168.0.3:23"}},
[]string{"192.168.0.1:22","192.168.0.3:23"},
args{[]string{"192.168.0.1", "192.168.0.3:23"}},
[]string{"192.168.0.1:22", "192.168.0.3:23"},
},
{
"test decode ips",
args{[]string{"192.168.0.1-192.168.0.3:23","192.168.0.5"}},
[]string{"192.168.0.1:23","192.168.0.2:23","192.168.0.3:23","192.168.0.5:22"},
args{[]string{"192.168.0.1-192.168.0.3:23", "192.168.0.5"}},
[]string{"192.168.0.1:23", "192.168.0.2:23", "192.168.0.3:23", "192.168.0.5:22"},
},
{
"test decode ips",
args{[]string{"192.168.0.1:24","192.168.0.3"}},
[]string{"192.168.0.1:24","192.168.0.3:22"},
args{[]string{"192.168.0.1:24", "192.168.0.3"}},
[]string{"192.168.0.1:24", "192.168.0.3:22"},
},
{
"test decode ips",
args{[]string{"192.168.0.1-192.168.0.3","192.168.0.4-192.168.0.6:25", "192.168.0.7:25","192.168.0.8"}},
[]string{"192.168.0.1:22","192.168.0.2:22","192.168.0.3:22","192.168.0.4:25","192.168.0.5:25","192.168.0.6:25","192.168.0.7:25","192.168.0.8:22"},
args{[]string{"192.168.0.1-192.168.0.3", "192.168.0.4-192.168.0.6:25", "192.168.0.7:25", "192.168.0.8"}},
[]string{"192.168.0.1:22", "192.168.0.2:22", "192.168.0.3:22", "192.168.0.4:25", "192.168.0.5:25", "192.168.0.6:25", "192.168.0.7:25", "192.168.0.8:22"},
},
}
for _, tt := range tests {
Expand All @@ -121,4 +121,4 @@ func TestDecodeIPs(t *testing.T) {
}
})
}
}
}
12 changes: 5 additions & 7 deletions net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ import (
)

const (
CALICO = "calico"
FLANNEL = "flannel"
CALICO = "calico"
FLANNEL = "flannel"
defaultInterface = "eth.*|en.*"
defaultCIDR = "100.64.0.0/10"
defaultCIDR = "100.64.0.0/10"
)

type MetaData struct {
Interface string
CIDR string
// ipip mode for calico.yml
IPIP bool
IPIP bool
// MTU size
MTU string
}


// Net is CNI interface
type Net interface {
// if template is "" using default template
Expand All @@ -30,7 +29,6 @@ type Net interface {
Template() string
}


func NewNetwork(t string, metadata MetaData) Net {
switch t {
case CALICO:
Expand All @@ -45,6 +43,6 @@ func NewNetwork(t string, metadata MetaData) Net {
func render(data MetaData, temp string) string {
var b bytes.Buffer
t := template.Must(template.New("net").Parse(temp))
t.Execute(&b,&data)
t.Execute(&b, &data)
return b.String()
}
8 changes: 4 additions & 4 deletions net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (

func TestNewNetwork(t *testing.T) {
netyaml := NewNetwork("calico", MetaData{
Interface:"en.*|eth.*",
CIDR:"10.1.1.1/24",
IPIP: true,
MTU: "1440",
Interface: "en.*|eth.*",
CIDR: "10.1.1.1/24",
IPIP: true,
MTU: "1440",
}).Manifests("")
fmt.Println(netyaml)
}
8 changes: 6 additions & 2 deletions pkg/sshcmd/sshutil/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
/**
这里主要是做连接ssh操作的
*/
func (ss *SSH) Connect(host string) (*ssh.Session, error) {
func (ss *SSH) connect(host string) (*ssh.Client, error) {
auth := []ssh.AuthMethod{ss.sshAuthMethod(ss.Password, ss.PkFile)}
config := ssh.Config{
Ciphers: []string{"aes128-ctr", "aes192-ctr", "aes256-ctr", "[email protected]", "arcfour256", "arcfour128", "aes128-cbc", "3des-cbc", "aes192-cbc", "aes256-cbc"},
Expand All @@ -34,7 +34,11 @@ func (ss *SSH) Connect(host string) (*ssh.Session, error) {
}

addr := ss.addrReformat(host)
client, err := ssh.Dial("tcp", addr, clientConfig)
return ssh.Dial("tcp", addr, clientConfig)
}

func (ss *SSH) Connect(host string) (*ssh.Session, error) {
client, err := ss.connect(host)
if err != nil {
return nil, err
}
Expand Down
35 changes: 25 additions & 10 deletions pkg/sshcmd/sshutil/scp.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package sshutil

import (
"bytes"
"fmt"
"github.com/fanux/sealos/pkg/sshcmd/md5sum"
"github.com/pkg/sftp"
"github.com/wonderivan/logger"
"golang.org/x/crypto/ssh"
"io"
"net"
"os"
"strings"
Expand All @@ -32,14 +34,18 @@ func (ss *SSH) CopyForMD5(host, localFilePath, remoteFilePath, md5 string) bool
logger.Error("[ssh]md5 validate false")
return false
}

func (ss *SSH) Md5Sum(host, remoteFilePath string) string {
cmd := fmt.Sprintf("md5sum %s | cut -d\" \" -f1", remoteFilePath)
remoteMD5 := ss.CmdToString(host, cmd, "")
return remoteMD5
}

//Copy is
func (ss *SSH) Copy(host, localFilePath, remoteFilePath string) {
func (ss *SSH) Copy(host, remoteFilePath string, localFilePathOrBytes interface{}) {
var (
data io.Reader
)
sftpClient, err := ss.sftpConnect(host)
defer func() {
if r := recover(); r != nil {
Expand All @@ -50,16 +56,25 @@ func (ss *SSH) Copy(host, localFilePath, remoteFilePath string) {
panic(1)
}
defer sftpClient.Close()
srcFile, err := os.Open(localFilePath)
defer func() {
if r := recover(); r != nil {
logger.Error("[ssh][%s]scpCopy: %s", host, err)

switch v := localFilePathOrBytes.(type) {
case string:
srcFile, err := os.Open(v)
defer func() {
if r := recover(); r != nil {
logger.Error("[ssh][%s]scpCopy: %s", host, err)
}
}()
if err != nil {
panic(1)
}
}()
if err != nil {
panic(1)
defer srcFile.Close()
data = srcFile
case []byte:
data = bytes.NewReader(v)
default:
panic("must use path or []bytes")
}
defer srcFile.Close()

dstFile, err := sftpClient.Create(remoteFilePath)
defer func() {
Expand All @@ -74,7 +89,7 @@ func (ss *SSH) Copy(host, localFilePath, remoteFilePath string) {
buf := make([]byte, 100*oneMBByte) //100mb
totalMB := 0
for {
n, _ := srcFile.Read(buf)
n, _ := data.Read(buf)
if n == 0 {
break
}
Expand Down
8 changes: 4 additions & 4 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var (
Version = "latest"
Build = ""
VersionStr = fmt.Sprintf("sealos version %v, build %v %v", Version, Build, runtime.Version())
)
Version = "latest"
Build = ""
VersionStr = fmt.Sprintf("sealos version %v, build %v %v", Version, Build, runtime.Version())
)

0 comments on commit cb4e615

Please sign in to comment.