Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modified some commands to compatible running In the container #132

Merged
merged 8 commits into from
Jan 17, 2025
9 changes: 6 additions & 3 deletions pkg/bootstrap/os/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,12 @@ func (n *NodeConfigureOS) Execute(runtime connector.Runtime) error {
}
}

_, err1 := runtime.GetRunner().SudoCmd(fmt.Sprintf("hostnamectl set-hostname %s && sed -i '/^127.0.1.1/s/.*/127.0.1.1 %s/g' /etc/hosts", host.GetName(), host.GetName()), false, false)
if err1 != nil {
return errors.Wrap(errors.WithStack(err1), "Failed to override hostname")
// if running in docker container, /etc/hosts file is bind mounting, cannot be replaced via mv command
if !n.KubeConf.Arg.IsOlaresInContainer {
_, err1 := runtime.GetRunner().SudoCmd(fmt.Sprintf("hostnamectl set-hostname %s && sed -i '/^127.0.1.1/s/.*/127.0.1.1 %s/g' /etc/hosts", host.GetName(), host.GetName()), false, false)
if err1 != nil {
return errors.Wrap(errors.WithStack(err1), "Failed to override hostname")
}
}

if runtime.GetSystemInfo().IsWsl() {
Expand Down
3 changes: 3 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ const (
ENV_AUTO_ADD_FIREWALL_RULES = "AUTO_ADD_FIREWALL_RULES"
ENV_TERMINUS_OS_DOMAINNAME = "TERMINUS_OS_DOMAINNAME"
ENV_DEFAULT_WSL_DISTRO_LOCATION = "DEFAULT_WSL_DISTRO_LOCATION" // If set to 1, the default WSL distro storage will be used.

ENV_CONTAINER = "container"
ENV_CONTAINER_MODE = "CONTAINER_MODE" // running in docker container
)

// TerminusGlobalEnvs holds a group of general environment variables
Expand Down
8 changes: 6 additions & 2 deletions pkg/common/kube_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ package common
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/pflag"
"net"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/pkg/errors"
"github.com/spf13/pflag"

kubekeyapiv1alpha2 "bytetrade.io/web3os/installer/apis/kubekey/v1alpha2"
kubekeyclientset "bytetrade.io/web3os/installer/clients/clientset/versioned"
"bytetrade.io/web3os/installer/pkg/core/common"
Expand Down Expand Up @@ -120,6 +121,8 @@ type Argument struct {
HostIP string `json:"host_ip"`

CudaVersion string `json:"cuda_version"`

IsOlaresInContainer bool `json:"is_olares_in_container"`
}

type MasterHostConfig struct {
Expand Down Expand Up @@ -241,6 +244,7 @@ func NewArgument() *Argument {
}
arg.IsCloudInstance, _ = strconv.ParseBool(os.Getenv(ENV_TERMINUS_IS_CLOUD_VERSION))
arg.PublicNetworkInfo.PubliclyAccessible, _ = strconv.ParseBool(os.Getenv(ENV_PUBLICLY_ACCESSIBLE))
arg.IsOlaresInContainer = os.Getenv("CONTAINER_MODE") == "oic"
return arg
}

Expand Down
192 changes: 95 additions & 97 deletions pkg/images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"time"

kubekeyapiv1alpha2 "bytetrade.io/web3os/installer/apis/kubekey/v1alpha2"
"bytetrade.io/web3os/installer/pkg/common"
Expand Down Expand Up @@ -130,101 +128,101 @@ func (images *Images) PullImages(runtime connector.Runtime, kubeConf *common.Kub
return nil
}

type LocalImage struct {
Filename string
}

type LocalImages []LocalImage

func (i LocalImages) LoadImages(runtime connector.Runtime, kubeConf *common.KubeConf) error {
loadCmd := "docker"
host := runtime.RemoteHost()
retry := func(f func() error, times int) (err error) {
for i := 0; i < times; i++ {
err = f()
if err == nil {
return nil
}
var dur = 5 + (i+1)*10
logger.Warnf("load image %s failed, wait for %d seconds(%d times)", err, dur, i+1)
if (i + 1) < times {
time.Sleep(time.Duration(dur) * time.Second)
}
}

return
}

for _, image := range i {
switch {
case host.IsRole(common.Master):
// logger.Debugf("%s preloading image: %s", host.GetName(), image.Filename)
start := time.Now()
fileName := filepath.Base(image.Filename)
// fileName = strings.ReplaceAll(fileName, ".gz", "")
// fmt.Println(">>> ", fileName, HasSuffixI(image.Filename, ".tar.gz", ".tgz"))
if HasSuffixI(image.Filename, ".tar.gz", ".tgz") {
switch kubeConf.Cluster.Kubernetes.ContainerManager {
case "crio":
loadCmd = "ctr" // BUG
case "containerd":
loadCmd = "ctr -n k8s.io images import -"
case "isula":
loadCmd = "isula"
default:
loadCmd = "docker load"
}

// continue if load image error
if err := retry(func() error {
logger.Infof("preloading image: %s", fileName)
if stdout, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("env PATH=$PATH gunzip -c %s | %s", image.Filename, loadCmd), false, false); err != nil {
return fmt.Errorf("%s", fileName)
} else {
logger.Infof("%s in %s\n", formatLoadImageRes(stdout, fileName), time.Since(start))
// fmt.Printf("%s in %s\n", formatLoadImageRes(stdout, fileName), time.Since(start))
}
return nil
}, 5); err != nil {
return fmt.Errorf("%s", fileName)
}
} else if HasSuffixI(image.Filename, ".tar") {
switch kubeConf.Cluster.Kubernetes.ContainerManager {
case "crio":
loadCmd = "ctr" // BUG
case "containerd":
loadCmd = "ctr -n k8s.io images import"
case "isula":
loadCmd = "isula"
default:
loadCmd = "docker load -i"
}

if err := retry(func() error {
logger.Infof("preloading image: %s", fileName)
if stdout, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("env PATH=$PATH %s %s", loadCmd, image.Filename), false, false); err != nil {
return fmt.Errorf("%s", fileName)
} else {
logger.Infof("%s in %s\n", formatLoadImageRes(stdout, fileName), time.Since(start))
// fmt.Printf("%s in %s\n", formatLoadImageRes(stdout, fileName), time.Since(start))
}

return nil
}, 5); err != nil {
return fmt.Errorf("%s", fileName)
}
} else {
logger.Warnf("invalid image file name %s, skip ...", image.Filename)
return nil
}
default:
continue
}

}
return nil

}
// type LocalImage struct {
// Filename string
// }

// type LocalImages []LocalImage

// func (i LocalImages) LoadImages(runtime connector.Runtime, kubeConf *common.KubeConf) error {
// loadCmd := "docker"
// host := runtime.RemoteHost()
// retry := func(f func() error, times int) (err error) {
// for i := 0; i < times; i++ {
// err = f()
// if err == nil {
// return nil
// }
// var dur = 5 + (i+1)*10
// logger.Warnf("load image %s failed, wait for %d seconds(%d times)", err, dur, i+1)
// if (i + 1) < times {
// time.Sleep(time.Duration(dur) * time.Second)
// }
// }

// return
// }

// for _, image := range i {
// switch {
// case host.IsRole(common.Master):
// // logger.Debugf("%s preloading image: %s", host.GetName(), image.Filename)
// start := time.Now()
// fileName := filepath.Base(image.Filename)
// // fileName = strings.ReplaceAll(fileName, ".gz", "")
// // fmt.Println(">>> ", fileName, HasSuffixI(image.Filename, ".tar.gz", ".tgz"))
// if HasSuffixI(image.Filename, ".tar.gz", ".tgz") {
// switch kubeConf.Cluster.Kubernetes.ContainerManager {
// case "crio":
// loadCmd = "ctr" // BUG
// case "containerd":
// loadCmd = "ctr -n k8s.io images import -"
// case "isula":
// loadCmd = "isula"
// default:
// loadCmd = "docker load"
// }

// // continue if load image error
// if err := retry(func() error {
// logger.Infof("preloading image: %s", fileName)
// if stdout, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("env PATH=$PATH gunzip -c %s | %s", image.Filename, loadCmd), false, false); err != nil {
// return fmt.Errorf("%s", fileName)
// } else {
// logger.Infof("%s in %s\n", formatLoadImageRes(stdout, fileName), time.Since(start))
// // fmt.Printf("%s in %s\n", formatLoadImageRes(stdout, fileName), time.Since(start))
// }
// return nil
// }, 5); err != nil {
// return fmt.Errorf("%s", fileName)
// }
// } else if HasSuffixI(image.Filename, ".tar") {
// switch kubeConf.Cluster.Kubernetes.ContainerManager {
// case "crio":
// loadCmd = "ctr" // BUG
// case "containerd":
// loadCmd = "ctr -n k8s.io images import"
// case "isula":
// loadCmd = "isula"
// default:
// loadCmd = "docker load -i"
// }

// if err := retry(func() error {
// logger.Infof("preloading image: %s", fileName)
// if stdout, err := runtime.GetRunner().SudoCmd(fmt.Sprintf("env PATH=$PATH %s %s", loadCmd, image.Filename), false, false); err != nil {
// return fmt.Errorf("%s", fileName)
// } else {
// logger.Infof("%s in %s\n", formatLoadImageRes(stdout, fileName), time.Since(start))
// // fmt.Printf("%s in %s\n", formatLoadImageRes(stdout, fileName), time.Since(start))
// }

// return nil
// }, 5); err != nil {
// return fmt.Errorf("%s", fileName)
// }
// } else {
// logger.Warnf("invalid image file name %s, skip ...", image.Filename)
// return nil
// }
// default:
// continue
// }

// }
// return nil

// }

func formatLoadImageRes(str string, fileName string) string {
if strings.Contains(str, "(sha256:") {
Expand Down
9 changes: 7 additions & 2 deletions pkg/images/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,13 @@ func (t *LoadImages) Execute(runtime connector.Runtime) (reserr error) {
var loadCmd string
var loadParm string

if runtime.GetSystemInfo().GetFsType() == "zfs" {
loadParm = "--snapshotter=zfs"
// unused
// if runtime.GetSystemInfo().GetFsType() == "zfs" {
// loadParm = "--snapshotter=zfs"
// }

if t.KubeConf.Arg.IsOlaresInContainer {
loadParm = "--no-unpack"
}

if runtime.RemoteHost().GetOs() == common.Darwin {
Expand Down
74 changes: 37 additions & 37 deletions pkg/k3s/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
package k3s

import (
"bytetrade.io/web3os/installer/pkg/storage"
storagetpl "bytetrade.io/web3os/installer/pkg/storage/templates"
"context"
"encoding/base64"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"time"

"bytetrade.io/web3os/installer/pkg/storage"
storagetpl "bytetrade.io/web3os/installer/pkg/storage/templates"

"bytetrade.io/web3os/installer/pkg/container"
"bytetrade.io/web3os/installer/pkg/manifest"
"bytetrade.io/web3os/installer/pkg/registry"
Expand Down Expand Up @@ -352,40 +352,40 @@ func (e *EnableK3sService) Execute(runtime connector.Runtime) error {
return nil
}

type PreloadImagesService struct {
common.KubeAction
}

func (p *PreloadImagesService) Execute(runtime connector.Runtime) error {
if utils.IsExist(common.K3sImageDir) {
if err := util.CreateDir(common.K3sImageDir); err != nil {
logger.Errorf("create dir %s failed: %v", common.K3sImageDir, err)
return err
}
}

fileInfos, err := os.ReadDir(common.K3sImageDir)
if err != nil {
logger.Errorf("Unable to read images in %s: %v", common.K3sImageDir, err)
return nil
}

var loadingImages images.LocalImages
for _, fileInfo := range fileInfos {
if fileInfo.IsDir() {
continue
}

filePath := filepath.Join(common.K3sImageDir, fileInfo.Name())

loadingImages = append(loadingImages, images.LocalImage{Filename: filePath})
}

if err := loadingImages.LoadImages(runtime, p.KubeConf); err != nil {
return errors.Wrap(errors.WithStack(err), "preload image failed")
}
return nil
}
// type PreloadImagesService struct {
// common.KubeAction
// }

// func (p *PreloadImagesService) Execute(runtime connector.Runtime) error {
// if utils.IsExist(common.K3sImageDir) {
// if err := util.CreateDir(common.K3sImageDir); err != nil {
// logger.Errorf("create dir %s failed: %v", common.K3sImageDir, err)
// return err
// }
// }

// fileInfos, err := os.ReadDir(common.K3sImageDir)
// if err != nil {
// logger.Errorf("Unable to read images in %s: %v", common.K3sImageDir, err)
// return nil
// }

// var loadingImages images.LocalImages
// for _, fileInfo := range fileInfos {
// if fileInfo.IsDir() {
// continue
// }

// filePath := filepath.Join(common.K3sImageDir, fileInfo.Name())

// loadingImages = append(loadingImages, images.LocalImage{Filename: filePath})
// }

// if err := loadingImages.LoadImages(runtime, p.KubeConf); err != nil {
// return errors.Wrap(errors.WithStack(err), "preload image failed")
// }
// return nil
// }

type CopyK3sKubeConfig struct {
common.KubeAction
Expand Down
Loading