Skip to content

Commit

Permalink
fix: get remote kubeconfig path with $HOME variable (#3906) (#3909)
Browse files Browse the repository at this point in the history
Co-authored-by: fengxsong <fengxsong@outlook.com>
  • Loading branch information
sealos-ci-robot and fengxsong committed Sep 12, 2023
1 parent 6bd5c0a commit dda1eba
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 20 deletions.
25 changes: 10 additions & 15 deletions pkg/runtime/k3s/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ package k3s
import (
"context"
"fmt"
"path"
"path/filepath"

"golang.org/x/sync/errgroup"

"github.com/labring/sealos/pkg/constants"

"github.com/labring/sealos/pkg/utils/file"
"github.com/labring/sealos/pkg/utils/logger"
"github.com/labring/sealos/pkg/utils/rand"
Expand All @@ -38,7 +36,7 @@ func (k *K3s) initMaster0() error {
k.generateAndSendInitConfig,
func() error { return k.enableK3sService(master0) },
k.pullKubeConfigFromMaster0,
func() error { return k.copyKubeConfigFileToAllNodes([]string{k.cluster.GetMaster0IPAndPort()}) },
func() error { return k.copyKubeConfigFileToNodes(k.cluster.GetMaster0IPAndPort()) },
)
}

Expand Down Expand Up @@ -97,7 +95,7 @@ func (k *K3s) joinMaster(master string) error {
return k.sshClient.Copy(master, filepath.Join(k.pathResolver.EtcPath(), defaultJoinMastersFilename), defaultK3sConfigPath)
},
func() error { return k.enableK3sService(master) },
func() error { return k.copyKubeConfigFileToAllNodes([]string{master}) },
func() error { return k.copyKubeConfigFileToNodes(master) },
)
}

Expand All @@ -120,7 +118,7 @@ func (k *K3s) joinNode(node string) error {
return k.sshClient.Copy(node, filepath.Join(k.pathResolver.EtcPath(), defaultJoinNodesFilename), defaultK3sConfigPath)
},
func() error { return k.enableK3sService(node) },
func() error { return k.copyKubeConfigFileToAllNodes([]string{node}) },
func() error { return k.copyKubeConfigFileToNodes(node) },
)
}

Expand Down Expand Up @@ -193,21 +191,18 @@ func (k *K3s) pullKubeConfigFromMaster0() error {
return k.sshClient.Fetch(k.cluster.GetMaster0IPAndPort(), defaultKubeConfigPath, dest)
}

func (k *K3s) copyKubeConfigFileToAllNodes(hosts []string) error {
func (k *K3s) copyKubeConfigFileToNodes(hosts ...string) error {
src := k.pathResolver.AdminFile()
dst := path.Join(".kube", "config")
return k.sendFileToHosts(hosts, src, dst)
}

func (k *K3s) sendFileToHosts(Hosts []string, src, dst string) error {
eg, _ := errgroup.WithContext(context.Background())
for _, node := range Hosts {
for _, node := range hosts {
node := node
eg.Go(func() error {
if err := k.sshClient.Copy(node, src, dst); err != nil {
return fmt.Errorf("send file failed %v", err)
home, err := k.sshClient.CmdToString(node, "echo $HOME", "")
if err != nil {
return err
}
return nil
dst := filepath.Join(home, ".kube", "config")
return k.sshClient.Copy(node, src, dst)
})
}
return eg.Wait()
Expand Down
24 changes: 20 additions & 4 deletions pkg/runtime/kubernetes/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,30 @@ limitations under the License.

package kubernetes

import "path"
import (
"context"
"path/filepath"

"golang.org/x/sync/errgroup"
)

const copyKubeAdminConfigCommand = `rm -rf $HOME/.kube/config && mkdir -p $HOME/.kube && cp /etc/kubernetes/admin.conf $HOME/.kube/config`

func (k *KubeadmRuntime) copyKubeConfigFileToNodes(hosts []string) error {
func (k *KubeadmRuntime) copyKubeConfigFileToNodes(hosts ...string) error {
src := k.pathResolver.AdminFile()
dst := path.Join(".kube", "config")
return k.sendFileToHosts(hosts, src, dst)
eg, _ := errgroup.WithContext(context.Background())
for _, node := range hosts {
node := node
eg.Go(func() error {
home, err := k.sshClient.CmdToString(node, "echo $HOME", "")
if err != nil {
return err
}
dst := filepath.Join(home, ".kube", "config")
return k.sshClient.Copy(node, src, dst)
})
}
return eg.Wait()
}

func (k *KubeadmRuntime) copyMasterKubeConfig(host string) error {
Expand Down
2 changes: 1 addition & 1 deletion pkg/runtime/kubernetes/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (k *KubeadmRuntime) ScaleUp(newMasterIPList []string, newNodeIPList []strin
if err := k.joinNodes(newNodeIPList); err != nil {
return err
}
return k.copyKubeConfigFileToNodes(newNodeIPList)
return k.copyKubeConfigFileToNodes(newNodeIPList...)
}
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/ssh/scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ func (c *Client) sftpConnect(host string) (sshClient *ssh.Client, sftpClient *sf
// Copy is copy file or dir to remotePath, add md5 validate
func (c *Client) Copy(host, localPath, remotePath string) error {
if c.isLocalAction(host) {
if !filepath.IsAbs(localPath) || !filepath.IsAbs(remotePath) {
logger.Warn(`either the local path or the remote path is not an absolute path, `+
`copy might not work as expected.`, localPath, remotePath)
}
logger.Debug("local %s copy files src %s to dst %s", host, localPath, remotePath)
return file.RecursionCopy(localPath, remotePath)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/file/file_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func CopyDirV3(srcPath, destPath string, filters ...func(filePath string) bool)

// Copy copies file from source to target path.
func Copy(src, dest string) error {
_ = os.Remove(dest)
// Gather file information to set back later.
si, err := os.Lstat(src)
if err != nil {
Expand Down

0 comments on commit dda1eba

Please sign in to comment.