Skip to content

Commit

Permalink
opt: sealer common values (sealerio#2246)
Browse files Browse the repository at this point in the history
Signed-off-by: kakzhou719 <kakazhou719@163.com>
  • Loading branch information
kakaZhou719 authored Jun 14, 2023
1 parent 9ebe3d7 commit 52f25f1
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 176 deletions.
43 changes: 0 additions & 43 deletions cmd/sealer/boot/boot.go

This file was deleted.

13 changes: 11 additions & 2 deletions cmd/sealer/cmd/cluster/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package cluster
import (
"fmt"
"net"
"os"
"path/filepath"

"github.com/pkg/errors"
Expand Down Expand Up @@ -567,9 +568,17 @@ func (k KubeInstaller) Delete(options KubeDeleteOptions) error {
if err = installer.UnInstall(); err != nil {
return err
}
//delete local files,including clusterfile, application.json under sealer work dir
if err = os.Remove(common.GetDefaultClusterfile()); err != nil {
return err
}

if err = os.Remove(common.GetDefaultApplicationFile()); err != nil {
return err
}

//delete local files,including sealer workdir,cluster file under sealer,kubeconfig under home dir.
if err = fs.FS.RemoveAll(common.GetSealerWorkDir(), common.DefaultKubeConfigDir()); err != nil {
//delete kubeconfig under home dir.
if err = fs.FS.RemoveAll(common.DefaultKubeConfigDir()); err != nil {
return err
}

Expand Down
6 changes: 0 additions & 6 deletions cmd/sealer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,12 @@ package main

import (
"github.com/containers/buildah"

"github.com/sealerio/sealer/cmd/sealer/boot"
"github.com/sealerio/sealer/cmd/sealer/cmd"
)

func main() {
if buildah.InitReexec() {
return
}

if err := boot.OnBoot(); err != nil {
panic(err)
}
cmd.Execute()
}
36 changes: 0 additions & 36 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ const (

// Default dir and file path
const (
EtcDir = "etc"
DefaultTmpDir = "/var/lib/sealer/tmp"
DefaultLogDir = "/var/lib/sealer/log"
DefaultSealerDataDir = "/var/lib/sealer/data"
KubeAdminConf = "/etc/kubernetes/admin.conf"
DefaultKubectlPath = "/usr/bin/kubectl"
ClusterfileName = "ClusterfileName"
RenderChartsDir = "charts"
RenderManifestsDir = "manifests"
KubeLvsCareStaticPodName = "kube-lvscare"
RegLvsCareStaticPodName = "reg-lvscare"
StaticPodDir = "/etc/kubernetes/manifests"
Expand Down Expand Up @@ -90,10 +85,7 @@ const (

// image module
const (
DefaultImageRootDir = "/var/lib/sealer/data"
DefaultMetadataName = "Metadata"
DefaultImageMetaRootDir = "/var/lib/sealer/metadata"
DefaultLayerDir = "/var/lib/sealer/data/overlay2"
DefaultRegistryDomain = "sea.hub"
DefaultRegistryPort = 5000
DefaultRegistryURL = "sea.hub:5000"
Expand Down Expand Up @@ -171,34 +163,6 @@ func DefaultKubeConfigDir() string {
return filepath.Join(GetHomeDir(), ".kube")
}

func DefaultKubeConfigFile() string {
return filepath.Join(DefaultKubeConfigDir(), "config")
}

func DefaultTheClusterRootfsDir(clusterName string) string {
return filepath.Join(DefaultSealerDataDir, clusterName, "rootfs")
}

func DefaultTheClusterNydusdDir(clusterName string) string {
return filepath.Join(DefaultSealerDataDir, clusterName, "nydusd")
}

func DefaultTheClusterNydusdFileDir(clusterName string) string {
return filepath.Join(DefaultSealerDataDir, clusterName, "nydusdfile")
}

func DefaultTheClusterRootfsPluginDir(clusterName string) string {
return filepath.Join(DefaultTheClusterRootfsDir(clusterName), "plugins")
}

func TheDefaultClusterCertDir(clusterName string) string {
return filepath.Join(DefaultSealerDataDir, clusterName, "certs")
}

func DefaultClusterBaseDir(clusterName string) string {
return filepath.Join(DefaultSealerDataDir, clusterName)
}

func GetHomeDir() string {
home, err := homedir.Dir()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/imagedistributor/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Distributor interface {

type Mounter interface {
//Mount sealer image with specified platform, return mount Dir,container ID,image ID and err.
Mount(imageName string, platform v1.Platform) (string, string, string, error)
Mount(imageName string, platform v1.Platform, dest string) (string, string, string, error)

//Umount :delete all mounted directory and remove related working container.
Umount(dir, containerID string) error
Expand Down
38 changes: 17 additions & 21 deletions pkg/imagedistributor/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,17 @@ import (
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/pkg/imageengine"
v1 "github.com/sealerio/sealer/types/api/v1"
osi "github.com/sealerio/sealer/utils/os"
"github.com/sealerio/sealer/utils/os/fs"
)

const (
mountBaseDir = "/var/lib/sealer/data/mount"
)

type buildAhMounter struct {
imageEngine imageengine.Interface
}

func (b buildAhMounter) Mount(imageName string, platform v1.Platform) (string, string, string, error) {
path := platform.OS + "_" + platform.Architecture + "_" + platform.Variant
mountDir := filepath.Join(imageMountDir(imageName), path)
if osi.IsFileExist(mountDir) {
err := os.RemoveAll(mountDir)
if err != nil {
return "", "", "", err
}
}
func (b buildAhMounter) Mount(imageName string, platform v1.Platform, dest string) (string, string, string, error) {
mountDir := filepath.Join(dest,
strings.ReplaceAll(imageName, "/", "_"),
strings.Join([]string{platform.OS, platform.Architecture, platform.Variant}, "_"))

imageID, err := b.imageEngine.Pull(&options.PullOptions{
Quiet: false,
Expand Down Expand Up @@ -93,6 +83,7 @@ func NewBuildAhMounter(imageEngine imageengine.Interface) Mounter {

type ImagerMounter struct {
Mounter
rootDir string
hostsPlatform map[v1.Platform][]net.IP
}

Expand All @@ -108,9 +99,9 @@ type ClusterImageMountInfo struct {
func (c ImagerMounter) Mount(imageName string) ([]ClusterImageMountInfo, error) {
var imageMountInfos []ClusterImageMountInfo
for platform, hosts := range c.hostsPlatform {
mountDir, cid, imageID, err := c.Mounter.Mount(imageName, platform)
mountDir, cid, imageID, err := c.Mounter.Mount(imageName, platform, c.rootDir)
if err != nil {
return nil, fmt.Errorf("failed to mount image with platform %s:%v", platform.ToString(), err)
return nil, fmt.Errorf("failed to mount image %s with platform %s:%v", imageName, platform.ToString(), err)
}
imageMountInfos = append(imageMountInfos, ClusterImageMountInfo{
Hosts: hosts,
Expand All @@ -131,21 +122,26 @@ func (c ImagerMounter) Umount(imageName string, imageMountInfo []ClusterImageMou
return fmt.Errorf("failed to umount %s:%v", info.MountDir, err)
}
}

// delete all mounted images
if err := fs.FS.RemoveAll(imageMountDir(imageName)); err != nil {
if err := fs.FS.RemoveAll(c.rootDir); err != nil {
return err
}
return nil
}

func NewImageMounter(imageEngine imageengine.Interface, hostsPlatform map[v1.Platform][]net.IP) (*ImagerMounter, error) {
tempDir, err := os.MkdirTemp("", "sealer-mount-tmp")
if err != nil {
return nil, fmt.Errorf("failed to create tmp mount dir, err: %v", err)
}

c := &ImagerMounter{
// todo : user could set this value by env or sealer config
rootDir: tempDir,
hostsPlatform: hostsPlatform,
}

c.Mounter = NewBuildAhMounter(imageEngine)
return c, nil
}

func imageMountDir(name string) string {
return filepath.Join(mountBaseDir, strings.ReplaceAll(name, "/", "_"))
}
40 changes: 0 additions & 40 deletions pkg/imagedistributor/mount_test.go

This file was deleted.

10 changes: 4 additions & 6 deletions pkg/imagedistributor/scp_distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import (
"os"
"path/filepath"

"github.com/sirupsen/logrus"

"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/config"
"github.com/sealerio/sealer/pkg/env"
"github.com/sealerio/sealer/pkg/infradriver"
v1 "github.com/sealerio/sealer/types/api/v1"
osi "github.com/sealerio/sealer/utils/os"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)

Expand Down Expand Up @@ -181,9 +179,9 @@ func (s *scpDistributor) dumpConfigToRootfs(mountDir string) error {
// using cluster render data to render Rootfs files
func (s *scpDistributor) renderRootfs(mountDir string) error {
var (
renderEtc = filepath.Join(mountDir, common.EtcDir)
renderChart = filepath.Join(mountDir, common.RenderChartsDir)
renderManifests = filepath.Join(mountDir, common.RenderManifestsDir)
renderEtc = filepath.Join(mountDir, "etc")
renderChart = filepath.Join(mountDir, "charts")
renderManifests = filepath.Join(mountDir, "manifests")
renderData = s.infraDriver.GetClusterEnv()
)

Expand Down
3 changes: 1 addition & 2 deletions pkg/imageengine/buildah/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ import (

"github.com/containers/common/libimage"
"github.com/go-errors/errors"
"github.com/sirupsen/logrus"

"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/utils/archive"
"github.com/sirupsen/logrus"
)

var LoadError = errors.Errorf("failed to load new image")
Expand Down
3 changes: 2 additions & 1 deletion pkg/imageengine/buildah/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"os"
"path/filepath"

"github.com/sealerio/sealer/common"

"github.com/containers/common/libimage"
"github.com/containers/common/libimage/manifests"
"github.com/pkg/errors"
"github.com/sealerio/sealer/common"
"github.com/sealerio/sealer/pkg/define/options"
"github.com/sealerio/sealer/utils/archive"
osi "github.com/sealerio/sealer/utils/os"
Expand Down
1 change: 1 addition & 0 deletions pkg/logger/file_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

func NewFileHook(filePath string) (logrus.Hook, error) {
if filePath == "" {
//todo : user could set log dir through sealer config
filePath = common.DefaultLogDir
}

Expand Down
1 change: 0 additions & 1 deletion pkg/runtime/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"

ocispecs "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/sealerio/sealer/common"
osi "github.com/sealerio/sealer/utils/os"
)
Expand Down
9 changes: 3 additions & 6 deletions utils/os/fs/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ import (
"path/filepath"

"github.com/sirupsen/logrus"

"github.com/sealerio/sealer/common"

"golang.org/x/sys/unix"
)

Expand All @@ -34,7 +31,7 @@ type Interface interface {
Stat(name string) (os.FileInfo, error)
Rename(oldPath, newPath string) error
MkdirAll(path string) error
MkTmpdir() (string, error)
MkTmpdir(path string) (string, error)
CopyFile(src, dst string) (int64, error)
CopyDir(srcPath, dstPath string) error
RemoveAll(path ...string) error
Expand Down Expand Up @@ -83,8 +80,8 @@ func (f filesystem) MkdirAll(path string) error {
return os.MkdirAll(path, os.ModePerm)
}

func (f filesystem) MkTmpdir() (string, error) {
tempDir, err := os.MkdirTemp(common.DefaultTmpDir, ".DTmp-")
func (f filesystem) MkTmpdir(path string) (string, error) {
tempDir, err := os.MkdirTemp(path, ".DTmp-")
if err != nil {
return "", err
}
Expand Down
Loading

0 comments on commit 52f25f1

Please sign in to comment.