Skip to content

Commit

Permalink
feat(autok3s): support install k3s into Alpine
Browse files Browse the repository at this point in the history
Remove sudo in all command
Running shell commands in a wrapped script
And including minor fixes:
- Combine SetWindowSize and OpenTerminal functions in dialer interface
- OpenTerminal can support initial heigh and width size
  • Loading branch information
orangedeng authored and JacieChao committed Jul 24, 2023
1 parent 30a9298 commit 5e2bebd
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 159 deletions.
5 changes: 4 additions & 1 deletion pkg/airgap/file_scp.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ func ScpFiles(logger *logrus.Logger, clusterName string, pkg *common.Package, di
targetFilename := filepath.Join(targetPath, filename)

var stdout, stderr bytes.Buffer
if err := dialer.RenewSession(); err != nil {
return err
}
dialer = dialer.SetStdio(&stdout, &stderr, nil)
moveCMD := fmt.Sprintf("sudo mkdir -p %s;sudo mv %s %s", targetPath, remoteFileName, targetFilename)
moveCMD := fmt.Sprintf("mkdir -p %s;mv %s %s", targetPath, remoteFileName, targetFilename)
fieldLogger.Infof("executing cmd in remote server %s", moveCMD)
if err := dialer.Cmd(moveCMD).Run(); err != nil {
fieldLogger.Errorf("failed to execute cmd %s, stdout: %s, stderr: %s, %v", moveCMD, stdout.String(), stderr.String(), err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/cluster/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
ui = false
embedEtcd = false
defaultCidr = "10.42.0.0/16"
uploadManifestCmd = "echo \"%s\" | base64 -d | sudo tee \"%s/%s\""
uploadManifestCmd = "echo \"%s\" | base64 -d | tee \"%s/%s\""
dockerInstallScript = "https://get.docker.com"
)

Expand Down Expand Up @@ -1177,8 +1177,8 @@ func (p *ProviderBase) ReleaseManifests() error {
)

_ = dialer.SetStdio(&stdout, &stderr, nil).SetWriter(p.Logger.Out).
Cmd(fmt.Sprintf("sudo kubectl delete -f %s/ui.yaml", common.K3sManifestsDir)).
Cmd(fmt.Sprintf("sudo rm %s/ui.yaml", common.K3sManifestsDir)).Run()
Cmd(fmt.Sprintf("kubectl delete -f %s/ui.yaml", common.K3sManifestsDir)).
Cmd(fmt.Sprintf("rm %s/ui.yaml", common.K3sManifestsDir)).Run()
_ = dialer.Close()
break
}
Expand Down
61 changes: 40 additions & 21 deletions pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"os"
Expand All @@ -22,9 +21,11 @@ import (
"github.com/cnrancher/autok3s/pkg/utils"

"github.com/k3d-io/k3d/v5/pkg/types/k3s"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
Expand Down Expand Up @@ -117,7 +118,7 @@ func (p *ProviderBase) InitK3sCluster(cluster *types.Cluster) error {
}

// get k3s cluster config.
cfg, err := p.execute(&cluster.MasterNodes[0], []string{catCfgCommand})
cfg, err := p.executeWithRetry(3, &cluster.MasterNodes[0], []string{catCfgCommand})
if err != nil {
return err
}
Expand Down Expand Up @@ -487,19 +488,37 @@ func (p *ProviderBase) execute(n *types.Node, cmds []string) (string, error) {
stdout bytes.Buffer
stderr bytes.Buffer
)
dialer.SetStdio(&stdout, &stderr, nil).SetWriter(p.Logger.Out)

for _, cmd := range cmds {
dialer.Cmd(cmd)
}

if err := dialer.Run(); err != nil {
if err := dialer.SetStdio(&stdout, &stderr, nil).
SetWriter(p.Logger.Out).
Cmd(cmds...).
Run(); err != nil {
return "", fmt.Errorf("%w: %s", err, stderr.String())
}

return stdout.String(), nil
}

func (p *ProviderBase) executeWithRetry(count int, n *types.Node, cmds []string) (string, error) {
backoff := wait.Backoff{
Duration: 2 * time.Second,
Factor: 1,
Steps: count,
}
var rtn string
var lastError error
err := wait.ExponentialBackoff(backoff, func() (bool, error) {
rtn, lastError = p.execute(n, cmds)
if lastError != nil {
return false, nil
}
return true, nil
})
if err != nil {
return "", errors.Wrapf(lastError, "failed to execute cmd with max retry count %d", count)
}
return rtn, nil
}

func terminal(n *types.Node) error {
dialer, err := hosts.NewSSHDialer(n, true, common.NewLogger(nil))
if err != nil {
Expand Down Expand Up @@ -537,15 +556,15 @@ func (p *ProviderBase) handleRegistry(n *types.Node, c *types.Cluster) (err erro
return err
}
} else {
cmd = []string{fmt.Sprintf("sudo mkdir -p %s", registryPath)}
cmd = []string{fmt.Sprintf("mkdir -p %s", registryPath)}
}

registryContent, err := utils.RegistryToString(registry)
if err != nil {
return err
}

cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | sudo tee \"/etc/rancher/k3s/registries.yaml\"",
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | tee \"/etc/rancher/k3s/registries.yaml\"",
base64.StdEncoding.EncodeToString([]byte(registryContent))))
_, err = p.execute(n, cmd)
return err
Expand Down Expand Up @@ -601,13 +620,13 @@ func saveRegistryTLS(registry *k3s.Registry, m map[string]map[string][]byte) ([]

// i.e /etc/rancher/k3s/mycustomreg:5000/.
path := fmt.Sprintf("/etc/rancher/k3s/%s", r)
cmd = append(cmd, fmt.Sprintf("sudo mkdir -p %s", path))
cmd = append(cmd, fmt.Sprintf("mkdir -p %s", path))

for f, b := range c {
// i.e /etc/rancher/k3s/mycustomreg:5000/{ca,key,cert}.
file := fmt.Sprintf("%s/%s", path, f)
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | sudo tee \"%s\"", base64.StdEncoding.EncodeToString(b), file))
cmd = append(cmd, fmt.Sprintf("sudo chmod 755 %s", file))
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | tee \"%s\"", base64.StdEncoding.EncodeToString(b), file))
cmd = append(cmd, fmt.Sprintf("chmod 755 %s", file))

switch f {
case "cert":
Expand Down Expand Up @@ -847,41 +866,41 @@ func (p *ProviderBase) scpFiles(clusterName string, pkg *common.Package, node *t

func (p *ProviderBase) handleDataStoreCertificate(n *types.Node, c *types.Cluster) error {
cmd := make([]string, 0)
cmd = append(cmd, fmt.Sprintf("sudo mkdir -p %s", datastoreCertificatesPath))
cmd = append(cmd, fmt.Sprintf("mkdir -p %s", datastoreCertificatesPath))
if c.DataStoreCAFile != "" {
caFile, err := os.ReadFile(c.DataStoreCAFile)
if err != nil {
return err
}
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | sudo tee \"%s/ds-ca.pem\"",
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | tee \"%s/ds-ca.pem\"",
base64.StdEncoding.EncodeToString(caFile), datastoreCertificatesPath))
}
if c.DataStoreCertFile != "" {
certFile, err := os.ReadFile(c.DataStoreCertFile)
if err != nil {
return err
}
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | sudo tee \"%s/ds-cert.pem\"",
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | tee \"%s/ds-cert.pem\"",
base64.StdEncoding.EncodeToString(certFile), datastoreCertificatesPath))
}
if c.DataStoreKeyFile != "" {
keyFile, err := os.ReadFile(c.DataStoreKeyFile)
if err != nil {
return err
}
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | sudo tee \"%s/ds-key.pem\"",
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | tee \"%s/ds-key.pem\"",
base64.StdEncoding.EncodeToString(keyFile), datastoreCertificatesPath))
}
if c.DataStoreCAFileContent != "" {
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | sudo tee \"%s/ds-ca.pem\"",
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | tee \"%s/ds-ca.pem\"",
base64.StdEncoding.EncodeToString([]byte(p.DataStoreCAFileContent)), datastoreCertificatesPath))
}
if c.DataStoreKeyFileContent != "" {
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | sudo tee \"%s/ds-key.pem\"",
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | tee \"%s/ds-key.pem\"",
base64.StdEncoding.EncodeToString([]byte(p.DataStoreKeyFileContent)), datastoreCertificatesPath))
}
if c.DataStoreCertFileContent != "" {
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | sudo tee \"%s/ds-cert.pem\"",
cmd = append(cmd, fmt.Sprintf("echo \"%s\" | base64 -d | tee \"%s/ds-cert.pem\"",
base64.StdEncoding.EncodeToString([]byte(p.DataStoreCertFileContent)), datastoreCertificatesPath))
}
_, err := p.execute(n, cmd)
Expand Down
12 changes: 6 additions & 6 deletions pkg/cluster/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const (
)

var (
getTokenCommand = "sudo cat /var/lib/rancher/k3s/server/node-token"
catCfgCommand = "sudo cat /etc/rancher/k3s/k3s.yaml"
getTokenCommand = "cat /var/lib/rancher/k3s/server/node-token"
catCfgCommand = "cat /etc/rancher/k3s/k3s.yaml"
dockerCommand = "if ! type docker; then curl -sSL %s | %s sh - %s; fi"
masterUninstallCommand = "sh /usr/local/bin/k3s-uninstall.sh"
workerUninstallCommand = "sh /usr/local/bin/k3s-agent-uninstall.sh"
k3sRestart = `if [ -n "$(command -v systemctl)" ]; then sudo systemctl restart k3s; elif [ -n "$(command -v service)" ]; then sudo service k3s restart; fi`
k3sAgentRestart = `if [ -n "$(command -v systemctl)" ]; then sudo systemctl restart k3s-agent; elif [ -n "$(command -v service)" ]; then sudo service k3s-agent restart; fi`
masterUninstallCommand = "[ -x /usr/local/bin/k3s-uninstall.sh ] && sh /usr/local/bin/k3s-uninstall.sh || true"
workerUninstallCommand = "[ -x /usr/local/bin/k3s-agent-uninstall.sh ] && sh /usr/local/bin/k3s-agent-uninstall.sh || true"
k3sRestart = `if [ -n "$(command -v systemctl)" ]; then systemctl restart k3s; elif [ -n "$(command -v service)" ]; then service k3s restart; fi`
k3sAgentRestart = `if [ -n "$(command -v systemctl)" ]; then systemctl restart k3s-agent; elif [ -n "$(command -v service)" ]; then service k3s-agent restart; fi`
)

// getCommand first node should be init
Expand Down
13 changes: 2 additions & 11 deletions pkg/hosts/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ type DockerDialer struct {
Stderr io.Writer
Writer io.Writer

Height int
Weight int

ctx context.Context
client client.APIClient
response *dockertypes.HijackedResponse
Expand Down Expand Up @@ -150,12 +147,6 @@ func (d *DockerDialer) SetIO(stdout, stderr io.Writer, stdin io.ReadCloser) {
d.Stdin = stdin
}

// SetWindowSize set dialer's default win size.
func (d *DockerDialer) SetWindowSize(height, weight int) {
d.Height = height
d.Weight = weight
}

// SetWriter set dialer's logs writer.
func (d *DockerDialer) SetWriter(w io.Writer) *DockerDialer {
d.Writer = w
Expand Down Expand Up @@ -189,7 +180,7 @@ func (d *DockerDialer) Terminal() error {
}

// OpenTerminal open docker websocket terminal.
func (d *DockerDialer) OpenTerminal() error {
func (d *DockerDialer) OpenTerminal(win WindowSize) error {
return d.ExecStart(false)
}

Expand Down Expand Up @@ -444,7 +435,7 @@ func (d *DockerDialer) beginInputStream(restoreInput func()) (doneC <-chan struc
}

// ChangeWindowSize change tty window size for websocket.
func (d *DockerDialer) ChangeWindowSize(win *WindowSize) error {
func (d *DockerDialer) ChangeWindowSize(win WindowSize) error {
return d.ResizeTtyTo(d.ctx, uint(win.Height), uint(win.Width))
}

Expand Down
17 changes: 4 additions & 13 deletions pkg/hosts/pty.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ type PtyDialer struct {
Stderr io.Writer
Writer io.Writer

Height int
Weight int

ctx context.Context
conn *os.File
cmd *exec.Cmd
Expand Down Expand Up @@ -54,17 +51,11 @@ func (d *PtyDialer) SetIO(stdout, stderr io.Writer, stdin io.ReadCloser) {
d.Stdin = stdin
}

// SetWindowSize set tty window size size.
func (d *PtyDialer) SetWindowSize(height, weight int) {
d.Height = height
d.Weight = weight
}

// OpenTerminal open pty websocket terminal.
func (d *PtyDialer) OpenTerminal() error {
func (d *PtyDialer) OpenTerminal(win WindowSize) error {
p, err := pty.StartWithSize(d.cmd, &pty.Winsize{
Rows: uint16(d.Height),
Cols: uint16(d.Weight),
Rows: uint16(win.Height),
Cols: uint16(win.Width),
})
if err != nil {
return err
Expand All @@ -84,7 +75,7 @@ func (d *PtyDialer) OpenTerminal() error {
}

// ChangeWindowSize changes to the current window size.
func (d *PtyDialer) ChangeWindowSize(win *WindowSize) error {
func (d *PtyDialer) ChangeWindowSize(win WindowSize) error {
return pty.Setsize(d.conn, &pty.Winsize{
Rows: uint16(win.Height),
Cols: uint16(win.Width),
Expand Down
12 changes: 1 addition & 11 deletions pkg/hosts/pty_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ type PtyDialer struct {
Stderr io.Writer
Writer io.Writer

Height int
Weight int

ctx context.Context
conn *os.File
cmd *exec.Cmd
Expand Down Expand Up @@ -56,19 +53,12 @@ func (d *PtyDialer) SetStdio(stdout, stderr io.Writer, stdin io.ReadCloser) *Pty
return d
}

// SetDefaultSize set dialer's default win size.
func (d *PtyDialer) SetDefaultSize(height, weight int) *PtyDialer {
d.Height = height
d.Weight = weight
return d
}

// WebSocketTerminal open pty websocket terminal.
func (d *PtyDialer) WebSocketTerminal() error {
return fmt.Errorf("not support windows")
}

// ChangeSize changes to the current win size.
func (d *PtyDialer) ChangeSize(win *WindowSize) error {
func (d *PtyDialer) ChangeSize(win WindowSize) error {
return nil
}
Loading

0 comments on commit 5e2bebd

Please sign in to comment.