diff --git a/go.mod b/go.mod index 8ccd30a9..acb731a7 100644 --- a/go.mod +++ b/go.mod @@ -14,14 +14,14 @@ require ( github.com/onsi/ginkgo v1.12.1 github.com/onsi/gomega v1.10.1 github.com/pkg/errors v0.9.1 - github.com/pkg/sftp v1.12.0 + github.com/pkg/sftp v1.13.2 github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 github.com/sirupsen/logrus v1.7.0 github.com/spf13/cobra v1.1.1 github.com/spf13/viper v1.7.1 github.com/tmc/scp v0.0.0-20170824174625-f7b48647feef - golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 - golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e + golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 + golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e // indirect gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect helm.sh/helm/v3 v3.3.0 diff --git a/go.sum b/go.sum index 57225885..9cafb83f 100644 --- a/go.sum +++ b/go.sum @@ -597,6 +597,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/profile v1.2.1/go.mod h1:hJw3o1OdXxsrSjjVksARp5W95eeEaEfptyVZyv6JUPA= github.com/pkg/sftp v1.12.0 h1:/f3b24xrDhkhddlaobPe2JgBqfdt+gC/NYl0QY9IOuI= github.com/pkg/sftp v1.12.0/go.mod h1:fUqqXB5vEgVCZ131L+9say31RAri6aF6KDViawhxKK8= +github.com/pkg/sftp v1.13.2 h1:taJnKntsWgU+qae21Rx52lIwndAdKrj0mfUNQsz1z4Q= +github.com/pkg/sftp v1.13.2/go.mod h1:LzqnAvaD5TWeNBsZpfKxSYn1MbjWwOsCIAFFJbpIsK8= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= @@ -705,6 +707,7 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/syndtr/gocapability v0.0.0-20170704070218-db04d3cc01c8/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= @@ -783,6 +786,10 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 h1:/ZScEX8SfEmUGRHs0gxpqteO5nfNW6axyZbBdw9A12g= golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= +golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= +golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8= @@ -830,6 +837,8 @@ golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20191004110552-13f9640d40b9/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7 h1:AeiKBIuRw3UomYXSbLy0Mc2dDLfdtbT/IVn4keq83P0= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= @@ -877,8 +886,16 @@ golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7 h1:iGu644GcxtEcrInvDsQRCwJjtCIOlT2V7IRt6ah2Whw= +golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e h1:XMgFehsDnnLGtjvjOfqWSUzt0alpTR1RSEuznObga2c= +golang.org/x/sys v0.0.0-20210831042530-f4d43177bf5e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221 h1:/ZHdbVpdR/jk3g30/d4yUL0JU9kksj8+F/bnQUVLGDM= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20160726164857-2910a502d2bf/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/pkg/core/action/base.go b/pkg/core/action/base.go index 3c27904d..af9870c0 100644 --- a/pkg/core/action/base.go +++ b/pkg/core/action/base.go @@ -6,15 +6,15 @@ import ( ) type BaseAction struct { - Cache *cache.Cache - RootCache *cache.Cache - RuntimeConf connector.Runtime + Cache *cache.Cache + RootCache *cache.Cache + Runtime connector.Runtime } -func (b *BaseAction) Init(cache *cache.Cache, rootCache *cache.Cache, runtimeConf connector.Runtime) { +func (b *BaseAction) Init(cache *cache.Cache, rootCache *cache.Cache, runtime connector.Runtime) { b.Cache = cache b.RootCache = rootCache - b.RuntimeConf = runtimeConf + b.Runtime = runtime } func (b *BaseAction) Execute(runtime connector.Runtime) error { diff --git a/pkg/core/connector/dialer.go b/pkg/core/connector/dialer.go index 896ff7a0..edeafc3b 100644 --- a/pkg/core/connector/dialer.go +++ b/pkg/core/connector/dialer.go @@ -7,12 +7,12 @@ import ( type Dialer struct { lock sync.Mutex - connections map[int]Connection + connections map[string]Connection } func NewDialer() *Dialer { return &Dialer{ - connections: make(map[int]Connection), + connections: make(map[string]Connection), } } @@ -22,7 +22,7 @@ func (d *Dialer) Connect(host Host) (Connection, error) { d.lock.Lock() defer d.lock.Unlock() - conn, ok := d.connections[host.GetIndex()] + conn, ok := d.connections[host.GetName()] if !ok { opts := Cfg{ Username: host.GetUser(), @@ -37,7 +37,7 @@ func (d *Dialer) Connect(host Host) (Connection, error) { if err != nil { return nil, err } - d.connections[host.GetIndex()] = conn + d.connections[host.GetName()] = conn } return conn, nil diff --git a/pkg/core/connector/host.go b/pkg/core/connector/host.go index 9198ff4f..c1b6b0f1 100644 --- a/pkg/core/connector/host.go +++ b/pkg/core/connector/host.go @@ -10,7 +10,6 @@ type BaseHost struct { PrivateKey string `yaml:"privateKey,omitempty" json:"privateKey,omitempty"` PrivateKeyPath string `yaml:"privateKeyPath,omitempty" json:"privateKeyPath,omitempty"` Arch string `yaml:"arch,omitempty" json:"arch,omitempty"` - Index int `json:"-"` Roles []string `json:"-"` RoleTable map[string]bool `json:"-"` } @@ -79,14 +78,6 @@ func (b *BaseHost) SetArch(arch string) { b.Arch = arch } -func (b *BaseHost) GetIndex() int { - return b.Index -} - -func (b *BaseHost) SetIndex(i int) { - b.Index = i -} - func (b *BaseHost) GetRoles() []string { return b.Roles } diff --git a/pkg/core/connector/interface.go b/pkg/core/connector/interface.go index 3200731a..7f1a0f40 100644 --- a/pkg/core/connector/interface.go +++ b/pkg/core/connector/interface.go @@ -52,8 +52,6 @@ type Host interface { SetPrivateKeyPath(path string) GetArch() string SetArch(arch string) - GetIndex() int - SetIndex(i int) GetRoles() []string SetRoles(roles []string) IsRole(role string) bool diff --git a/pkg/core/connector/runner.go b/pkg/core/connector/runner.go index 225ebf14..adbfce2e 100644 --- a/pkg/core/connector/runner.go +++ b/pkg/core/connector/runner.go @@ -45,10 +45,10 @@ func (r *Runner) Exec(cmd string, printOutput bool) (string, string, int, error) stdout, stderr, code, err := r.Conn.Exec(cmd) if printOutput { if stdout != "" { - logger.Log.Infof("[stdout]: %s", stdout) + logger.Log.Infof("stdout: [%s]\n%s", r.Host.GetName(), stdout) } if stderr != "" { - logger.Log.Infof("[stderr]: %s", stderr) + logger.Log.Infof("stderr: [%s]\n%s", r.Host.GetName(), stderr) } } if err != nil { @@ -79,7 +79,7 @@ func (r *Runner) Fetch(local, remote string) error { } if err := r.Conn.Fetch(local, remote); err != nil { - logger.Log.Errorf("fetch remote file %s to local %s failed: %v", remote, local, err) + logger.Log.Debugf("fetch remote file %s to local %s failed: %v", remote, local, err) return err } logger.Log.Debugf("fetch remote file %s to local %s success", remote, local) @@ -92,7 +92,7 @@ func (r *Runner) Scp(local, remote string) error { } if err := r.Conn.Scp(local, remote); err != nil { - logger.Log.Errorf("scp local file %s to remote %s failed: %v", local, remote, err) + logger.Log.Debugf("scp local file %s to remote %s failed: %v", local, remote, err) return err } logger.Log.Debugf("scp local file %s to remote %s success", local, remote) @@ -106,6 +106,7 @@ func (r *Runner) SudoScp(local, remote string) error { // scp to tmp dir remoteTmp := filepath.Join(common.TmpDir, remote) + //remoteTmp := remote if err := r.Scp(local, remoteTmp); err != nil { return err } @@ -135,7 +136,7 @@ func (r *Runner) DirExist(remote string) (bool, error) { ok, err := r.Conn.RemoteDirExist(remote) if err != nil { - logger.Log.Errorf("check remote dir exist failed: %v", err) + logger.Log.Debugf("check remote dir exist failed: %v", err) return false, err } logger.Log.Debugf("check remote dir exist: %v", ok) diff --git a/pkg/core/connector/runtime.go b/pkg/core/connector/runtime.go index 34061562..907f688f 100644 --- a/pkg/core/connector/runtime.go +++ b/pkg/core/connector/runtime.go @@ -1,14 +1,22 @@ package connector type BaseRuntime struct { - ObjName string - connector Connector - runner *Runner - DownloadCommand func(path, url string) string - WorkDir string - ClusterHosts []string - AllHosts []Host - RoleHosts map[string][]Host + ObjName string + connector Connector + runner *Runner + workDir string + allHosts []Host + roleHosts map[string][]Host +} + +func NewBaseRuntime(name string, connector Connector, workDir string) BaseRuntime { + return BaseRuntime{ + ObjName: name, + connector: connector, + workDir: workDir, + allHosts: make([]Host, 0, 0), + roleHosts: make(map[string][]Host), + } } func (b *BaseRuntime) GetRunner() *Runner { @@ -28,23 +36,23 @@ func (b *BaseRuntime) SetConnector(c Connector) { } func (b *BaseRuntime) GetWorkDir() string { - return b.WorkDir + return b.workDir } func (b *BaseRuntime) SetWorkDir(str string) { - b.WorkDir = str + b.workDir = str } func (b *BaseRuntime) GetAllHosts() []Host { - return b.AllHosts + return b.allHosts } func (b *BaseRuntime) SetAllHosts(hosts []Host) { - b.AllHosts = hosts + b.allHosts = hosts } func (b *BaseRuntime) GetHostsByRole(role string) []Host { - return b.RoleHosts[role] + return b.roleHosts[role] } func (b *BaseRuntime) RemoteHost() Host { @@ -57,24 +65,24 @@ func (b *BaseRuntime) Copy() Runtime { } func (b *BaseRuntime) GenerateRoleMap() { - for i := range b.AllHosts { - b.AppendRoleMap(b.AllHosts[i]) + for i := range b.allHosts { + b.AppendRoleMap(b.allHosts[i]) } } func (b *BaseRuntime) AppendHost(host Host) { - b.AllHosts = append(b.AllHosts, host) + b.allHosts = append(b.allHosts, host) } func (b *BaseRuntime) AppendRoleMap(host Host) { for _, r := range host.GetRoles() { - if hosts, ok := b.RoleHosts[r]; ok { + if hosts, ok := b.roleHosts[r]; ok { hosts = append(hosts, host) - b.RoleHosts[r] = hosts + b.roleHosts[r] = hosts } else { first := make([]Host, 0, 0) first = append(first, host) - b.RoleHosts[r] = first + b.roleHosts[r] = first } } } diff --git a/pkg/core/connector/ssh.go b/pkg/core/connector/ssh.go index 8f220169..40483d70 100644 --- a/pkg/core/connector/ssh.go +++ b/pkg/core/connector/ssh.go @@ -125,6 +125,11 @@ func NewConnection(dialer *Dialer, cfg Cfg) (Connection, error) { if cfg.Bastion == "" { sshConn.sshclient = client + sftpClient, err := sftp.NewClient(sshConn.sshclient) + if err != nil { + return nil, errors.Wrapf(err, "new sftp client failed: %v", err) + } + sshConn.sftpclient = sftpClient return sshConn, nil } @@ -387,7 +392,7 @@ func (c *connection) copyFileToRemote(src, dst string) error { } defer srcFile.Close() // the dst file mod will be 0666 - dstFile, err := c.sftpclient.Create(dst) + dstFile, err := c.sftpclient.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC) if err != nil { return err } @@ -454,8 +459,7 @@ func (c *connection) RemoteDirExist(dst string) (bool, error) { } func (c *connection) MkDirAll(path string) error { - remotePath := filepath.Dir(path) - mkDstDir := fmt.Sprintf("mkdir -p %s || true", remotePath) + mkDstDir := fmt.Sprintf("mkdir -p %s || true", path) if _, _, _, err := c.Exec(SudoPrefix(mkDstDir)); err != nil { return err } diff --git a/pkg/core/modules/task.go b/pkg/core/modules/task.go index ca9d70a3..81e3ce34 100644 --- a/pkg/core/modules/task.go +++ b/pkg/core/modules/task.go @@ -77,7 +77,6 @@ func (t *Task) Execute() *ending.TaskResult { return t.TaskResult } -// todo: 待上环境测试,多节点报错的情况 func (t *Task) RunWithTimeout(ctx context.Context, runtime connector.Runtime, host connector.Host, index int, wg *sync.WaitGroup, pool chan struct{}) { @@ -166,8 +165,8 @@ func (t *Task) WhenWithRetry(runtime connector.Runtime) (bool, error) { err := fmt.Errorf("pre-check exec failed after %d retires", t.Retry) for i := 0; i < t.Retry; i++ { if res, e := t.When(runtime); e != nil { + logger.Log.Errorf("message: [%s]\n%s", runtime.RemoteHost().GetName(), e.Error()) logger.Log.Infof("retry: [%s]", runtime.GetRunner().Host.GetName()) - logger.Log.Error(e) time.Sleep(t.Delay) continue } else { @@ -181,12 +180,12 @@ func (t *Task) WhenWithRetry(runtime connector.Runtime) (bool, error) { } func (t *Task) ExecuteWithRetry(runtime connector.Runtime) error { - err := fmt.Errorf("action exec failed after %d retires", t.Retry) + err := fmt.Errorf("[%s] exec failed after %d retires", t.Name, t.Retry) for i := 0; i < t.Retry; i++ { e := t.Action.Execute(runtime) if e != nil { + logger.Log.Errorf("message: [%s]\n%s", runtime.RemoteHost().GetName(), e.Error()) logger.Log.Infof("retry: [%s]", runtime.GetRunner().Host.GetName()) - logger.Log.Error(e) time.Sleep(t.Delay) continue } else { @@ -209,17 +208,21 @@ func (t *Task) Default() { return } + if t.Prepare == nil { + t.Prepare = new(prepare.BasePrepare) + } + if t.Action == nil { t.TaskResult.AppendErr(nil, errors.New("the action is nil")) return } if t.Retry < 1 { - t.Retry = 1 + t.Retry = 3 } if t.Delay <= 0 { - t.Delay = 3 * time.Second + t.Delay = 5 * time.Second } if t.Concurrency <= 0 || t.Concurrency > 1 { diff --git a/pkg/core/pipeline/pipeline.go b/pkg/core/pipeline/pipeline.go index 0f81558e..7ee7b5dc 100644 --- a/pkg/core/pipeline/pipeline.go +++ b/pkg/core/pipeline/pipeline.go @@ -38,8 +38,8 @@ func (p *Pipeline) RunModule(m modules.Module) error { moduleCache := p.newModuleCache() defer p.releaseModuleCache(moduleCache) m.Default(p.Runtime, p.PipelineCache, moduleCache) - m.Init() m.AutoAssert() + m.Init() m.Slogan() switch m.Is() { case modules.TaskModuleType: diff --git a/pkg/core/prepare/base.go b/pkg/core/prepare/base.go index 9212a93e..b1722d9c 100644 --- a/pkg/core/prepare/base.go +++ b/pkg/core/prepare/base.go @@ -6,15 +6,15 @@ import ( ) type BasePrepare struct { - Cache *cache.Cache - RootCache *cache.Cache - RuntimeConf connector.Runtime + Cache *cache.Cache + RootCache *cache.Cache + Runtime connector.Runtime } -func (b *BasePrepare) Init(cache *cache.Cache, rootCache *cache.Cache, runtimeConf connector.Runtime) { +func (b *BasePrepare) Init(cache *cache.Cache, rootCache *cache.Cache, runtime connector.Runtime) { b.Cache = cache b.RootCache = rootCache - b.RuntimeConf = runtimeConf + b.Runtime = runtime } func (b *BasePrepare) PreCheck(runtime connector.Runtime) (bool, error) { diff --git a/pkg/core/prepare/interface.go b/pkg/core/prepare/interface.go index f39d1b55..60549033 100644 --- a/pkg/core/prepare/interface.go +++ b/pkg/core/prepare/interface.go @@ -7,6 +7,6 @@ import ( type Prepare interface { PreCheck(runtime connector.Runtime) (bool, error) - Init(cache *cache.Cache, rootCache *cache.Cache, runtimeConf connector.Runtime) + Init(cache *cache.Cache, rootCache *cache.Cache, runtime connector.Runtime) AutoAssert() } diff --git a/pkg/core/util/file.go b/pkg/core/util/file.go index 8e418d2e..57ae925e 100644 --- a/pkg/core/util/file.go +++ b/pkg/core/util/file.go @@ -11,6 +11,30 @@ import ( "path/filepath" ) +func IsExist(path string) bool { + _, err := os.Stat(path) + if err != nil { + if os.IsExist(err) { + return true + } + if os.IsNotExist(err) { + return false + } + return false + } + return true +} + +func CreateDir(path string) error { + if IsExist(path) == false { + err := os.MkdirAll(path, os.ModePerm) + if err != nil { + return err + } + } + return nil +} + func IsDir(path string) bool { s, err := os.Stat(path) if err != nil { diff --git a/pkg/core/util/ip.go b/pkg/core/util/ip.go new file mode 100644 index 00000000..bba72ecf --- /dev/null +++ b/pkg/core/util/ip.go @@ -0,0 +1,150 @@ +package util + +import ( + "encoding/binary" + "github.com/kubesphere/kubekey/pkg/core/logger" + "github.com/pkg/errors" + "net" + "strconv" + "strings" +) + +func ParseIp(ip string) []string { + var availableIPs []string + // if ip is "1.1.1.1/",trim / + ip = strings.TrimRight(ip, "/") + if strings.Contains(ip, "/") == true { + if strings.Contains(ip, "/32") == true { + aip := strings.Replace(ip, "/32", "", -1) + availableIPs = append(availableIPs, aip) + } else { + availableIPs = GetAvailableIP(ip) + } + } else if strings.Contains(ip, "-") == true { + ipRange := strings.SplitN(ip, "-", 2) + availableIPs = GetAvailableIPRange(ipRange[0], ipRange[1]) + } else { + availableIPs = append(availableIPs, ip) + } + return availableIPs +} + +func GetAvailableIPRange(ipStart, ipEnd string) []string { + var availableIPs []string + + firstIP := net.ParseIP(ipStart) + endIP := net.ParseIP(ipEnd) + if firstIP.To4() == nil || endIP.To4() == nil { + return availableIPs + } + firstIPNum := ipToInt(firstIP.To4()) + EndIPNum := ipToInt(endIP.To4()) + pos := int32(1) + + newNum := firstIPNum + + for newNum <= EndIPNum { + availableIPs = append(availableIPs, intToIP(newNum).String()) + newNum = newNum + pos + } + return availableIPs +} + +func GetAvailableIP(ipAndMask string) []string { + var availableIPs []string + + ipAndMask = strings.TrimSpace(ipAndMask) + ipAndMask = IPAddressToCIDR(ipAndMask) + _, ipnet, _ := net.ParseCIDR(ipAndMask) + + firstIP, _ := networkRange(ipnet) + ipNum := ipToInt(firstIP) + size := networkSize(ipnet.Mask) + pos := int32(1) + max := size - 2 // -1 for the broadcast address, -1 for the gateway address + + var newNum int32 + for attempt := int32(0); attempt < max; attempt++ { + newNum = ipNum + pos + pos = pos%max + 1 + availableIPs = append(availableIPs, intToIP(newNum).String()) + } + return availableIPs +} + +func IPAddressToCIDR(ipAddress string) string { + if strings.Contains(ipAddress, "/") == true { + ipAndMask := strings.Split(ipAddress, "/") + ip := ipAndMask[0] + mask := ipAndMask[1] + if strings.Contains(mask, ".") == true { + mask = IPMaskStringToCIDR(mask) + } + return ip + "/" + mask + } else { + return ipAddress + } +} + +func IPMaskStringToCIDR(netmask string) string { + netmaskList := strings.Split(netmask, ".") + var mint []int + for _, v := range netmaskList { + strv, _ := strconv.Atoi(v) + mint = append(mint, strv) + } + myIPMask := net.IPv4Mask(byte(mint[0]), byte(mint[1]), byte(mint[2]), byte(mint[3])) + ones, _ := myIPMask.Size() + return strconv.Itoa(ones) +} + +func networkRange(network *net.IPNet) (net.IP, net.IP) { + netIP := network.IP.To4() + firstIP := netIP.Mask(network.Mask) + lastIP := net.IPv4(0, 0, 0, 0).To4() + for i := 0; i < len(lastIP); i++ { + lastIP[i] = netIP[i] | ^network.Mask[i] + } + return firstIP, lastIP +} + +func networkSize(mask net.IPMask) int32 { + m := net.IPv4Mask(0, 0, 0, 0) + for i := 0; i < net.IPv4len; i++ { + m[i] = ^mask[i] + } + return int32(binary.BigEndian.Uint32(m)) + 1 +} + +func ipToInt(ip net.IP) int32 { + return int32(binary.BigEndian.Uint32(ip.To4())) +} + +func intToIP(n int32) net.IP { + b := make([]byte, 4) + binary.BigEndian.PutUint32(b, uint32(n)) + return net.IP(b) +} + +func GetLocalIP() (string, error) { + addrs, err := net.InterfaceAddrs() + if err != nil { + return "", err + } + for _, addr := range addrs { + if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil && ipnet.IP.IsGlobalUnicast() { + return ipnet.IP.String(), nil + } + } + } + return "", errors.New("valid local IP not found!") +} + +func LocalIP() string { + localIp, err := GetLocalIP() + if err != nil { + logger.Log.Fatalf("Failed to get Local IP: %v", err) + } + return localIp +} diff --git a/pkg/core/util/util.go b/pkg/core/util/util.go index 92deaa58..1a6128aa 100644 --- a/pkg/core/util/util.go +++ b/pkg/core/util/util.go @@ -18,47 +18,19 @@ package util import ( "bytes" - "encoding/binary" "fmt" - "github.com/kubesphere/kubekey/pkg/core/logger" "github.com/pkg/errors" "math" - "net" "os" "os/exec" "os/user" "regexp" "runtime" "sort" - "strconv" "strings" "text/template" ) -func IsExist(path string) bool { - _, err := os.Stat(path) - if err != nil { - if os.IsExist(err) { - return true - } - if os.IsNotExist(err) { - return false - } - return false - } - return true -} - -func CreateDir(path string) error { - if IsExist(path) == false { - err := os.MkdirAll(path, os.ModePerm) - if err != nil { - return err - } - } - return nil -} - type Data map[string]interface{} // Render text template with given `variables` Render-context @@ -72,146 +44,6 @@ func Render(tmpl *template.Template, variables map[string]interface{}) (string, return buf.String(), nil } -func ParseIp(ip string) []string { - var availableIPs []string - // if ip is "1.1.1.1/",trim / - ip = strings.TrimRight(ip, "/") - if strings.Contains(ip, "/") == true { - if strings.Contains(ip, "/32") == true { - aip := strings.Replace(ip, "/32", "", -1) - availableIPs = append(availableIPs, aip) - } else { - availableIPs = GetAvailableIP(ip) - } - } else if strings.Contains(ip, "-") == true { - ipRange := strings.SplitN(ip, "-", 2) - availableIPs = GetAvailableIPRange(ipRange[0], ipRange[1]) - } else { - availableIPs = append(availableIPs, ip) - } - return availableIPs -} - -func GetAvailableIPRange(ipStart, ipEnd string) []string { - var availableIPs []string - - firstIP := net.ParseIP(ipStart) - endIP := net.ParseIP(ipEnd) - if firstIP.To4() == nil || endIP.To4() == nil { - return availableIPs - } - firstIPNum := ipToInt(firstIP.To4()) - EndIPNum := ipToInt(endIP.To4()) - pos := int32(1) - - newNum := firstIPNum - - for newNum <= EndIPNum { - availableIPs = append(availableIPs, intToIP(newNum).String()) - newNum = newNum + pos - } - return availableIPs -} - -func GetAvailableIP(ipAndMask string) []string { - var availableIPs []string - - ipAndMask = strings.TrimSpace(ipAndMask) - ipAndMask = IPAddressToCIDR(ipAndMask) - _, ipnet, _ := net.ParseCIDR(ipAndMask) - - firstIP, _ := networkRange(ipnet) - ipNum := ipToInt(firstIP) - size := networkSize(ipnet.Mask) - pos := int32(1) - max := size - 2 // -1 for the broadcast address, -1 for the gateway address - - var newNum int32 - for attempt := int32(0); attempt < max; attempt++ { - newNum = ipNum + pos - pos = pos%max + 1 - availableIPs = append(availableIPs, intToIP(newNum).String()) - } - return availableIPs -} - -func IPAddressToCIDR(ipAddress string) string { - if strings.Contains(ipAddress, "/") == true { - ipAndMask := strings.Split(ipAddress, "/") - ip := ipAndMask[0] - mask := ipAndMask[1] - if strings.Contains(mask, ".") == true { - mask = IPMaskStringToCIDR(mask) - } - return ip + "/" + mask - } else { - return ipAddress - } -} - -func IPMaskStringToCIDR(netmask string) string { - netmaskList := strings.Split(netmask, ".") - var mint []int - for _, v := range netmaskList { - strv, _ := strconv.Atoi(v) - mint = append(mint, strv) - } - myIPMask := net.IPv4Mask(byte(mint[0]), byte(mint[1]), byte(mint[2]), byte(mint[3])) - ones, _ := myIPMask.Size() - return strconv.Itoa(ones) -} - -func networkRange(network *net.IPNet) (net.IP, net.IP) { - netIP := network.IP.To4() - firstIP := netIP.Mask(network.Mask) - lastIP := net.IPv4(0, 0, 0, 0).To4() - for i := 0; i < len(lastIP); i++ { - lastIP[i] = netIP[i] | ^network.Mask[i] - } - return firstIP, lastIP -} - -func networkSize(mask net.IPMask) int32 { - m := net.IPv4Mask(0, 0, 0, 0) - for i := 0; i < net.IPv4len; i++ { - m[i] = ^mask[i] - } - return int32(binary.BigEndian.Uint32(m)) + 1 -} - -func ipToInt(ip net.IP) int32 { - return int32(binary.BigEndian.Uint32(ip.To4())) -} - -func intToIP(n int32) net.IP { - b := make([]byte, 4) - binary.BigEndian.PutUint32(b, uint32(n)) - return net.IP(b) -} - -func GetLocalIP() (string, error) { - addrs, err := net.InterfaceAddrs() - if err != nil { - return "", err - } - for _, addr := range addrs { - if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil && ipnet.IP.IsGlobalUnicast() { - return ipnet.IP.String(), nil - } - } - } - return "", errors.New("valid local IP not found!") -} - -func LocalIP() string { - localIp, err := GetLocalIP() - if err != nil { - logger.Log.Fatalf("Failed to get Local IP: %v", err) - } - return localIp -} - // Home returns the home directory for the executing user. func Home() (string, error) { u, err := user.Current() diff --git a/pkg/pipelines/binaries/k3s.go b/pkg/pipelines/binaries/k3s.go index 5abeb80e..7357d476 100644 --- a/pkg/pipelines/binaries/k3s.go +++ b/pkg/pipelines/binaries/k3s.go @@ -14,7 +14,7 @@ import ( ) // K3sFilesDownloadHTTP defines the kubernetes' binaries that need to be downloaded in advance and downloads them. -func K3sFilesDownloadHTTP(kubeConf *common.KubeRuntime, filepath, version, arch string) error { +func K3sFilesDownloadHTTP(kubeConf *common.KubeConf, filepath, version, arch string) error { kkzone := os.Getenv("KKZONE") etcd := files.KubeBinary{Name: "etcd", Arch: arch, Version: kubekeyapiv1alpha1.DefaultEtcdVersion} k3s := files.KubeBinary{Name: "k3s", Arch: arch, Version: version} @@ -31,19 +31,19 @@ func K3sFilesDownloadHTTP(kubeConf *common.KubeRuntime, filepath, version, arch k3s.Url = fmt.Sprintf("https://kubernetes-release.pek3b.qingstor.com/k3s/releases/download/%s+k3s1/linux/%s/k3s", k3s.Version, k3s.Arch) kubecni.Url = fmt.Sprintf("https://containernetworking.pek3b.qingstor.com/plugins/releases/download/%s/cni-plugins-linux-%s-%s.tgz", kubecni.Version, kubecni.Arch, kubecni.Version) helm.Url = fmt.Sprintf("https://kubernetes-helm.pek3b.qingstor.com/linux-%s/%s/helm", helm.Arch, helm.Version) - helm.GetCmd = kubeConf.DownloadCommand(helm.Path, helm.Url) + helm.GetCmd = kubeConf.Arg.DownloadCommand(helm.Path, helm.Url) } else { etcd.Url = fmt.Sprintf("https://github.com/coreos/etcd/releases/download/%s/etcd-%s-linux-%s.tar.gz", etcd.Version, etcd.Version, etcd.Arch) k3s.Url = fmt.Sprintf("https://github.com/rancher/k3s/releases/download/%s+k3s1/k3s", k3s.Version) kubecni.Url = fmt.Sprintf("https://github.com/containernetworking/plugins/releases/download/%s/cni-plugins-linux-%s-%s.tgz", kubecni.Version, kubecni.Arch, kubecni.Version) helm.Url = fmt.Sprintf("https://get.helm.sh/helm-%s-linux-%s.tar.gz", helm.Version, helm.Arch) - getCmd := kubeConf.DownloadCommand(fmt.Sprintf("%s/helm-%s-linux-%s.tar.gz", filepath, helm.Version, helm.Arch), helm.Url) + getCmd := kubeConf.Arg.DownloadCommand(fmt.Sprintf("%s/helm-%s-linux-%s.tar.gz", filepath, helm.Version, helm.Arch), helm.Url) helm.GetCmd = fmt.Sprintf("%s && cd %s && tar -zxf helm-%s-linux-%s.tar.gz && mv linux-%s/helm . && rm -rf *linux-%s*", getCmd, filepath, helm.Version, helm.Arch, helm.Arch, helm.Arch) } - k3s.GetCmd = kubeConf.DownloadCommand(k3s.Path, k3s.Url) - kubecni.GetCmd = kubeConf.DownloadCommand(kubecni.Path, kubecni.Url) - etcd.GetCmd = kubeConf.DownloadCommand(etcd.Path, etcd.Url) + k3s.GetCmd = kubeConf.Arg.DownloadCommand(k3s.Path, k3s.Url) + kubecni.GetCmd = kubeConf.Arg.DownloadCommand(kubecni.Path, kubecni.Url) + etcd.GetCmd = kubeConf.Arg.DownloadCommand(etcd.Path, etcd.Url) binaries := []files.KubeBinary{k3s, helm, kubecni, etcd} diff --git a/pkg/pipelines/binaries/kubernetes.go b/pkg/pipelines/binaries/kubernetes.go index e9484660..a6c86f5e 100644 --- a/pkg/pipelines/binaries/kubernetes.go +++ b/pkg/pipelines/binaries/kubernetes.go @@ -16,7 +16,7 @@ import ( ) // K8sFilesDownloadHTTP defines the kubernetes' binaries that need to be downloaded in advance and downloads them. -func K8sFilesDownloadHTTP(kubeConf *common.KubeRuntime, filepath, version, arch string) error { +func K8sFilesDownloadHTTP(kubeConf *common.KubeConf, filepath, version, arch string) error { kkzone := os.Getenv("KKZONE") etcd := files.KubeBinary{Name: "etcd", Arch: arch, Version: kubekeyapiv1alpha1.DefaultEtcdVersion} kubeadm := files.KubeBinary{Name: "kubeadm", Arch: arch, Version: version} @@ -39,7 +39,7 @@ func K8sFilesDownloadHTTP(kubeConf *common.KubeRuntime, filepath, version, arch kubectl.Url = fmt.Sprintf("https://kubernetes-release.pek3b.qingstor.com/release/%s/bin/linux/%s/kubectl", kubectl.Version, kubectl.Arch) kubecni.Url = fmt.Sprintf("https://containernetworking.pek3b.qingstor.com/plugins/releases/download/%s/cni-plugins-linux-%s-%s.tgz", kubecni.Version, kubecni.Arch, kubecni.Version) helm.Url = fmt.Sprintf("https://kubernetes-helm.pek3b.qingstor.com/linux-%s/%s/helm", helm.Arch, helm.Version) - helm.GetCmd = kubeConf.DownloadCommand(helm.Path, helm.Url) + helm.GetCmd = kubeConf.Arg.DownloadCommand(helm.Path, helm.Url) } else { etcd.Url = fmt.Sprintf("https://github.com/coreos/etcd/releases/download/%s/etcd-%s-linux-%s.tar.gz", etcd.Version, etcd.Version, etcd.Arch) kubeadm.Url = fmt.Sprintf("https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/%s/kubeadm", kubeadm.Version, kubeadm.Arch) @@ -47,15 +47,15 @@ func K8sFilesDownloadHTTP(kubeConf *common.KubeRuntime, filepath, version, arch kubectl.Url = fmt.Sprintf("https://storage.googleapis.com/kubernetes-release/release/%s/bin/linux/%s/kubectl", kubectl.Version, kubectl.Arch) kubecni.Url = fmt.Sprintf("https://github.com/containernetworking/plugins/releases/download/%s/cni-plugins-linux-%s-%s.tgz", kubecni.Version, kubecni.Arch, kubecni.Version) helm.Url = fmt.Sprintf("https://get.helm.sh/helm-%s-linux-%s.tar.gz", helm.Version, helm.Arch) - getCmd := kubeConf.DownloadCommand(fmt.Sprintf("%s/helm-%s-linux-%s.tar.gz", filepath, helm.Version, helm.Arch), helm.Url) + getCmd := kubeConf.Arg.DownloadCommand(fmt.Sprintf("%s/helm-%s-linux-%s.tar.gz", filepath, helm.Version, helm.Arch), helm.Url) helm.GetCmd = fmt.Sprintf("%s && cd %s && tar -zxf helm-%s-linux-%s.tar.gz && mv linux-%s/helm . && rm -rf *linux-%s*", getCmd, filepath, helm.Version, helm.Arch, helm.Arch, helm.Arch) } - kubeadm.GetCmd = kubeConf.DownloadCommand(kubeadm.Path, kubeadm.Url) - kubelet.GetCmd = kubeConf.DownloadCommand(kubelet.Path, kubelet.Url) - kubectl.GetCmd = kubeConf.DownloadCommand(kubectl.Path, kubectl.Url) - kubecni.GetCmd = kubeConf.DownloadCommand(kubecni.Path, kubecni.Url) - etcd.GetCmd = kubeConf.DownloadCommand(etcd.Path, etcd.Url) + kubeadm.GetCmd = kubeConf.Arg.DownloadCommand(kubeadm.Path, kubeadm.Url) + kubelet.GetCmd = kubeConf.Arg.DownloadCommand(kubelet.Path, kubelet.Url) + kubectl.GetCmd = kubeConf.Arg.DownloadCommand(kubectl.Path, kubectl.Url) + kubecni.GetCmd = kubeConf.Arg.DownloadCommand(kubecni.Path, kubecni.Url) + etcd.GetCmd = kubeConf.Arg.DownloadCommand(etcd.Path, etcd.Url) binaries := []files.KubeBinary{kubeadm, kubelet, kubectl, helm, kubecni, etcd} @@ -95,7 +95,7 @@ func K8sFilesDownloadHTTP(kubeConf *common.KubeRuntime, filepath, version, arch if kubeConf.Cluster.KubeSphere.Version == "v2.1.1" { logger.Log.Infoln(fmt.Sprintf("Downloading %s ...", "helm2")) if util.IsExist(fmt.Sprintf("%s/helm2", filepath)) == false { - cmd := kubeConf.DownloadCommand(fmt.Sprintf("%s/helm2", filepath), fmt.Sprintf("https://kubernetes-helm.pek3b.qingstor.com/linux-%s/%s/helm", helm.Arch, "v2.16.9")) + cmd := kubeConf.Arg.DownloadCommand(fmt.Sprintf("%s/helm2", filepath), fmt.Sprintf("https://kubernetes-helm.pek3b.qingstor.com/linux-%s/%s/helm", helm.Arch, "v2.16.9")) if output, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput(); err != nil { fmt.Println(string(output)) return errors.Wrap(err, "Failed to download helm2 binary") diff --git a/pkg/pipelines/common/const.go b/pkg/pipelines/common/common.go similarity index 100% rename from pkg/pipelines/common/const.go rename to pkg/pipelines/common/common.go diff --git a/pkg/pipelines/common/kube_action.go b/pkg/pipelines/common/kube_action.go index 1c360820..504971f4 100644 --- a/pkg/pipelines/common/kube_action.go +++ b/pkg/pipelines/common/kube_action.go @@ -4,10 +4,19 @@ import "github.com/kubesphere/kubekey/pkg/core/action" type KubeAction struct { action.BaseAction - KubeConf *KubeRuntime + KubeConf *KubeConf } func (k *KubeAction) AutoAssert() { - conf := k.RuntimeConf.(*KubeRuntime) + kubeRuntime := k.Runtime.(*KubeRuntime) + conf := &KubeConf{ + ClusterHosts: kubeRuntime.ClusterHosts, + Cluster: kubeRuntime.Cluster, + Kubeconfig: kubeRuntime.Kubeconfig, + Conditions: kubeRuntime.Conditions, + ClientSet: kubeRuntime.ClientSet, + Arg: kubeRuntime.Arg, + } + k.KubeConf = conf } diff --git a/pkg/pipelines/common/kube_module.go b/pkg/pipelines/common/kube_module.go index 59f8c12d..f35a69a8 100644 --- a/pkg/pipelines/common/kube_module.go +++ b/pkg/pipelines/common/kube_module.go @@ -1,25 +1,54 @@ package common import ( + kubekeyapiv1alpha1 "github.com/kubesphere/kubekey/apis/kubekey/v1alpha1" + kubekeyclientset "github.com/kubesphere/kubekey/clients/clientset/versioned" "github.com/kubesphere/kubekey/pkg/core/modules" ) +type KubeConf struct { + ClusterHosts []string + Cluster *kubekeyapiv1alpha1.ClusterSpec + Kubeconfig string + Conditions []kubekeyapiv1alpha1.Condition + ClientSet *kubekeyclientset.Clientset + Arg Argument +} + type KubeModule struct { modules.BaseTaskModule - KubeConf *KubeRuntime + KubeConf *KubeConf } func (k *KubeModule) AutoAssert() { - conf := k.Runtime.(*KubeRuntime) + kubeRuntime := k.Runtime.(*KubeRuntime) + conf := &KubeConf{ + ClusterHosts: kubeRuntime.ClusterHosts, + Cluster: kubeRuntime.Cluster, + Kubeconfig: kubeRuntime.Kubeconfig, + Conditions: kubeRuntime.Conditions, + ClientSet: kubeRuntime.ClientSet, + Arg: kubeRuntime.Arg, + } + k.KubeConf = conf } type KubeCustomModule struct { modules.CustomModule - KubeConf *KubeRuntime + KubeConf *KubeConf } func (k *KubeCustomModule) AutoAssert() { - conf := k.Runtime.(*KubeRuntime) + kubeRuntime := k.Runtime.(*KubeRuntime) + conf := &KubeConf{ + ClusterHosts: kubeRuntime.ClusterHosts, + Cluster: kubeRuntime.Cluster, + Kubeconfig: kubeRuntime.Kubeconfig, + Conditions: kubeRuntime.Conditions, + ClientSet: kubeRuntime.ClientSet, + Arg: kubeRuntime.Arg, + } + k.KubeConf = conf } diff --git a/pkg/pipelines/common/kube_prepare.go b/pkg/pipelines/common/kube_prepare.go index 45713128..00c3015d 100644 --- a/pkg/pipelines/common/kube_prepare.go +++ b/pkg/pipelines/common/kube_prepare.go @@ -7,11 +7,20 @@ import ( type KubePrepare struct { prepare.BasePrepare - KubeConf *KubeRuntime + KubeConf *KubeConf } func (k *KubePrepare) AutoAssert() { - conf := k.RuntimeConf.(*KubeRuntime) + kubeRuntime := k.Runtime.(*KubeRuntime) + conf := &KubeConf{ + ClusterHosts: kubeRuntime.ClusterHosts, + Cluster: kubeRuntime.Cluster, + Kubeconfig: kubeRuntime.Kubeconfig, + Conditions: kubeRuntime.Conditions, + ClientSet: kubeRuntime.ClientSet, + Arg: kubeRuntime.Arg, + } + k.KubeConf = conf } diff --git a/pkg/pipelines/common/kube_runtime.go b/pkg/pipelines/common/kube_runtime.go index 4801758a..de3d6aed 100644 --- a/pkg/pipelines/common/kube_runtime.go +++ b/pkg/pipelines/common/kube_runtime.go @@ -14,11 +14,12 @@ import ( type KubeRuntime struct { connector.BaseRuntime - Cluster *kubekeyapiv1alpha1.ClusterSpec - Kubeconfig string - Conditions []kubekeyapiv1alpha1.Condition - ClientSet *kubekeyclientset.Clientset - Arg Argument + ClusterHosts []string + Cluster *kubekeyapiv1alpha1.ClusterSpec + Kubeconfig string + Conditions []kubekeyapiv1alpha1.Condition + ClientSet *kubekeyclientset.Clientset + Arg Argument } type Argument struct { @@ -58,14 +59,7 @@ func NewKubeRuntime(flag string, arg Argument) (*KubeRuntime, error) { clientset = c } - base := connector.BaseRuntime{ - ObjName: cluster.Name, - ClusterHosts: generateHosts(hostGroups, defaultCluster), - WorkDir: generateWorkDir(), - AllHosts: make([]connector.Host, 0, 0), - RoleHosts: make(map[string][]connector.Host), - } - base.SetConnector(connector.NewDialer()) + base := connector.NewBaseRuntime(cluster.Name, connector.NewDialer(), generateWorkDir()) for _, v := range hostGroups.All { host := ToHosts(v) if v.IsMaster { @@ -83,9 +77,10 @@ func NewKubeRuntime(flag string, arg Argument) (*KubeRuntime, error) { } r := &KubeRuntime{ - Cluster: defaultCluster, - ClientSet: clientset, - Arg: arg, + ClusterHosts: generateHosts(hostGroups, defaultCluster), + Cluster: defaultCluster, + ClientSet: clientset, + Arg: arg, } r.BaseRuntime = base diff --git a/pkg/pipelines/initialization/module.go b/pkg/pipelines/initialization/module.go index 52fbfff4..9147e8cc 100644 --- a/pkg/pipelines/initialization/module.go +++ b/pkg/pipelines/initialization/module.go @@ -14,6 +14,7 @@ import ( "github.com/modood/table" "github.com/pkg/errors" "os" + "path/filepath" "strings" ) @@ -160,7 +161,7 @@ func (c *ConfigureOSModule) Init() { Hosts: c.Runtime.GetAllHosts(), Action: &action.Template{ Template: templates.InitOsScriptTmpl, - Dst: "/tmp/kubekey/initOS.sh", + Dst: filepath.Join(common.KubeScriptDir, "initOS.sh"), Data: util.Data{ "Hosts": c.KubeConf.ClusterHosts, }, diff --git a/pkg/pipelines/initialization/tasks.go b/pkg/pipelines/initialization/tasks.go index 5bac66d0..bc8705e2 100644 --- a/pkg/pipelines/initialization/tasks.go +++ b/pkg/pipelines/initialization/tasks.go @@ -153,11 +153,11 @@ type NodeExecScript struct { } func (n *NodeExecScript) Execute(runtime connector.Runtime) error { - if _, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("chmod +x %s/initOS.sh", "/tmp/kubekey"), false); err != nil { + if _, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("chmod +x %s/initOS.sh", common.KubeScriptDir), false); err != nil { return errors.Wrap(errors.WithStack(err), "Failed to chmod +x init os script") } - if _, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("cp %s/initOS.sh %s && sudo %s/initOS.sh", "/tmp/kubekey", common.KubeScriptDir, common.KubeScriptDir), true); err != nil { + if _, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("%s/initOS.sh", common.KubeScriptDir), true); err != nil { return errors.Wrap(errors.WithStack(err), "Failed to configure operating system") } return nil diff --git a/pkg/pipelines/initialization/templates/init_script.go b/pkg/pipelines/initialization/templates/init_script.go index 8a982489..74148b0f 100644 --- a/pkg/pipelines/initialization/templates/init_script.go +++ b/pkg/pipelines/initialization/templates/init_script.go @@ -5,7 +5,7 @@ import ( "text/template" ) -var InitOsScriptTmpl = template.Must(template.New("initOS").Parse( +var InitOsScriptTmpl = template.Must(template.New("initOS.sh").Parse( dedent.Dedent(`#!/usr/bin/env bash # Copyright 2020 The KubeSphere Authors. diff --git a/pkg/pipelines/loadbalancer/module.go b/pkg/pipelines/loadbalancer/module.go index b350d0cf..7085e8e5 100644 --- a/pkg/pipelines/loadbalancer/module.go +++ b/pkg/pipelines/loadbalancer/module.go @@ -34,7 +34,7 @@ func (h *HaproxyModule) Init() { Template: templates.HaproxyConfig, Dst: "/etc/kubekey/haproxy/haproxy.cfg", Data: util.Data{ - "MasterNodes": masterNodeStr(h.KubeConf), + "MasterNodes": masterNodeStr(h.Runtime, h.KubeConf), "LoadbalancerApiserverPort": h.KubeConf.Cluster.ControlPlaneEndpoint.Port, "LoadbalancerApiserverHealthcheckPort": 8081, "KubernetesType": h.KubeConf.Cluster.Kubernetes.Type, @@ -159,9 +159,9 @@ func (h *HaproxyModule) Init() { } } -func masterNodeStr(conf *common.KubeRuntime) []string { - masterNodes := make([]string, len(conf.GetHostsByRole(common.Master))) - for i, node := range conf.GetHostsByRole(common.Master) { +func masterNodeStr(runtime connector.Runtime, conf *common.KubeConf) []string { + masterNodes := make([]string, len(runtime.GetHostsByRole(common.Master))) + for i, node := range runtime.GetHostsByRole(common.Master) { masterNodes[i] = node.GetName() + " " + node.GetAddress() + ":" + strconv.Itoa(conf.Cluster.ControlPlaneEndpoint.Port) } return masterNodes